Skip to content

Commit bc64c8a

Browse files
committed
Add update script
1 parent 50fc513 commit bc64c8a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lantern_hnsw/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ set (_update_files
268268
sql/updates/0.3.4--0.4.0.sql
269269
sql/updates/0.4.0--0.4.1.sql
270270
sql/updates/0.4.1--0.5.0.sql
271+
sql/updates/0.5.0--0.6.0.sql
271272
)
272273

273274
# Generate version information for the binary
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
ALTER TABLE lantern.tasks ADD COLUMN pg_cron_jobid bigint default null;
2+
CREATE OR REPLACE FUNCTION _lantern_internal.async_task_finalizer_trigger() RETURNS TRIGGER AS $$
3+
DECLARE
4+
res RECORD;
5+
BEGIN
6+
-- if NEW.status is one of "starting", "running", "sending, "connecting", return
7+
IF NEW.status IN ('starting', 'running', 'sending', 'connecting') THEN
8+
RETURN NEW;
9+
END IF;
10+
11+
IF NEW.status NOT IN ('succeeded', 'failed') THEN
12+
RAISE WARNING 'Lantern Async tasks: Unexpected status %', NEW.status;
13+
END IF;
14+
15+
-- Update pg_cron_jobid on lantern.tasks table before the job is unscheduled
16+
-- This is necessary because under some circumstances jobs continue changing status even after they no longer
17+
-- appear in cron.job. The easiest way to trigger this case is to schedule a multi-statement job
18+
-- where the second statement causes a failure, e.g. async_task('select 1; select haha;')
19+
UPDATE lantern.tasks t SET
20+
pg_cron_jobid = c.jobid
21+
FROM cron.job c
22+
LEFT JOIN cron.job_run_details run
23+
ON c.jobid = run.jobid
24+
WHERE
25+
t.pg_cron_job_name = c.jobname AND
26+
c.jobid = NEW.jobid
27+
-- using returning as a trick to run the unschedule function as a side effect
28+
-- Note: have to unschedule by jobid because of pg_cron#320 https://github.com/citusdata/pg_cron/issues/320
29+
-- Note2: unscheduling happens here since the update below may run multiple times for the same async task
30+
-- and unscheduling same job multiple times is not allowed
31+
-- At least experimentally so far, this update runs once per async task
32+
RETURNING cron.unschedule(NEW.jobid) INTO res;
33+
34+
-- Get the job name from the jobid
35+
-- Call the job finalizer if corresponding job exists BOTH in lantern async tasks AND
36+
-- active cron jobs
37+
UPDATE lantern.tasks t SET
38+
(duration, status, error_message) = (run.end_time - t.started_at, NEW.status,
39+
CASE WHEN NEW.status = 'failed' THEN return_message ELSE NULL END)
40+
FROM cron.job_run_details run
41+
WHERE
42+
t.pg_cron_jobid = NEW.jobid
43+
AND t.pg_cron_jobid = run.jobid;
44+
45+
RETURN NEW;
46+
47+
EXCEPTION
48+
WHEN OTHERS THEN
49+
RAISE WARNING 'Lantern Async tasks: Unknown job failure in % % %', NEW, SQLERRM, SQLSTATE;
50+
PERFORM cron.unschedule(NEW.jobid);
51+
RETURN NEW;
52+
END
53+
$$ LANGUAGE plpgsql;
54+

0 commit comments

Comments
 (0)