Overview
Monitoring services poll SyftBox for changes and trigger actions:- JobMonitor - Detects new jobs, approvals, and completions
- PeerMonitor - Detects peer connection requests and approvals
- DaemonManager - Manages background service lifecycle
JobMonitor
Monitors job lifecycle events using a hybrid approach:- Google Drive API - Polls for new job submissions (lightweight, no sync)
- Local filesystem - Checks for status changes (approved, executed)
Quick Start
Factory Methods
Create monitor from authenticated clientParameters:
client: SyftboxManager- Client fromsc.login_do()gmail_token_path: Optional[str]- Gmail token path (default:~/.syft-notifications/gmail_token.json)drive_token_path: Optional[str]- Drive token path (auto-detected from credentials dir)notifications: bool = True- Enable email notifications
JobMonitor instanceCreate monitor from YAML configuration fileParameters:
config_path: str- Path to config.yaml
JobMonitor instanceConfig format:Constructor
Path to SyftBox directory
Data Owner email address
Email sender implementation (e.g., GmailSender)
State tracker to prevent duplicate notifications
Configuration with notification toggles:
notify_on_new_job: boolnotify_on_approved: boolnotify_on_executed: bool
Google Drive token for direct polling (enables lightweight mode)
Fallback client if no drive_token_path (uses full sync)
Monitoring Methods
Run monitoring checks (blocking)Parameters:
interval: Optional[int]- If None, runs once. If set, polls every N secondsduration: Optional[int]- How long to run in seconds (None = infinite)
Start monitoring in background thread (non-blocking)Parameters:
interval: int = 10- Check interval in seconds
threading.Thread - Background thread handleStop background monitoring thread
Detection Logic
New Jobs (via Drive API):- Find inbox folders:
syft_outbox_inbox_{sender}_to_{recipient} - List message files:
msgv2_* - Download and parse messages
- Extract job info from
ProposedFileChangesMessage - Send notification if not already processed
- Scan
{syftbox_root}/{do_email}/app_data/job/ - For each job directory:
- Check for
approvedmarker file → Send approval notification - Check for
donemarker file → Send completion notification
- Check for
- Only notify for jobs previously detected as “new”
PeerMonitor
Monitors peer connection requests using Google Drive API.Quick Start
Factory Method
Create monitor from authenticated clientParameters:
client: SyftboxManager- Client fromsc.login_do()gmail_token_path: Optional[str]- Gmail token pathnotifications: bool = True- Enable notifications
PeerMonitor instanceConstructor
Data Owner email address
Google Drive OAuth token path (required for Drive polling)
Email sender implementation
State tracker for notification history
Configuration:
notify_on_new_peer: bool- Send notification for new peer requestsnotify_on_peer_granted: bool- Send notification when peer approved
Methods
Manually trigger peer approval notificationParameters:Note: Automatic detection of peer approvals is not possible via Drive API alone,
as
ds_email: str- Data Scientist email
add_peer_as_do() doesn’t create new folders. Must be called manually.Detection Logic
New Peer Requests:- Query Drive for folders:
syft_outbox_inbox_{sender}_to_{do_email} - Filter folders where sender ≠ DO (external peer requests)
- Compare with previous snapshot
- Send notifications:
- To DO: “New peer request from ”
- To DS: “Peer request sent to “
DaemonManager
Manages background daemon processes with PID tracking and log rotation.Quick Start
Constructor
Parameters:
config_path: Path- Path to daemon configuration YAML
~/.syft-creds/:syft-notify.pid- Process ID filesyft-notify.log- Main log filesyft-notify.error.log- Error log file
Daemon Control
Start daemon in backgroundParameters:
interval: Optional[int]- Check interval (overrides config)
bool - True if started successfullyForks into background, redirects stdout/stderr to log files.Stop running daemonReturns:
bool - True if stopped successfullySends SIGTERM for graceful shutdown, waits 10s, then sends SIGKILL if needed.Restart daemonParameters:
interval: Optional[int]- Check interval
bool - True if restarted successfullyCheck daemon status and display recent activityReturns:
bool - True if runningPrints:- PID and config path
- Log file locations
- Last 5 log lines
Log Management
Display daemon logsParameters:
follow: bool = False- Follow logs (liketail -f)lines: int = 50- Number of lines to show (if not following)
Log Rotation
Automatic log rotation:- Max file size: 10 MB
- Backup count: 7 files
- Format:
YYYY-MM-DD HH:MM:SS - logger - level - message
Signal Handling
- SIGTERM - Graceful shutdown
- SIGHUP - Config reload (not yet implemented)
PID Management
Get daemon process IDReturns:
Optional[int] - PID or None if not runningCheck if daemon process existsReturns:
bool - True if process is aliveUses os.kill(pid, 0) to check without sending actual signal.Monitor Base Class
All monitors inherit from abstractMonitor base class.
Abstract Methods
Check all entities for events (must be implemented by subclasses)This is where monitor-specific logic goes:
- Query data sources (Drive API, filesystem, database)
- Detect changes
- Send notifications
- Update state
Common Configuration
CLI Integration
Thesyft-bg CLI wraps these monitoring services:
Service Registry
Services are registered insyft_bg/services/registry.py:
Drive API Integration
Scopes Required
- List folders
- Read file metadata
- Download file contents
Building Drive Service
Thread Safety
Important:googleapiclient.discovery.build() is not thread-safe.
Monitors create Drive service on the main thread in __init__:
Best Practices
- Use from_client for notebooks - Simplest setup with existing client
- Use from_config for daemons - Better for production services
- Enable Drive token - Enables lightweight polling without full sync
- Set reasonable intervals:
- Notifications: 30 seconds
- Approvals: 5 seconds
- Monitor logs regularly - Use
syft-bg logs -fto catch issues - Use state management - Prevents duplicate notifications
- Run in background threads - Non-blocking for interactive notebooks
Error Handling
Monitors catch and log errors without crashing:- Console (if running in foreground)
~/.syft-creds/syft-notify.log(if running as daemon)~/.syft-creds/syft-notify.error.log(stderr)
Performance Considerations
Lightweight Polling
JobMonitor with Drive API:- No full sync required
- Only downloads message headers
- Parses only job-related messages
- Skips non-job messages without processing
State Caching
PeerMonitor snapshots:- Stores previous peer list in state
- Only processes new peers (delta detection)
- Avoids re-processing known peers
Interval Tuning
See Also
- Notifications - Email sender and templates
- Auto-Approval - Automatic approval rules