tangled
alpha
login
or
join now
openstatus.dev
/
openstatus
5
fork
atom
Openstatus
www.openstatus.dev
5
fork
atom
overview
issues
pulls
pipelines
๐ improve logging
Thibault Le Ouay
2 years ago
2bb21a77
88232117
+10
-15
1 changed file
expand all
collapse all
unified
split
apps
web
src
app
api
checker
cron
_cron.ts
+10
-15
apps/web/src/app/api/checker/cron/_cron.ts
···
45
45
periodicity,
46
46
);
47
47
48
48
-
console.log(`Start cron for ${periodicity}`);
49
48
const timestamp = Date.now();
50
49
51
51
-
// const ctx = createTRPCContext({ req, serverSideCall: true });
52
52
-
// // FIXME: we should the proper type
53
53
-
// ctx.auth = { userId: "cron" } as any;
54
54
-
// const caller = edgeRouter.createCaller(ctx);
55
55
-
56
56
-
// const monitors = await caller.monitor.getMonitorsForPeriodicity({
57
57
-
// periodicity: periodicity,
58
58
-
// });
59
50
const result = await db
60
51
.select()
61
52
.from(monitor)
62
53
.where(and(eq(monitor.periodicity, periodicity), eq(monitor.active, true)))
63
54
.all();
55
55
+
console.log(`Start cron for ${periodicity}`);
56
56
+
64
57
const monitors = z.array(selectMonitorSchema).parse(result);
65
58
const allResult = [];
66
59
···
73
66
.where(eq(monitorStatusTable.monitorId, row.id))
74
67
.all();
75
68
const monitorStatus = z.array(selectMonitorStatusSchema).parse(result);
76
76
-
// const monitorStatus = await caller.monitor.getMonitorStatusByMonitorId({
77
77
-
// monitorId: row.id,
78
78
-
// });
79
69
80
70
for (const region of selectedRegions) {
81
71
const status =
···
90
80
});
91
81
allResult.push(response);
92
82
if (periodicity === "30s") {
93
93
-
console.log("30s cron for", row.id, region, status);
94
83
// we schedule another task in 30s
95
84
const scheduledAt = timestamp + 30 * 1000;
96
85
const response = createCronTask({
···
105
94
}
106
95
}
107
96
}
108
108
-
await Promise.allSettled(allResult);
109
97
110
110
-
console.log(`End cron for ${periodicity} with ${allResult.length} jobs`);
98
98
+
const allRequests = await Promise.allSettled(allResult);
99
99
+
100
100
+
const success = allRequests.filter((r) => r.status === "fulfilled").length;
101
101
+
const failed = allRequests.filter((r) => r.status === "rejected").length;
102
102
+
103
103
+
console.log(
104
104
+
`End cron for ${periodicity} with ${allResult.length} jobs with ${success} success and ${failed} failed`,
105
105
+
);
111
106
};
112
107
// timestamp needs to be in ms
113
108
const createCronTask = async ({