Skip to content

Commit 9ea055a

Browse files
committed
Make scheduled tasks configurable via worker.configure_scheduled_tasks()
Replace the free function configure_scheduled_tasks(worker) with a TaskHubGrpcWorker.configure_scheduled_tasks() method and remove the registration module. The feature is unreleased, so this is a clean breaking change to a single canonical pattern consistent with add_orchestrator/add_entity.
1 parent ea459df commit 9ea055a

8 files changed

Lines changed: 36 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1010
ADDED
1111

1212
- Added `durabletask.scheduled`, a recurring schedule feature built on durable
13-
entities. Use `configure_scheduled_tasks(worker)` to enable it on a worker,
13+
entities. Use `worker.configure_scheduled_tasks()` to enable it on a worker,
1414
then manage schedules from the client via `ScheduledTaskClient` (and the
1515
per-schedule `ScheduleClient`). Supports creating, describing, listing,
1616
updating, pausing, resuming, and deleting schedules with configurable

durabletask/scheduled/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"""Scheduled tasks support for the Durable Task SDK.
55
66
This package provides a recurring schedule feature built on top of durable
7-
entities and a helper orchestrator. Register the entity and orchestrator with a
8-
worker via :func:`configure_scheduled_tasks`, then manage schedules from the
9-
client via :class:`ScheduledTaskClient`.
7+
entities and a helper orchestrator. Enable it on a worker via
8+
:meth:`durabletask.worker.TaskHubGrpcWorker.configure_scheduled_tasks`, then
9+
manage schedules from the client via :class:`ScheduledTaskClient`.
1010
"""
1111

1212
from durabletask.scheduled.client import ScheduleClient, ScheduledTaskClient
@@ -17,7 +17,6 @@
1717
from durabletask.scheduled.models import (ScheduleCreationOptions,
1818
ScheduleDescription, ScheduleQuery,
1919
ScheduleUpdateOptions)
20-
from durabletask.scheduled.registration import configure_scheduled_tasks
2120
from durabletask.scheduled.schedule_status import ScheduleStatus
2221

2322
__all__ = [
@@ -32,7 +31,6 @@
3231
"ScheduleNotFoundError",
3332
"ScheduleClientValidationError",
3433
"ScheduleInvalidTransitionError",
35-
"configure_scheduled_tasks",
3634
]
3735

3836
PACKAGE_NAME = "durabletask.scheduled"

durabletask/scheduled/registration.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

