trading_agent.scheduling.cron_scheduler
¶
Runs every desk's scheduled jobs: the user's, the desk's own, the heartbeat and the after-close rollover.
The scheduler is a loop with no model. When it starts, it makes up once for each run missed while the gateway was down, if the missed run was within the last 24 hours and the job allows it. Then, every 30 seconds or sooner, it starts each job whose next run has come:
- an agent job for the desk joins the desk's queue as a trigger, so it never runs beside one of the desk's turns; the heartbeat is skipped when the desk is busy or has triggers waiting, so heartbeats cannot pile up;
- an agent job for a subagent and a command job run on their own, and their result is published as a cron_result event;
- a job with a delivery address has its result POSTed there, with secrets masked;
- a one-off job is deleted after it starts.
Each run is recorded in the desk's runs.jsonl before it starts, so a crash cannot make a job run twice.
Typical usage example:
scheduler = CronScheduler(desk_schedules, calendar, hours, event_bus, redactor) task = asyncio.create_task(scheduler.run())
DeskSchedule
dataclass
¶
What the scheduler needs for one desk.
Attributes:
| Name | Type | Description |
|---|---|---|
desk |
DeskRuntime
|
The running desk. |
store |
CronJobStore
|
Its jobs and run record. |
built_in_jobs |
list[CronJob]
|
The heartbeat and rollover jobs from trading_agent.toml. |
command_runner |
CommandJobRunner
|
Runs its command jobs. |
subagent_runner |
SubagentRunner
|
Runs its subagent jobs. |
CronScheduler
¶
Starts scheduled jobs on time.
Subscribes to the bus at once, so no result to deliver is missed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedules
|
Mapping[str, DeskSchedule]
|
Each desk's schedule, keyed by desk name. |
required |
calendar
|
MarketCalendar
|
Decides trading days. |
required |
hours
|
ExchangeHours
|
Decides market hours. |
required |
event_bus
|
EventBus
|
Where runs and results are published, and desk answers are read for delivery. |
required |
redactor
|
SecretRedactor | None
|
Masks secrets in delivered results, or None. |
None
|
clock
|
Callable[[], datetime] | None
|
Returns the current time; None uses the system clock. Tests replace it. |
None
|
Source code in src/trading_agent/scheduling/cron_scheduler.py
now
¶
The current time in India.
Returns:
| Type | Description |
|---|---|
datetime
|
The time. |
run
async
¶
Catches up, then starts due jobs until cancelled.
Source code in src/trading_agent/scheduling/cron_scheduler.py
catch_up
async
¶
Starts once each job that missed a run while the gateway was down.
Returns:
| Type | Description |
|---|---|
list[str]
|
The names of the jobs started. |
Source code in src/trading_agent/scheduling/cron_scheduler.py
tick
async
¶
Starts every job whose next run has come.
Returns:
| Type | Description |
|---|---|
datetime
|
When to look again: the earliest next run, but no later than 30 seconds from now. |
Source code in src/trading_agent/scheduling/cron_scheduler.py
run_now
async
¶
Starts one job at once, as if its time had come.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
job_id
|
str
|
The job. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the job was found and started, False when the desk or job is unknown. |
Source code in src/trading_agent/scheduling/cron_scheduler.py
upcoming
¶
The next few runs of a job, for lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
CronJob
|
The job. |
required |
count
|
int
|
How many runs. |
required |
Returns:
| Type | Description |
|---|---|
list[datetime]
|
The runs, soonest first. |