@catamorphic/work-server 0.1.0-alpha.31 → 0.1.0-alpha.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,14 +4,13 @@ import type { FastifyInstance } from "fastify";
4
4
  export interface InstallTarget {
5
5
  /** The control plane's public origin the worker dials. */
6
6
  controlPlaneUrl: string;
7
- /** The worker image, normally this server's own release. */
7
+ /** The deployment's explicitly selected worker image. */
8
8
  image: string;
9
9
  }
10
10
 
11
11
  /**
12
12
  * The install target, or why machines cannot install from this server: a
13
- * worker image is needed (the published image's own, or
14
- * `WORK_WORKER_IMAGE`), and a public origin other machines reach.
13
+ * deployment-owned worker image is needed (`WORK_WORKER_IMAGE`), and a public origin other machines reach.
15
14
  */
16
15
  export function installTarget(args: {
17
16
  publicBase: string;
@@ -20,7 +19,7 @@ export function installTarget(args: {
20
19
  if (!args.workerImage)
21
20
  return {
22
21
  unavailable:
23
- "This server does not know which worker image machines should run. Set WORK_WORKER_IMAGE to the Work server image of this release (for example ghcr.io/<owner>/work-server:<version>).",
22
+ "This server does not know which worker image machines should run. Set WORK_WORKER_IMAGE to your deployment's pinned worker image (see @catamorphic/work-server deployment guidance).",
24
23
  };
25
24
  if (isLoopback(args.publicBase))
26
25
  return {
@@ -91,7 +90,7 @@ usage() {
91
90
  Usage: install.sh --code <code> [--image <image>] [--data-dir <dir>] [--name <name>] [--sandbox <backend>]
92
91
 
93
92
  --code <code> the one-time enrollment code from the Work server (required)
94
- --image <image> the worker image (default: this server's release)
93
+ --image <image> the worker image (default: the deployment's configured worker image)
95
94
  --data-dir <dir> where the worker keeps its credential, sandboxes and
96
95
  members' sign-ins (default: ${DEFAULT_WORKER_DATA_DIR}): a new
97
96
  directory, or one under /var/lib, /srv, /opt, /data or
@@ -1440,18 +1440,7 @@ export class MachineReconciler {
1440
1440
  ])
1441
1441
  .where("placement_waits.tenant_id", "=", this.deps.tenantId)
1442
1442
  .where("placement_waits.last_tried_at", ">", waitFresh())
1443
- .where(({ exists, selectFrom }) =>
1444
- exists(
1445
- selectFrom("agent_turns")
1446
- .select("agent_turns.id")
1447
- .whereRef(
1448
- "agent_turns.session_id",
1449
- "=",
1450
- "placement_waits.root_workload_id",
1451
- )
1452
- .where("agent_turns.status", "=", "queued"),
1453
- ),
1454
- )
1443
+ .where(queuedPlacementWait(this.deps.db))
1455
1444
  .orderBy("placement_waits.waiting_since")
1456
1445
  .execute(),
1457
1446
  this.deps.workers.machines(),
@@ -1557,18 +1546,7 @@ export class MachineReconciler {
1557
1546
  )
1558
1547
  .where("tenant_id", "=", this.deps.tenantId)
1559
1548
  .where("last_tried_at", ">", waitFresh())
1560
- .where(({ exists, selectFrom }) =>
1561
- exists(
1562
- selectFrom("agent_turns")
1563
- .select("agent_turns.id")
1564
- .whereRef(
1565
- "agent_turns.session_id",
1566
- "=",
1567
- "placement_waits.root_workload_id",
1568
- )
1569
- .where("agent_turns.status", "=", "queued"),
1570
- ),
1571
- )
1549
+ .where(queuedPlacementWait(this.deps.db))
1572
1550
  .as("waits"),
1573
1551
  eb
1574
1552
  .selectFrom("execution_allocations")
@@ -1948,3 +1926,29 @@ function canonical(value: unknown): string {
1948
1926
  : entry,
1949
1927
  );
1950
1928
  }
1929
+
1930
+ /** Both kinds of work reuse placement demand; waiting journals alone are no demand. */
1931
+ function queuedPlacementWait(db: Kysely<DB>) {
1932
+ const turns = db
1933
+ .selectFrom("agent_turns")
1934
+ .select("id")
1935
+ .where("session_id", "=", sql<string>`placement_waits.root_workload_id`)
1936
+ .where("status", "=", "queued");
1937
+ const jobs = db
1938
+ .selectFrom("workflow_runs as root")
1939
+ .innerJoin(
1940
+ "workflow_runs as run",
1941
+ "run.allocation_id",
1942
+ "root.allocation_id",
1943
+ )
1944
+ .innerJoin("execution_jobs as job", "job.workflow_run_id", "run.id")
1945
+ .select("job.id")
1946
+ .where("root.id", "=", sql<string>`placement_waits.root_workload_id`)
1947
+ .where("root.status", "in", ["pending", "running", "waiting"])
1948
+ .where("run.status", "in", ["pending", "running", "waiting"])
1949
+ .where("job.status", "=", "pending")
1950
+ .where("job.kind", "!=", "durable_pause_timeout")
1951
+ .where("job.available_at", "<=", sql<Date>`now()`);
1952
+ return sql<boolean>`((placement_waits.workload_kind = 'agent' AND EXISTS (${turns})) OR
1953
+ (placement_waits.workload_kind = 'workflow' AND EXISTS (${jobs})))`;
1954
+ }