-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Announce jobs using PG trigger
Previously, new, non-future jobs were announced by executing `NOTIFY` in Go code. Triggers are much better suited for this, and reduces neoq complexity by allowing PG to perform notification work for most jobs. "Future" jobs continue to use the `announceJob` method on a timer.
- Loading branch information
Showing
3 changed files
with
18 additions
and
13 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
backends/postgres/migrations/20230928141356_create-new-job-trigger.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TRIGGER IF EXISTS announce_job ON neoq_jobs CASCADE; |
12 changes: 12 additions & 0 deletions
12
backends/postgres/migrations/20230928141356_create-new-job-trigger.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE OR REPLACE FUNCTION announce_job() RETURNS trigger AS $$ | ||
DECLARE | ||
BEGIN | ||
PERFORM pg_notify(CAST(NEW.queue AS text), CAST(NEW.id AS text)); | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER announce_job | ||
AFTER INSERT ON neoq_jobs FOR EACH ROW | ||
WHEN (NEW.run_after <= timezone('utc', NEW.created_at)) | ||
EXECUTE PROCEDURE announce_job(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters