Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/project/project-status.ts
1452 views
1
/*
2
Broadcast project status whenever it updates.
3
*/
4
5
import { projectSubject } from "@cocalc/conat/names";
6
import { conat } from "@cocalc/conat/client";
7
import { getLogger } from "@cocalc/conat/client";
8
import { type Subscription } from "@cocalc/conat/core/client";
9
10
const SERVICE_NAME = "project-status";
11
const logger = getLogger("project:project-status");
12
13
function getSubject({ project_id, compute_server_id }) {
14
return projectSubject({
15
project_id,
16
compute_server_id,
17
service: SERVICE_NAME,
18
});
19
}
20
21
// publishes status updates when they are emitted.
22
export async function createPublisher({
23
project_id,
24
compute_server_id,
25
projectStatusServer,
26
}) {
27
const client = await conat();
28
const subject = getSubject({ project_id, compute_server_id });
29
logger.debug("publishing status updates on ", { subject });
30
projectStatusServer.on("status", (status) => {
31
logger.debug("publishing updated status", status);
32
client.publishSync(subject, status);
33
});
34
}
35
36
// async iterator over the status updates:
37
export async function get({
38
project_id,
39
compute_server_id,
40
}): Promise<Subscription> {
41
const client = await conat();
42
const subject = getSubject({ project_id, compute_server_id });
43
return await client.subscribe(subject);
44
}
45
46