-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20250904065521_job_setup.sql
More file actions
88 lines (75 loc) · 2.54 KB
/
Copy path20250904065521_job_setup.sql
File metadata and controls
88 lines (75 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
CREATE TABLE jobs (
id UUID PRIMARY KEY,
unique_per_type BOOLEAN NOT NULL,
job_type VARCHAR NOT NULL,
cancelled_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE UNIQUE INDEX idx_unique_job_type ON jobs (job_type) WHERE unique_per_type = TRUE;
CREATE TABLE job_events (
id UUID NOT NULL REFERENCES jobs(id),
sequence INT NOT NULL,
event_type VARCHAR NOT NULL,
event JSONB NOT NULL,
context JSONB DEFAULT NULL,
recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(id, sequence)
);
CREATE TYPE JobExecutionState AS ENUM ('pending', 'running');
CREATE TABLE job_executions (
id UUID REFERENCES jobs(id) NOT NULL UNIQUE,
job_type VARCHAR NOT NULL,
queue_id VARCHAR,
poller_instance_id UUID,
attempt_index INT NOT NULL DEFAULT 1,
state JobExecutionState NOT NULL DEFAULT 'pending',
execution_state_json JSONB,
execute_at TIMESTAMPTZ,
alive_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX idx_job_executions_poller_instance
ON job_executions(poller_instance_id)
WHERE state = 'running';
CREATE INDEX idx_job_executions_running_alive_at
ON job_executions(alive_at)
WHERE state = 'running';
CREATE INDEX idx_job_executions_pending_execute_at
ON job_executions(execute_at)
WHERE state = 'pending';
CREATE INDEX idx_job_executions_pending_job_type_execute_at
ON job_executions(job_type, execute_at)
WHERE state = 'pending';
CREATE INDEX idx_job_executions_running_queue_id
ON job_executions(queue_id)
WHERE state = 'running' AND queue_id IS NOT NULL;
CREATE OR REPLACE FUNCTION notify_job_event() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
PERFORM pg_notify('job_events',
json_build_object('type', 'execution_ready', 'job_type', NEW.job_type)::text);
RETURN NULL;
END IF;
IF TG_OP = 'UPDATE' THEN
IF NEW.execute_at IS DISTINCT FROM OLD.execute_at THEN
PERFORM pg_notify('job_events',
json_build_object('type', 'execution_ready', 'job_type', NEW.job_type)::text);
END IF;
RETURN NULL;
END IF;
IF TG_OP = 'DELETE' THEN
PERFORM pg_notify('job_events',
json_build_object('type', 'job_terminal', 'job_id', OLD.id::text)::text);
IF OLD.queue_id IS NOT NULL THEN
PERFORM pg_notify('job_events',
json_build_object('type', 'execution_ready', 'job_type', OLD.job_type)::text);
END IF;
RETURN NULL;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER job_executions_notify_event_trigger
AFTER INSERT OR UPDATE OR DELETE ON job_executions
FOR EACH ROW
EXECUTE FUNCTION notify_job_event();