Skip to content

Commit

Permalink
Run job queue's createNotificationTrigger inside a single transaction
Browse files Browse the repository at this point in the history
This fixes jobs sometimes not being picked up directly
  • Loading branch information
mpscholten committed Nov 5, 2021
1 parent ab3be5f commit de0a51d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion IHP/Job/Queue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ pollForJob tableName pollInterval handleJob = do
Concurrent.threadDelay pollIntervalWithJitter

createNotificationTrigger :: Text -> Text
createNotificationTrigger tableName = "CREATE OR REPLACE FUNCTION " <> functionName <> "() RETURNS TRIGGER AS $$"
createNotificationTrigger tableName = ""
<> "BEGIN;\n"
<> "CREATE OR REPLACE FUNCTION " <> functionName <> "() RETURNS TRIGGER AS $$"
<> "BEGIN\n"
<> " PERFORM pg_notify('" <> eventName tableName <> "', '');\n"
<> " RETURN new;"
<> "END;\n"
<> "$$ language plpgsql;"
<> "DROP TRIGGER IF EXISTS " <> insertTriggerName <> " ON " <> tableName <> "; CREATE TRIGGER " <> insertTriggerName <> " AFTER INSERT ON \"" <> tableName <> "\" FOR EACH ROW WHEN (NEW.status = 'job_status_not_started' OR NEW.status = 'job_status_retry') EXECUTE PROCEDURE " <> functionName <> "();\n"
<> "DROP TRIGGER IF EXISTS " <> updateTriggerName <> " ON " <> tableName <> "; CREATE TRIGGER " <> updateTriggerName <> " AFTER UPDATE ON \"" <> tableName <> "\" FOR EACH ROW WHEN (NEW.status = 'job_status_not_started' OR NEW.status = 'job_status_retry') EXECUTE PROCEDURE " <> functionName <> "();\n"
<> "COMMIT;"
where
functionName = "notify_job_queued_" <> tableName
insertTriggerName = "did_insert_job_" <> tableName
Expand Down

0 comments on commit de0a51d

Please sign in to comment.