API reference: Job¶
- class schedium.types.cancel_job.CancelJob(reason=None)[source][source]¶
Bases:
objectReturn value that cancels (removes) the current job.
If a job’s callable returns an instance of
CancelJob,schedium.scheduler.Scheduler.run_pending()will remove that job from the scheduler.- Attributes:
- reasonstr | None, default None
Optional human-readable reason for cancellation.
- Parameters:
reason (str | None)
- class schedium.job.Job(func, trigger, name=None)[source][source]¶
Bases:
objectA scheduled unit of work.
- Parameters:
- funcCallable[[], object]
A zero-argument callable. The return value is returned by
run()and byschedium.scheduler.Scheduler.run_pending(). If you need to pass arguments, wrap them in a closure or functools.partial.- triggerBaseTrigger
The trigger that decides when this job is due.
- namestr, default None
Optional human-readable label used in repr(job).
- Parameters:
func (Callable[[], object])
trigger (BaseTrigger)
name (str | None)
Notes
- Deduplication
A job runs at most once per trigger event token.
Internally,
schedium.utils.evaluate.evaluate()turns a trigger match into aschedium.triggers.base.TriggerEvent. The event’s token typically represents a time bucket (minute/hour/day, etc.). If the scheduler is called multiple times within the same bucket, the job is considered not due after the first run.- State
Jobs are stateful:
last_eventis updated on successfulrun(). If you want the same callable to run under multiple independent schedules, create multipleJobinstances.- Threading
Jobs do not run in separate threads/processes by default. The scheduler runs them inline.
- is_due(now)[source][source]¶
Return True if the job should run at
now.A job is due when:
its trigger matches at
nowand yields an event token, andthat token is different from the last token the job already ran for.
This method does not mutate state.
- Parameters:
- nowdatetime
Timestamp used to evaluate the trigger.
- Returns:
- bool
True if the job is due at
now.
- Parameters:
now (datetime)
- Return type:
bool
- run(now)[source][source]¶
Run the job if due, update
last_event, and return the result.- Parameters:
- nowdatetime
Timestamp used to evaluate the trigger.
- Returns:
- object
The callable’s return value.
- Raises:
- RuntimeError
If called when the job is not due, or if called again for the same trigger token (double-run protection).
- Parameters:
now (datetime)
- Return type:
object
- run_func()[source][source]¶
Run the job’s callable without checking triggers or updating state.
Particularly useful to override in subclasses that want to customize the run behavior while keeping the same trigger-based scheduling logic.
- Returns:
- object
The callable’s return value.
- Return type:
object
- datetime_of_next_run(after=None, *, max_iterations=100000)[source][source]¶
Return the next run datetime for this job.
This is derived from the trigger’s next validity window.
- Parameters:
- afterdatetime, optional
Lower bound (inclusive) for the computed next run time. If omitted, uses the current system time.
- max_iterationsint, default 100_000
Safety cap used by some triggers/combinators that scan forward.
- Returns:
- datetime | None
The earliest datetime at which this job is due, or
Noneif the trigger tree cannot produce a next window.
- Parameters:
after (datetime | None)
max_iterations (int)
- Return type:
datetime | None