Skip to content

Commit

Permalink
Closes #289: same periodic tasks on different queues can coexist
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Jablon committed Aug 21, 2020
1 parent 80a7b2b commit 8a5764a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 14 additions & 0 deletions docs/howto/cron.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ plan to have the same value for every job::
def run_healthchecks(timestamp: int):
...

The value of those parameters is static, but you could put different values on different
workers. If the same task is periodically deferred to different queues, each job will be
independent::

@app.periodic(cron="*/5 * * * *")
@app.task(
queue=f"healthchecks_{my_worker_id}",
)
def run_healthchecks(timestamp: int):
...

In this setup, each worker (with differing values of ``my_worker_id``) would defer their
own healthcheck jobs, independently from other workers.

Using cron
----------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
ALTER TABLE procrastinate_periodic_defers
ADD COLUMN queue_name character varying(128) NOT NULL DEFAULT 'undefined';

ALTER TABLE procrastinate_periodic_defers
ALTER COLUMN queue_name DROP DEFAULT;

ALTER TABLE procrastinate_periodic_defers
DROP CONSTRAINT procrastinate_periodic_defers_task_name_defer_timestamp_key;

ALTER TABLE procrastinate_periodic_defers
ADD CONSTRAINT procrastinate_periodic_defers_unique UNIQUE (task_name, queue_name, defer_timestamp);

DROP FUNCTION IF EXISTS procrastinate_defer_periodic_job;
CREATE FUNCTION procrastinate_defer_periodic_job(
_queue_name character varying,
Expand All @@ -14,8 +26,8 @@ DECLARE
BEGIN

INSERT
INTO procrastinate_periodic_defers (task_name, defer_timestamp)
VALUES (_task_name, _defer_timestamp)
INTO procrastinate_periodic_defers (task_name, queue_name, defer_timestamp)
VALUES (_task_name, _queue_name, _defer_timestamp)
ON CONFLICT DO NOTHING
RETURNING id into _defer_id;

Expand Down
7 changes: 4 additions & 3 deletions procrastinate/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ CREATE TABLE procrastinate_periodic_defers (
task_name character varying(128) NOT NULL,
defer_timestamp bigint,
job_id bigint REFERENCES procrastinate_jobs(id) NULL,
UNIQUE (task_name, defer_timestamp)
queue_name character varying(128) NOT NULL,
CONSTRAINT procrastinate_periodic_defers_unique UNIQUE (task_name, queue_name, defer_timestamp)
);

CREATE TABLE procrastinate_events (
Expand Down Expand Up @@ -100,8 +101,8 @@ DECLARE
BEGIN

INSERT
INTO procrastinate_periodic_defers (task_name, defer_timestamp)
VALUES (_task_name, _defer_timestamp)
INTO procrastinate_periodic_defers (task_name, queue_name, defer_timestamp)
VALUES (_task_name, _queue_name, _defer_timestamp)
ON CONFLICT DO NOTHING
RETURNING id into _defer_id;

Expand Down

0 comments on commit 8a5764a

Please sign in to comment.