Implement cronjob system #22

Closed
opened 2020-11-10 06:31:10 +00:00 by dxs · 3 comments

Some sort of a system that uses the database to orchestrate running jobs on a regular interval without executing more than necessary.

Some sort of a system that uses the database to orchestrate running jobs on a regular interval without executing more than necessary.
Poster
Owner

Functions that need this:

  • Bans
  • Updating empty metadata
  • Retrieving followers and subscribers
Functions that need this: - Bans - Updating empty metadata - Retrieving followers and subscribers
Poster
Owner
CREATE TABLE cron (
    id integer GENERATED ALWAYS AS IDENTITY,
    name varchar NOT NULL, -- Application-recognizable name for the job
    runinterval interval, -- Duration between runs
    lastSuccess timestamptz -- Last successful run - only gets updated if run is successful
);

BEGIN; -- Transaction necessary for locking
SELECT * FROM cron
  WHERE (lastSuccess + runInterval) < now() -- Next run should be before now
  FOR UPDATE SKIP LOCKED LIMIT 1; -- Lock one not-already-locked row
-- If no rows returned by select, either no pending jobs or all pending jobs are already being handled. Either way, nothing to do, so application subroutine should exit
COMMIT;

``` CREATE TABLE cron ( id integer GENERATED ALWAYS AS IDENTITY, name varchar NOT NULL, -- Application-recognizable name for the job runinterval interval, -- Duration between runs lastSuccess timestamptz -- Last successful run - only gets updated if run is successful ); BEGIN; -- Transaction necessary for locking SELECT * FROM cron WHERE (lastSuccess + runInterval) < now() -- Next run should be before now FOR UPDATE SKIP LOCKED LIMIT 1; -- Lock one not-already-locked row -- If no rows returned by select, either no pending jobs or all pending jobs are already being handled. Either way, nothing to do, so application subroutine should exit COMMIT; ```
dxs added the
feature
label 2020-11-28 18:21:34 +00:00
dxs added this to the v1.0 milestone 2020-11-28 18:21:37 +00:00
Poster
Owner

Updated version:

CREATE TABLE cron (
    id integer GENERATED ALWAYS AS IDENTITY,
    name varchar NOT NULL, -- Application-recognizable name for the job
    runinterval interval, -- Duration between runs
    lastSuccess timestamptz -- Last successful run - only gets updated if run is successful
);

BEGIN; -- Transaction necessary for locking
SELECT * FROM cron
  WHERE (lastSuccess + runInterval) < now() -- Next run should be before now
  AND name = 'jobName' -- Application will iterate through jobs it knows about, specifying job name in query
  FOR UPDATE SKIP LOCKED; -- Lock row, do not include rows currently locked
-- If no rows returned by select, either no pending jobs or all pending jobs are already being handled. Either way, nothing to do, so application subroutine should exit
COMMIT;

Changes from app saying "give me a job to do" to "does this job need done?". This way, the application will never get a job it doesn't know how to handle, and will guarantee all jobs that need to get run do so in one cron cycle.

Updated version: ``` CREATE TABLE cron ( id integer GENERATED ALWAYS AS IDENTITY, name varchar NOT NULL, -- Application-recognizable name for the job runinterval interval, -- Duration between runs lastSuccess timestamptz -- Last successful run - only gets updated if run is successful ); BEGIN; -- Transaction necessary for locking SELECT * FROM cron WHERE (lastSuccess + runInterval) < now() -- Next run should be before now AND name = 'jobName' -- Application will iterate through jobs it knows about, specifying job name in query FOR UPDATE SKIP LOCKED; -- Lock row, do not include rows currently locked -- If no rows returned by select, either no pending jobs or all pending jobs are already being handled. Either way, nothing to do, so application subroutine should exit COMMIT; ``` Changes from app saying "give me a job to do" to "does this job need done?". This way, the application will never get a job it doesn't know how to handle, and will guarantee all jobs that need to get run do so in one cron cycle.
dxs referenced this issue from a commit 2020-11-29 06:27:10 +00:00
dxs closed this issue 2020-11-29 06:27:35 +00:00
dxs referenced this issue from a commit 2022-01-06 04:41:26 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: dxs/learn-request-queue#22
There is no content yet.