Skip to content

Commit ad943cc

Browse files
committed
Update README with Recurring tasks info
1 parent 596d580 commit ad943cc

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ It is recommended to set this value less than or equal to the queue database's c
250250
- `concurrency_maintenance`: whether the dispatcher will perform the concurrency maintenance work. This is `true` by default, and it's useful if you don't use any [concurrency controls](#concurrency-controls) and want to disable it or if you run multiple dispatchers and want some of them to just dispatch jobs without doing anything else.
251251

252252

253+
### Scheduler polling interval
254+
255+
The scheduler process checks for due recurring tasks and reloads dynamic tasks at a configurable interval. You can set this interval using the `polling_interval` key under the `scheduler` section in your `config/queue.yml`:
256+
257+
```yaml
258+
scheduler:
259+
polling_interval: 5 # seconds
260+
```
261+
262+
This controls how frequently the scheduler wakes up to enqueue due recurring jobs and reload dynamic tasks.
263+
253264
### Queue order and priorities
254265

255266
As mentioned above, if you specify a list of queues for a worker, these will be polled in the order given, such as for the list `real_time,background`, no jobs will be taken from `background` unless there aren't any more jobs waiting in `real_time`.
@@ -607,7 +618,45 @@ Rails.application.config.after_initialize do # or to_prepare
607618
end
608619
```
609620

610-
You can also dynamically add or remove recurring tasks by creating or deleting SolidQueue::RecurringTask records. It works the same way as with static tasks, except you must set the static field to false. Changes won’t be picked up immediately — they take effect after about a one-minute delay.
621+
### Creating and Deleting Recurring Tasks Dynamically
622+
623+
You can create and delete recurring tasks at runtime, without editing the configuration file. Use the following methods:
624+
625+
#### Creating a recurring task
626+
627+
```ruby
628+
SolidQueue.schedule_recurring_task(
629+
"my_dynamic_task",
630+
command: "puts 'Hello from a dynamic task!'",
631+
schedule: "every 10 minutes"
632+
)
633+
```
634+
635+
This will create a dynamic recurring task with the given key, command, and schedule. You can also use the `class` and `args` options as in the configuration file.
636+
637+
#### Deleting a recurring task
638+
639+
```ruby
640+
SolidQueue.delete_recurring_task(task_id)
641+
```
642+
643+
This will delete a dynamically scheduled recurring task by its ID. If you attempt to delete a static (configuration-defined) recurring task, an error will be raised.
644+
645+
> **Note:** Static recurring tasks (those defined in `config/recurring.yml`) cannot be deleted at runtime. Attempting to do so will raise an error.
646+
647+
#### Example: Creating and deleting a recurring task
648+
649+
```ruby
650+
# Create a new dynamic recurring task
651+
recurring_task = SolidQueue.schedule_recurring_task(
652+
"cleanup_temp_files",
653+
command: "TempFileCleaner.clean!",
654+
schedule: "every day at 2am"
655+
)
656+
657+
# Delete the task later by ID
658+
SolidQueue.delete_recurring_task(recurring_task.id)
659+
```
611660

612661
It's possible to run multiple schedulers with the same `recurring_tasks` configuration, for example, if you have multiple servers for redundancy, and you run the `scheduler` in more than one of them. To avoid enqueuing duplicate tasks at the same time, an entry in a new `solid_queue_recurring_executions` table is created in the same transaction as the job is enqueued. This table has a unique index on `task_key` and `run_at`, ensuring only one entry per task per time will be created. This only works if you have `preserve_finished_jobs` set to `true` (the default), and the guarantee applies as long as you keep the jobs around.
613662

0 commit comments

Comments
 (0)