Skip to content

Commit

Permalink
Added schedule_id label to the task. (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Apr 27, 2024
1 parent a73c8b5 commit 6b23ce5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/guide/scheduling-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,12 @@ Or it can be done manually, by calling `delete_schedule` on schedule source prov
```python
await redis_source.delete_schedule(schedule.schedule_id)
```

Also, you can get schedule_id from the tasks's labels.

```python
@broker.task
async def my_task(context: Context = TaskiqDepends()) -> None:
schedule_id = context.message.labels.get("schedule_id")
print("Schedule ID:", schedule_id)
```
3 changes: 2 additions & 1 deletion taskiq/cli/scheduler/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ async def run_scheduler_loop(scheduler: TaskiqScheduler) -> None:
task_delay = get_task_delay(task)
except ValueError:
logger.warning(
"Cannot parse cron: %s for task: %s",
"Cannot parse cron: %s for task: %s, schedule_id: %s",
task.cron,
task.task_name,
task.schedule_id,
)
continue
if task_delay is not None:
Expand Down
4 changes: 3 additions & 1 deletion taskiq/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ async def on_ready(self, source: "ScheduleSource", task: ScheduledTask) -> None:
except ScheduledTaskCancelledError:
logger.info("Scheduled task %s has been cancelled.", task.task_name)
else:
await AsyncKicker(task.task_name, self.broker, task.labels).kiq(
await AsyncKicker(task.task_name, self.broker, task.labels).with_labels(
schedule_id=task.schedule_id,
).kiq(
*task.args,
**task.kwargs,
)
Expand Down

0 comments on commit 6b23ce5

Please sign in to comment.