durabletask/worker.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def __init__(
580580
# Extra worker capabilities advertised to the backend in
581581
# GetWorkItemsRequest (in addition to ones derived from worker state such
582582
# as LARGE_PAYLOADS). Feature-enablement helpers like
583-
# durabletask.scheduled.configure_scheduled_tasks register theirs here.
583+
# TaskHubGrpcWorker.configure_scheduled_tasks register theirs here.
584584
self._capabilities: set[int] = set()
585585

586586
@property
@@ -645,16 +645,37 @@ def add_capability(self, capability: int) -> None:
645645
"""Advertise a worker capability to the backend in ``GetWorkItemsRequest``.
646646
647647
Most users do not call this directly; feature-enablement helpers such as
648-
:func:`durabletask.scheduled.configure_scheduled_tasks` use it to
649-
advertise the capabilities (``pb.WORKER_CAPABILITY_*``) their feature
650-
relies on.
648+
:meth:`configure_scheduled_tasks` use it to advertise the capabilities
649+
(``pb.WORKER_CAPABILITY_*``) their feature relies on.
651650
"""
652651
if self._is_running:
653652
raise RuntimeError(
654653
"Capabilities cannot be added while the worker is running."
655654
)
656655
self._capabilities.add(capability)
657656

657+
def configure_scheduled_tasks(self) -> None:
658+
"""Enable scheduled tasks support on this worker.
659+
660+
Registers the schedule entity and operation orchestrator and advertises
661+
the scheduled-tasks capability to the backend. Call this before starting
662+
the worker. Schedules are then managed from the client via
663+
:class:`durabletask.scheduled.ScheduledTaskClient`.
664+
"""
665+
if self._is_running:
666+
raise RuntimeError(
667+
"Scheduled tasks cannot be configured while the worker is running."
668+
)
669+
# Imported lazily to avoid a circular import: durabletask.scheduled
670+
# imports from durabletask.worker.
671+
from durabletask.scheduled.orchestrator import \
672+
execute_schedule_operation_orchestrator
673+
from durabletask.scheduled.schedule_entity import ENTITY_NAME, Schedule
674+
675+
self.add_entity(Schedule, ENTITY_NAME)
676+
self.add_orchestrator(execute_schedule_operation_orchestrator)
677+
self.add_capability(pb.WORKER_CAPABILITY_SCHEDULED_TASKS)
678+
658679
def use_versioning(self, version: VersioningOptions) -> None:
659680
"""Initializes versioning options for sub-orchestrators and activities."""
660681
if self._is_running:

examples/scheduled_tasks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from durabletask import task
1818
from durabletask.azuremanaged.client import DurableTaskSchedulerClient
1919
from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker
20-
from durabletask.scheduled import (ScheduledTaskClient, ScheduleCreationOptions,
21-
configure_scheduled_tasks)
20+
from durabletask.scheduled import ScheduledTaskClient, ScheduleCreationOptions
2221

2322

2423
def greet_orchestrator(ctx: task.OrchestrationContext, name: str) -> Generator[task.Task[Any], Any, Any]:
@@ -41,7 +40,7 @@ def greet_orchestrator(ctx: task.OrchestrationContext, name: str) -> Generator[t
4140
taskhub=taskhub_name, token_credential=credential) as worker:
4241
worker.add_orchestrator(greet_orchestrator)
4342
# Register the schedule entity and operation orchestrator.
44-
configure_scheduled_tasks(worker)
43+
worker.configure_scheduled_tasks()
4544
worker.start()
4645

4746
client = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=secure_channel,

tests/durabletask-azuremanaged/scheduled/test_dts_scheduled_e2e.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker
2020
from durabletask.scheduled import (ScheduledTaskClient, ScheduleCreationOptions,
2121
ScheduleQuery, ScheduleStatus,
22-
ScheduleUpdateOptions,
23-
configure_scheduled_tasks)
22+
ScheduleUpdateOptions)
2423

2524
import os
2625

@@ -61,7 +60,7 @@ def _make_worker() -> DurableTaskSchedulerWorker:
6160
w = DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=secure_channel,
6261
taskhub=taskhub_name, token_credential=None)
6362
w.add_orchestrator(target_orchestrator)
64-
configure_scheduled_tasks(w)
63+
w.configure_scheduled_tasks()
6564
return w
6665

6766

tests/durabletask/scheduled/test_client_filter_and_capability.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from datetime import datetime, timedelta, timezone
77

88
import durabletask.internal.orchestrator_service_pb2 as pb
9-
from durabletask.scheduled import configure_scheduled_tasks
109
from durabletask.scheduled.client import ScheduledTaskClient
1110
from durabletask.scheduled.models import (ScheduleConfiguration,
1211
ScheduleCreationOptions, ScheduleQuery,
@@ -69,7 +68,7 @@ def test_status_filter(self):
6968
class TestScheduledTasksCapability:
7069
def test_configure_advertises_scheduled_tasks_capability(self):
7170
worker = TaskHubGrpcWorker()
72-
configure_scheduled_tasks(worker)
71+
worker.configure_scheduled_tasks()
7372
assert pb.WORKER_CAPABILITY_SCHEDULED_TASKS in worker._capabilities # pyright: ignore[reportPrivateUsage]
7473

7574
def test_capability_absent_by_default(self):

tests/durabletask/scheduled/test_scheduled_e2e.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from durabletask import client, task, worker
1313
from durabletask.scheduled import (ScheduledTaskClient, ScheduleCreationOptions,
1414
ScheduleQuery, ScheduleStatus,
15-
ScheduleUpdateOptions,
16-
configure_scheduled_tasks)
15+
ScheduleUpdateOptions)
1716
from durabletask.testing import create_test_backend
1817

1918
from tests.durabletask._port_utils import find_free_port
@@ -63,7 +62,7 @@ def target_orchestrator(ctx: task.OrchestrationContext, value):
6362
def _make_worker() -> worker.TaskHubGrpcWorker:
6463
w = worker.TaskHubGrpcWorker(host_address=HOST)
6564
w.add_orchestrator(target_orchestrator)
66-
configure_scheduled_tasks(w)
65+
w.configure_scheduled_tasks()
6766
return w
6867

6968

0 commit comments

Comments
 (0)