File size: 6,256 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { randomUUID } from "crypto";
import { logger as _logger } from "../../lib/logger";
import { addMonitorCheckJob } from "./queue";
import {
  advanceMonitorAfterSkippedCheck,
  claimDueMonitors,
  createMonitorCheck,
  deferMonitorClaim,
  dispatchScheduledMonitorCheck,
  getMonitorCheck,
  updateMonitorCheck,
  updateMonitorScheduleAfterRun,
} from "./store";
import { autumnService } from "../autumn/autumn.service";
import { isMonitorCheckStale, MONITOR_CHECK_STALE_ERROR } from "./stale";
import { validateMonitorCron } from "./cron";
import { monitorJitterOffsetMs } from "./jitter";
import type { MonitorRow } from "./types";

const logger = _logger.child({ module: "monitoring-scheduler" });

export async function enqueueMonitorCheck(params: {
  monitorId: string;
  checkId: string;
  teamId: string;
}): Promise<void> {
  await addMonitorCheckJob(params);
}

export async function enqueueDueMonitorChecks(
  params: {
    workerId?: string;
    limit?: number;
    leaseSeconds?: number;
  } = {},
): Promise<number> {
  const workerId = params.workerId ?? `monitor-scheduler-${randomUUID()}`;
  const monitors = await claimDueMonitors({
    workerId,
    limit: params.limit ?? 10,
    leaseSeconds: params.leaseSeconds ?? 60,
  });

  let enqueued = 0;
  for (let monitor of monitors) {
    let check: Awaited<ReturnType<typeof createMonitorCheck>> | null = null;
    let dispatched = false;
    try {
      if (await deferForJitter(monitor)) continue;

      if (monitor.current_check_id) {
        const cleared = await clearFinishedOrStaleCurrentCheck(monitor);
        if (cleared) {
          monitor = { ...monitor, current_check_id: null };
        }
      }

      if (monitor.current_check_id) {
        const skipped = await createMonitorCheck({
          monitor,
          trigger: "scheduled",
          scheduledFor: monitor.next_run_at,
          status: "skipped_overlap",
        });
        const finished = await updateMonitorCheck(skipped.id, {
          status: "skipped_overlap",
          finished_at: new Date().toISOString(),
          error: "Previous monitor check is still running.",
        });
        await advanceMonitorAfterSkippedCheck({ monitor, check: finished });
        continue;
      }

      check = await createMonitorCheck({
        monitor,
        trigger: "scheduled",
        scheduledFor: monitor.next_run_at,
      });
      dispatched = await dispatchScheduledMonitorCheck({
        monitor,
        checkId: check.id,
      });
      if (!dispatched) {
        check = await updateMonitorCheck(check.id, {
          status: "skipped_overlap",
          finished_at: new Date().toISOString(),
          error: "Previous monitor check is still running.",
        });
        await advanceMonitorAfterSkippedCheck({ monitor, check });
        continue;
      }

      await enqueueMonitorCheck({
        monitorId: monitor.id,
        checkId: check.id,
        teamId: monitor.team_id,
      });
      enqueued++;
    } catch (error) {
      if (check) {
        const failed = await updateMonitorCheck(check.id, {
          status: "failed",
          finished_at: new Date().toISOString(),
          error: error instanceof Error ? error.message : String(error),
        }).catch(updateError => {
          logger.error("Failed to mark monitor check enqueue failure", {
            updateError,
            error,
            monitorId: monitor.id,
            checkId: check?.id,
            teamId: monitor.team_id,
          });
          return null;
        });

        if (failed && dispatched) {
          await updateMonitorScheduleAfterRun({
            monitor,
            check: failed,
          }).catch(updateError => {
            logger.error("Failed to clear failed dispatched monitor check", {
              updateError,
              error,
              monitorId: monitor.id,
              checkId: failed.id,
              teamId: monitor.team_id,
            });
          });
        }
      }

      logger.error("Failed to enqueue due monitor check", {
        error,
        monitorId: monitor.id,
        teamId: monitor.team_id,
      });
    }
  }

  return enqueued;
}

async function deferForJitter(monitor: MonitorRow): Promise<boolean> {
  if (!monitor.next_run_at) return false;
  const { intervalMs } = validateMonitorCron(
    monitor.schedule_cron,
    monitor.schedule_timezone,
  );
  const dueAt =
    new Date(monitor.next_run_at).getTime() +
    monitorJitterOffsetMs(monitor.id, intervalMs);
  if (Date.now() >= dueAt) return false;
  try {
    await deferMonitorClaim(monitor.id, new Date(dueAt));
  } catch (error) {
    logger.warn("Failed to defer monitor claim for jitter", {
      error,
      monitorId: monitor.id,
    });
    return false;
  }
  return true;
}

async function clearFinishedOrStaleCurrentCheck(
  monitor: MonitorRow,
): Promise<boolean> {
  if (!monitor.current_check_id) return true;

  const current = await getMonitorCheck(
    monitor.team_id,
    monitor.id,
    monitor.current_check_id,
  );
  if (!current) return false;

  if (current.status === "running" || current.status === "queued") {
    if (!isMonitorCheckStale(current)) return false;

    if (current.autumn_lock_id) {
      await autumnService
        .finalizeCreditsLock({
          lockId: current.autumn_lock_id,
          action: "release",
          properties: {
            source: "monitorCheck",
            endpoint: "monitor",
            jobId: current.id,
          },
        })
        .catch(error => {
          logger.warn("Failed to release stale monitor check credit lock", {
            error,
            monitorId: monitor.id,
            checkId: current.id,
            lockId: current.autumn_lock_id,
          });
        });
    }

    const failed = await updateMonitorCheck(current.id, {
      status: "failed",
      finished_at: new Date().toISOString(),
      actual_credits: 0,
      billing_status: current.autumn_lock_id ? "released" : "not_applicable",
      error: MONITOR_CHECK_STALE_ERROR,
    });
    await updateMonitorScheduleAfterRun({
      monitor,
      check: failed,
    });
    return true;
  }

  await updateMonitorScheduleAfterRun({
    monitor,
    check: current,
  });
  return true;
}