Scheduled Tasks
How to set up recurring tasks for automated monitoring and reporting.
Scheduled Tasks
CodeSpar includes a built-in task scheduler that runs recurring operations at fixed intervals. Use it for health checks, periodic reports, cleanup jobs, or any custom automation.
Scheduling a Task
Use the scheduler.schedule() method to register a recurring task:
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | Unique identifier for the task. Used in API endpoints and logging. |
intervalMs | number | Interval between runs in milliseconds. Minimum: 10000 (10 seconds). |
handler | () => Promise<void> | Async function to execute on each run. |
Built-in Tasks
CodeSpar registers these tasks automatically on startup:
health-check (every 5 minutes)
Verifies that all system components are operational:
- Agent supervisor is running
- All enabled channel adapters are connected
- File storage is accessible
- AI API key is valid (lightweight ping)
Results are logged and available via GET /api/metrics.
build-status-report (every 24 hours)
Collects build status from all linked GitHub repositories:
- Fetches latest workflow runs via GitHub API
- Stores pass/fail/pending counts
- Updates the cached status used by
@codespar status build
audit-cleanup (every 24 hours)
Maintains the audit trail storage:
- Removes entries older than the retention period (default: 365 days)
- Preserves hash chain integrity by keeping boundary entries
- Logs the number of entries cleaned up
Creating Custom Scheduled Tasks
You can register custom tasks in your agent plugin or startup script:
Managing Tasks via API
All scheduled tasks can be managed through the Scheduler API:
List all tasks
Pause a task
Resume a task
Cancel a task
Error Isolation
Each task runs in its own try/catch block. If a task handler throws an error:
- The error is logged with the task name and stack trace.
- The task's
errorscounter is incremented. - The task remains scheduled and will run again at the next interval.
- Other tasks are not affected.
This means a failing health check will not prevent the build status report from running, and vice versa.
Next Steps
- Scheduler API -- REST endpoints for task management
- Configuration -- Environment variables
- Webhook Monitoring -- GitHub event processing