Scheduler Service
SchedulerService
Bases: Service
Service that manages scheduled jobs.
Source code in src/hassette/core/scheduler_service.py
32 33 34 35 36 37 38 39 40 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 | |
serve() -> None
async
Run the scheduler forever, processing jobs as they become due.
Source code in src/hassette/core/scheduler_service.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
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
122 123 124 125 126 127 128 129 130 131 132 133 | |
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
135 136 137 138 139 140 141 142 143 144 145 | |
sleep(next_run_time: ZonedDateTime | 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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
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
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 | |
run_job(job: ScheduledJob) -> 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 |
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 | |
reschedule_job(job: ScheduledJob) -> None
async
Reschedule a job based on its trigger's next_run_time().
If the trigger raises or returns None, the job is treated as exhausted and removed. If the trigger returns a non-future time (delta ≤ 0), a WARNING is logged and the next run is advanced by 1 second. Exceptions from next_run_time() are caught, logged, and treated as exhaustion — the scheduler must never crash due to a misbehaving trigger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
ScheduledJob
|
The job to reschedule. |
required |
Source code in src/hassette/core/scheduler_service.py
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 | |
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.
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
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 | |
get_all_jobs() -> list[ScheduledJob]
async
Return all currently scheduled jobs across all apps.
Source code in src/hassette/core/scheduler_service.py
422 423 424 | |
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
426 427 428 429 430 431 432 433 | |
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
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 | |
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
462 463 464 465 466 467 468 469 470 | |
HeapQueue
dataclass
Bases: Generic[T]
Source code in src/hassette/core/scheduler_service.py
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 | |
__iter__() -> Iterator[T]
Iterate over all items in the queue (unordered).
Source code in src/hassette/core/scheduler_service.py
638 639 640 | |
push(job: T)
Push a job onto the queue.
Source code in src/hassette/core/scheduler_service.py
645 646 647 | |
pop() -> T
Pop the next job from the queue.
Source code in src/hassette/core/scheduler_service.py
649 650 651 | |
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
653 654 655 656 657 658 | |
is_empty() -> bool
Check if the queue is empty.
Source code in src/hassette/core/scheduler_service.py
660 661 662 | |
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
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
remove_item(item: T) -> bool
Remove a specific item from the queue if present.
Source code in src/hassette/core/scheduler_service.py
684 685 686 687 688 689 690 691 692 | |