Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 1 | CREATE TABLE machines ( |
| 2 | machine_id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, |
| 3 | machine_created_at TIMESTAMPTZ NOT NULL |
| 4 | ); |
| 5 | |
| 6 | |
| 7 | -- Sessions are maintained by components as they work on the rest of the machine |
| 8 | -- database. Once a session is created, it must be maintained by its owning |
| 9 | -- component by repeatedly 'poking' it, ie. updating the heartbeat_deadline |
| 10 | -- value to be some point in the future. |
| 11 | -- |
| 12 | -- TODO: garbage collect old sessions. |
| 13 | CREATE TABLE sessions ( |
| 14 | session_id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, |
| 15 | -- Name of component which created this session. Human-readable. |
| 16 | session_component_name STRING NOT NULL, |
| 17 | -- Node name, hostname:port, pod name, whatever. Something to tell where |
| 18 | -- a particular component is running. Human-readable. |
| 19 | session_runtime_info STRING NOT NULL, |
| 20 | -- Time at which this session was created. |
| 21 | session_created_at TIMESTAMPTZ NOT NULL, |
| 22 | -- Number of seconds by which session_deadline (counting from now()) |
| 23 | -- is bumped up every time the session is poked. |
| 24 | session_interval_seconds INT NOT NULL, |
| 25 | -- Deadline after which this session should not be considered valid anymore. |
| 26 | session_deadline TIMESTAMPTZ NOT NULL |
| 27 | ); |
| 28 | |
| 29 | CREATE TYPE process AS ENUM ( |
| 30 | -- Reserved for unit tests. |
| 31 | 'UnitTest1', |
| 32 | 'UnitTest2' |
| 33 | ); |
| 34 | |
| 35 | -- Work items map a session to work performed on a machine. Multiple work items |
| 36 | -- can exist per session, and thus, a session can back multiple items of work |
| 37 | -- acting on multiple machines. These are optionally created by components to |
| 38 | -- indicate some long-running process being performed on a machine, and will |
| 39 | -- lock out the same process from being run simultaneously, eg. in a |
| 40 | -- concurrently running instance of the same component. |
| 41 | CREATE TABLE work ( |
| 42 | -- Machine that this work is being performed on. Prevent deleting machines |
| 43 | -- that have active work on them. |
| 44 | machine_id UUID NOT NULL REFERENCES machines(machine_id) ON DELETE RESTRICT, |
| 45 | -- Session that this work item is tied to. If the session expires, so does |
| 46 | -- the work item. |
| 47 | session_id UUID NOT NULL REFERENCES sessions(session_id) ON DELETE CASCADE, |
| 48 | -- Human-readable process name. |
| 49 | process process NOT NULL, |
| 50 | UNIQUE (machine_id, process), |
| 51 | CONSTRAINT "primary" PRIMARY KEY (machine_id, session_id, process) |
Serge Bazanski | 68ca370 | 2022-11-02 17:30:44 +0100 | [diff] [blame^] | 52 | ); |