bopodev-api 0.1.8 → 0.1.10

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.
@@ -1,80 +1,23 @@
1
- import { sql } from "drizzle-orm";
2
1
  import type { BopoDb } from "bopodev-db";
3
2
  import type { RealtimeHub } from "../realtime/hub";
4
3
  import { runHeartbeatSweep } from "../services/heartbeat-service";
5
4
 
6
- interface HeartbeatSchedulerOptions {
7
- companyIds?: string[];
8
- }
9
-
10
- export function createHeartbeatScheduler(db: BopoDb, realtimeHub?: RealtimeHub, options?: HeartbeatSchedulerOptions) {
5
+ export function createHeartbeatScheduler(db: BopoDb, companyId: string, realtimeHub?: RealtimeHub) {
11
6
  const intervalMs = Number(process.env.BOPO_HEARTBEAT_SWEEP_MS ?? 60_000);
12
- const warnedMissingConfiguredCompanyIds = new Set<string>();
13
7
  let running = false;
14
8
  const timer = setInterval(() => {
15
9
  if (running) {
16
10
  return;
17
11
  }
18
12
  running = true;
19
- void sweepScheduledCompanies(db, realtimeHub, options, warnedMissingConfiguredCompanyIds)
13
+ void runHeartbeatSweep(db, companyId, { realtimeHub })
14
+ .catch((error) => {
15
+ // eslint-disable-next-line no-console
16
+ console.error("[scheduler] heartbeat sweep failed", error);
17
+ })
20
18
  .finally(() => {
21
19
  running = false;
22
20
  });
23
21
  }, intervalMs);
24
22
  return () => clearInterval(timer);
25
23
  }
26
-
27
- async function sweepScheduledCompanies(
28
- db: BopoDb,
29
- realtimeHub?: RealtimeHub,
30
- options?: HeartbeatSchedulerOptions,
31
- warnedMissingConfiguredCompanyIds?: Set<string>
32
- ) {
33
- const companyIds = options?.companyIds?.length
34
- ? await resolveConfiguredCompanyIds(db, options.companyIds, warnedMissingConfiguredCompanyIds ?? new Set<string>())
35
- : await listCompanyIds(db);
36
- for (const companyId of companyIds) {
37
- try {
38
- await runHeartbeatSweep(db, companyId, { realtimeHub });
39
- } catch (error) {
40
- // eslint-disable-next-line no-console
41
- console.error(`[scheduler] heartbeat sweep failed for company '${companyId}': ${toErrorMessage(error)}`);
42
- }
43
- }
44
- }
45
-
46
- async function listCompanyIds(db: BopoDb) {
47
- const result = await db.execute(sql`
48
- SELECT id
49
- FROM companies
50
- ORDER BY created_at ASC
51
- `);
52
- return (result.rows ?? [])
53
- .map((row) => row.id)
54
- .filter((id): id is string => typeof id === "string" && id.length > 0);
55
- }
56
-
57
- async function resolveConfiguredCompanyIds(
58
- db: BopoDb,
59
- configuredCompanyIds: string[],
60
- warnedMissingConfiguredCompanyIds: Set<string>
61
- ) {
62
- const existingCompanyIds = new Set(await listCompanyIds(db));
63
- const activeCompanyIds: string[] = [];
64
- for (const companyId of configuredCompanyIds) {
65
- if (existingCompanyIds.has(companyId)) {
66
- activeCompanyIds.push(companyId);
67
- continue;
68
- }
69
- if (!warnedMissingConfiguredCompanyIds.has(companyId)) {
70
- warnedMissingConfiguredCompanyIds.add(companyId);
71
- // eslint-disable-next-line no-console
72
- console.warn(`[scheduler] skipping unknown company id '${companyId}' from BOPO_SCHEDULER_COMPANY_IDS`);
73
- }
74
- }
75
- return activeCompanyIds;
76
- }
77
-
78
- function toErrorMessage(error: unknown) {
79
- return error instanceof Error ? error.message : String(error);
80
- }