@wrenautomation/aerie 0.7.0 → 0.7.1

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.
package/dist/bin.js CHANGED
@@ -8801,7 +8801,9 @@ var init_schema2 = __esm({
8801
8801
  /** taken from the queue: where release(queue) puts it back */
8802
8802
  queued: boolean4().notNull().default(false),
8803
8803
  lane: text2(),
8804
- pin: text2()
8804
+ pin: text2(),
8805
+ /** the take's own key: a replayed take with it gets this lease back, not the next row */
8806
+ takeKey: text2("take_key")
8805
8807
  },
8806
8808
  (t) => [primaryKey({ columns: [t.ns, t.taskId, t.holder], name: "pk_coord_lease" })]
8807
8809
  );
@@ -10262,6 +10264,7 @@ var init_pglite = __esm({
10262
10264
  // src/coord/postgres.ts
10263
10265
  var postgres_exports = {};
10264
10266
  __export(postgres_exports, {
10267
+ COORD_CHANNEL: () => COORD_CHANNEL,
10265
10268
  pgCoord: () => pgCoord
10266
10269
  });
10267
10270
  async function rows2(q, query) {
@@ -10296,7 +10299,7 @@ function pgCoord(o) {
10296
10299
  if (!hit[0] || hit[0].status === name) return;
10297
10300
  await aerieStore({ db: tx }).edit(id, { status: name }, by);
10298
10301
  };
10299
- const lease = async (tx, id, holder, leaseMs, queued) => {
10302
+ const lease = async (tx, id, holder, leaseMs, queued, key) => {
10300
10303
  const t = clock();
10301
10304
  const others = await rows2(
10302
10305
  tx,
@@ -10309,9 +10312,10 @@ function pgCoord(o) {
10309
10312
  );
10310
10313
  const gen = num(g?.gen);
10311
10314
  await tx.execute(sql`
10312
- insert into ${L} (ns, task_id, holder, gen, expires, queued, lane, pin)
10313
- values (${ns}, ${id}, ${holder}, ${gen}, ${plus(t, leaseMs)}, ${!!queued}, ${queued?.lane ?? null}, ${queued?.pin ?? null})
10315
+ insert into ${L} (ns, task_id, holder, gen, expires, queued, lane, pin, take_key)
10316
+ values (${ns}, ${id}, ${holder}, ${gen}, ${plus(t, leaseMs)}, ${!!queued}, ${queued?.lane ?? null}, ${queued?.pin ?? null}, ${key ?? null})
10314
10317
  on conflict (ns, task_id, holder) do update set gen = excluded.gen, expires = excluded.expires,
10318
+ take_key = coalesce(excluded.take_key, ${L}.take_key),
10315
10319
  queued = excluded.queued or ${L}.queued,
10316
10320
  lane = case when excluded.queued then excluded.lane else ${L}.lane end,
10317
10321
  pin = case when excluded.queued then excluded.pin else ${L}.pin end`);
@@ -10363,7 +10367,9 @@ function pgCoord(o) {
10363
10367
  };
10364
10368
  const queueRows = (q) => rows2(
10365
10369
  q,
10366
- sql`select task_id as id, lane, pin, pos from ${Q} where ns = ${ns} order by pos, task_id`
10370
+ sql`select q.task_id as id, q.lane, q.pin, q.pos, k.title from ${Q} q
10371
+ left join ${task} k on k.id = upper(q.task_id)
10372
+ where q.ns = ${ns} order by q.pos, q.task_id`
10367
10373
  );
10368
10374
  const store = {
10369
10375
  name: "aerie",
@@ -10498,10 +10504,24 @@ function pgCoord(o) {
10498
10504
  await tx.execute(sql`delete from ${Q} where ns = ${ns} and task_id = any(${textArr(ids)})`);
10499
10505
  }),
10500
10506
  queueList: async () => (await queueRows(db)).map(
10501
- (i, pos) => ({ id: i.id, lane: i.lane, pin: i.pin, pos })
10507
+ (i, pos) => ({
10508
+ id: i.id,
10509
+ lane: i.lane,
10510
+ pin: i.pin,
10511
+ pos,
10512
+ ...i.title ? { title: i.title } : {}
10513
+ })
10502
10514
  ),
10503
10515
  queueTake: (x, holder, leaseMs) => atomic(db, async (tx) => {
10504
10516
  await lockQueue(tx);
10517
+ if (x.key) {
10518
+ const [had] = await rows2(
10519
+ tx,
10520
+ sql`select task_id as id, gen::float8 as gen from ${L} where ns = ${ns} and holder = ${holder}
10521
+ and take_key = ${x.key} and expires >= ${clock()}`
10522
+ );
10523
+ if (had) return { id: had.id, gen: num(had.gen) };
10524
+ }
10505
10525
  if (x.pin?.startsWith("run:")) {
10506
10526
  const t = clock();
10507
10527
  const lapsed = await rows2(
@@ -10544,7 +10564,14 @@ function pgCoord(o) {
10544
10564
  if (!item) return null;
10545
10565
  await lockTasks(tx, [item.id]);
10546
10566
  await tx.execute(sql`delete from ${Q} where ns = ${ns} and task_id = ${item.id}`);
10547
- const r = await lease(tx, item.id, holder, leaseMs, { lane: item.lane, pin: item.pin });
10567
+ const r = await lease(
10568
+ tx,
10569
+ item.id,
10570
+ holder,
10571
+ leaseMs,
10572
+ { lane: item.lane, pin: item.pin },
10573
+ x.key
10574
+ );
10548
10575
  return { id: item.id, gen: r.gen };
10549
10576
  }),
10550
10577
  fence: async (id, holder, gen) => (await rows2(
@@ -10556,9 +10583,18 @@ function pgCoord(o) {
10556
10583
  await o.end?.();
10557
10584
  }
10558
10585
  };
10586
+ const sub = o.subscribe;
10587
+ if (sub)
10588
+ store.listen = async (onChange) => {
10589
+ const stop = await sub(COORD_CHANNEL, (payload) => {
10590
+ const [what, ...rest] = payload.split(":");
10591
+ if (rest.join(":") === ns && (what === "queue" || what === "lease")) onChange(what);
10592
+ });
10593
+ return { end: stop };
10594
+ };
10559
10595
  return store;
10560
10596
  }
10561
- var ms, at2, num, textArr;
10597
+ var COORD_CHANNEL, ms, at2, num, textArr;
10562
10598
  var init_postgres = __esm({
10563
10599
  "src/coord/postgres.ts"() {
10564
10600
  "use strict";
@@ -10566,6 +10602,7 @@ var init_postgres = __esm({
10566
10602
  init_db2();
10567
10603
  init_schema2();
10568
10604
  init_store();
10605
+ COORD_CHANNEL = "aerie_coord";
10569
10606
  ms = (v) => sql`(extract(epoch from ${v}) * 1000)::float8`;
10570
10607
  at2 = (v) => sql`to_timestamp(${v}::float8 / 1000)`;
10571
10608
  num = (v) => Math.round(Number(v));