Scheduler Service
SchedulerService
Bases: Service
Service that manages scheduled jobs.
Source code in src/hassette/core/scheduler_service.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
serve() -> None
async
Run the scheduler forever, processing jobs as they become due.
Source code in src/hassette/core/scheduler_service.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
enqueue_job(job: ScheduledJob) -> None
async
Push a job onto the queue and wake the scheduler.
Source code in src/hassette/core/scheduler_service.py
110 111 112 113 114 | |
register_removal_callback(owner_id: str, callback: Callable[[ScheduledJob], None]) -> None
Register a callback to be called whenever a job belonging to owner_id is removed.
If a callback is already registered for owner_id, the new callback replaces it. This handles legitimate re-registration during hot-reload cycles where the old Scheduler instance is orphaned without a formal shutdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
str
|
The owner whose job removals should trigger the callback. |
required |
callback
|
Callable[[ScheduledJob], None]
|
Called with the removed ScheduledJob as its single argument. |
required |
Source code in src/hassette/core/scheduler_service.py
128 129 130 131 132 133 134 135 136 137 138 139 | |
deregister_removal_callback(owner_id: str) -> None
Remove the removal callback for owner_id, if any.
No-op when owner_id has no registered callback. Called by
Scheduler.on_shutdown so the slot is freed before the Scheduler
is re-initialized (e.g. during a hot-reload cycle).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
str
|
The owner whose callback should be removed. |
required |
Source code in src/hassette/core/scheduler_service.py
141 142 143 144 145 146 147 148 149 150 151 | |
fire_removal_callbacks(jobs: list[ScheduledJob]) -> None
Invoke per-owner removal callbacks for each job in jobs.
Source code in src/hassette/core/scheduler_service.py
153 154 155 156 157 158 | |
apply_jitter_to_heap(job: ScheduledJob) -> None
Apply jitter to the heap sort_index and fire_at without mutating job.next_run.
If job.jitter is not None, a random offset in [0, jitter) seconds is added
to job.fire_at and job.sort_index. job.next_run is never modified
— it is the unjittered logical fire time used as previous_run in subsequent
trigger calls. When jitter is None or 0, fire_at equals next_run exactly
(already set by set_next_run).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job whose sort_index and fire_at should be jittered. |
required |
Source code in src/hassette/core/scheduler_service.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
sleep(next_run_time: ZonedDateTime | None = None) -> None
async
Sleep until the next job is due or a kick is received.
This method will wait for the next job to be due or until a kick is received. If a kick is received, it will wake up immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
next_run_time
|
ZonedDateTime | None
|
Pre-fetched next run time to avoid an extra lock acquisition. If None, uses the default delay. |
None
|
Source code in src/hassette/core/scheduler_service.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
calculate_sleep_time(next_run_time: ZonedDateTime | None) -> TimeDelta
Calculate the time to sleep until the next job is due.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
next_run_time
|
ZonedDateTime | None
|
The next scheduled run time, or None if no jobs are queued. |
required |
Source code in src/hassette/core/scheduler_service.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
add_job(job: ScheduledJob) -> None
async
Register the job in DB, then push it to the queue.
DB registration is awaited inline — job.db_id is set before the job is enqueued to the scheduler heap. This eliminates the window where a job fires with db_id=None.
Trigger type dispatch uses the TriggerProtocol methods exclusively.
Non-protocol triggers are rejected synchronously by Scheduler.schedule()
before reaching this path.
Source code in src/hassette/core/scheduler_service.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
dispatch_and_log(job: ScheduledJob) -> None
async
Dispatch a job and log its execution.
Ordering: skip-if-dequeued → compute next → (enqueue next OR mark for removal) → predicate check → run-through-guard → remove-if-marked.
The current due fire ALWAYS runs once popped. Computing the next occurrence happens first so the next tick is on the heap before the run completes, enabling overlap for recurring jobs. A trigger that raises or returns None marks the job for removal after the current fire — the current fire is never skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job to dispatch. |
required |
Source code in src/hassette/core/scheduler_service.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
run_job_with_guard(job: ScheduledJob, trigger_mode: str | None = None) -> None
async
Route one job invocation through the job's execution-mode guard.
parallel: awaitsrun_jobinline — concurrency comes fromserve()spawning a fresh dispatch task per due-pop. No stall watch, no guard state.single/restart/queued: delegates torun_through_guard, which bridges completion via a per-invocation future and arms the stall watchdog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job to invoke. |
required |
trigger_mode
|
str | None
|
How this execution was triggered (e.g., "manual" for a run-now request). None for regular scheduled fires. |
None
|
Source code in src/hassette/core/scheduler_service.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
warn_stalled_job(job: ScheduledJob, threshold: float) -> None
Emit the stall WARNING: a non-parallel job is still holding its guard.
Called by the stall watchdog after threshold seconds. Named after the job
and its mode so the operator can identify the stuck invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job whose invocation is stalled. |
required |
threshold
|
float
|
The threshold the watchdog armed at, in seconds. |
required |
Source code in src/hassette/core/scheduler_service.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
run_job(job: ScheduledJob, trigger_mode: str | None = None) -> None
async
Run a scheduled job by delegating to the CommandExecutor.
All jobs go through ExecuteJob regardless of whether db_id is set.
When db_id is None (job not yet registered), ExecuteJob is created
with job_db_id=None and the CommandExecutor records an orphan execution row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job to run. |
required |
trigger_mode
|
str | None
|
How this execution was triggered (e.g., "manual" for a run-now request). None for regular scheduled fires. |
None
|
Source code in src/hassette/core/scheduler_service.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
trigger_due_jobs() -> int
async
Fire all jobs due at the current time.
Snapshots due jobs via a single pop_due_and_peek_next(date_utils.now())
call, then awaits each dispatch_and_log(job) inline (not via
task_bucket.spawn). Jobs re-enqueued during dispatch (repeating jobs)
are not included in this invocation — only the initial snapshot is
processed, preventing infinite loops when the clock is frozen.
For queued jobs that return QUEUED_ACCEPTED, dispatch_and_log
blocks on the completion bridge until the queued invocation drains. Under a
frozen clock this can deadlock when the drain callback fires synchronously
within the same sequential loop. Tests that rely on queued multi-tick
behavior must advance the loop with await asyncio.sleep(0) and assert
via the guard state directly, rather than calling trigger_due_jobs twice
back-to-back on the same blocked bridge.
This method bypasses the serve() loop's timing and wakeup logic.
Intended for controlled test dispatch via AppTestHarness.trigger_due_jobs()
or HassetteHarness.scheduler_service.trigger_due_jobs().
Returns:
| Type | Description |
|---|---|
int
|
The number of jobs dispatched. |
Source code in src/hassette/core/scheduler_service.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | |
get_all_jobs() -> list[ScheduledJob]
async
Return all currently scheduled jobs across all apps.
Source code in src/hassette/core/scheduler_service.py
628 629 630 | |
trigger_job(job_id: int) -> ScheduledJob
async
Look up a job on the live scheduler heap by its database id.
Used by the manual-trigger route handler (POST /api/scheduler/jobs/{job_id}/trigger)
to find the job to dispatch. Only looks up and returns the job — the caller is
responsible for any guard pre-check and for dispatching the job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_id
|
int
|
The job's |
required |
Returns:
| Type | Description |
|---|---|
ScheduledJob
|
The matching ScheduledJob from the live heap. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no job with the given job_id is found on the live heap. This covers
a one-shot job that already fired, a job mid-execution from its scheduled fire
(popped from the heap by |
Source code in src/hassette/core/scheduler_service.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
remove_jobs_by_owner(owner: str) -> asyncio.Task[None]
Remove all jobs for a given owner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
str
|
The owner of the jobs to remove. |
required |
Source code in src/hassette/core/scheduler_service.py
658 659 660 661 662 663 664 | |
dequeue_job(job: ScheduledJob) -> bool
Remove a job from the scheduler synchronously, fire removal callbacks, and kick.
Calls _ScheduledJobQueue.remove_item_sync directly (no lock). Fires
fire_removal_callbacks unconditionally — even when the job was not in
the heap — to prevent dict leaks when the serve loop already popped the job.
Calls kick() only when the job was actually removed from the heap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job to remove. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the job was found and removed from the heap, False otherwise. |
Source code in src/hassette/core/scheduler_service.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |
mark_job_cancelled(db_id: int) -> None
async
Persist durable cancellation state for a job by setting cancelled_at in the DB.
Delegates to CommandExecutor.mark_job_cancelled. No-op when db_id is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_id
|
int
|
The |
required |
Source code in src/hassette/core/scheduler_service.py
704 705 706 707 708 709 710 711 712 | |
HeapQueue
dataclass
Bases: Generic[T]
Source code in src/hassette/core/scheduler_service.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | |
__iter__() -> Iterator[T]
Iterate over all items in the queue (unordered).
Source code in src/hassette/core/scheduler_service.py
835 836 837 | |
push(job: T) -> None
Push a job onto the queue.
Source code in src/hassette/core/scheduler_service.py
842 843 844 | |
pop() -> T
Pop the next job from the queue.
Source code in src/hassette/core/scheduler_service.py
846 847 848 | |
peek() -> T | None
Peek at the next job without removing it.
Returns:
| Type | Description |
|---|---|
T | None
|
T | None: The next job in the queue, or None if the queue is empty |
Source code in src/hassette/core/scheduler_service.py
850 851 852 853 854 855 856 | |
is_empty() -> bool
Check if the queue is empty.
Source code in src/hassette/core/scheduler_service.py
858 859 860 | |
remove_where(predicate: Callable[[T], bool]) -> list[T]
Remove all items matching the predicate, returning the removed items.
Source code in src/hassette/core/scheduler_service.py
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 | |
remove_item(item: T) -> bool
Remove a specific item from the queue if present.
Source code in src/hassette/core/scheduler_service.py
881 882 883 884 885 886 887 888 | |