Skip to content

Commit aaf8e86

Browse files
committed
add agent and workspace insert notifs
1 parent db4f308 commit aaf8e86

File tree

5 files changed

+136
-5
lines changed

5 files changed

+136
-5
lines changed

coderd/database/dump.sql

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000260_agent_id_name_pair.down.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

coderd/database/migrations/000260_agent_id_name_pair.up.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DROP TYPE agent_id_name_pair;
2+
3+
DROP TRIGGER IF EXISTS new_workspace_notify ON workspaces;
4+
DROP FUNCTION IF EXISTS new_workspace_notify;
5+
6+
DROP TRIGGER IF EXISTS new_agent_notify ON workspace_agents;
7+
DROP FUNCTION new_agent_notify;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
CREATE TYPE agent_id_name_pair AS (
2+
id uuid,
3+
name text
4+
);
5+
6+
CREATE FUNCTION new_workspace_notify() RETURNS trigger
7+
LANGUAGE plpgsql
8+
AS $$
9+
DECLARE
10+
BEGIN
11+
-- Write to the notification channel `new_workspace:owner_id`
12+
-- with the workspace id as the payload.
13+
PERFORM pg_notify('new_workspace:' || NEW.owner_id, NEW.id);
14+
RETURN NEW;
15+
END;
16+
$$;
17+
18+
CREATE TRIGGER new_workspace_notify
19+
AFTER INSERT ON workspaces
20+
FOR EACH ROW
21+
EXECUTE FUNCTION new_workspace_notify();
22+
23+
24+
CREATE FUNCTION new_agent_notify() RETURNS trigger
25+
LANGUAGE plpgsql
26+
AS $$
27+
DECLARE
28+
workspace_owner_id uuid;
29+
BEGIN
30+
SELECT workspaces.owner_id
31+
INTO workspace_owner_id
32+
FROM
33+
workspaces
34+
WHERE
35+
workspaces.id = (
36+
SELECT
37+
workspace_id
38+
FROM
39+
workspace_builds
40+
WHERE
41+
workspace_builds.job_id = (
42+
SELECT
43+
job_id
44+
FROM
45+
workspace_resources
46+
WHERE
47+
workspace_resources.id = (
48+
SELECT
49+
resource_id
50+
FROM
51+
workspace_agents
52+
WHERE
53+
workspace_agents.id = NEW.id
54+
)
55+
)
56+
);
57+
-- Agents might not belong to a workspace (template imports)
58+
IF workspace_owner_id IS NOT NULL THEN
59+
-- Write to the notification channel `new_agent:workspace_owner_id`
60+
PERFORM pg_notify('new_agent:' || workspace_owner_id, '');
61+
END IF;
62+
RETURN NEW;
63+
END;
64+
$$;
65+
66+
67+
CREATE TRIGGER new_agent_notify
68+
AFTER INSERT ON workspace_agents
69+
FOR EACH ROW
70+
EXECUTE FUNCTION new_agent_notify();
71+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy