@geekmidas/constructs 9.2.1 → 9.4.0
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/CHANGELOG.md +73 -0
- package/README.md +1 -1
- package/dist/{AWSScheduledFunction-CU7mMGFA.mjs → AWSScheduledFunction-BXmRm9W6.mjs} +4 -3
- package/dist/{AWSScheduledFunction-CU7mMGFA.mjs.map → AWSScheduledFunction-BXmRm9W6.mjs.map} +1 -1
- package/dist/{AWSScheduledFunction-CKhVurOa.cjs → AWSScheduledFunction-C-JBSNak.cjs} +6 -5
- package/dist/{AWSScheduledFunction-CKhVurOa.cjs.map → AWSScheduledFunction-C-JBSNak.cjs.map} +1 -1
- package/dist/{AWSScheduledFunction-a6hRp3nk.d.mts → AWSScheduledFunction-DgQuL5kt.d.mts} +49 -11
- package/dist/AWSScheduledFunction-DgQuL5kt.d.mts.map +1 -0
- package/dist/{AWSScheduledFunction-DPMzOjZP.d.cts → AWSScheduledFunction-DoDZR8E6.d.cts} +49 -11
- package/dist/AWSScheduledFunction-DoDZR8E6.d.cts.map +1 -0
- package/dist/{Construct-D_3yZ9m5.d.cts → Construct-BEh2DSMe.d.cts} +7 -13
- package/dist/Construct-BEh2DSMe.d.cts.map +1 -0
- package/dist/{Construct-D09WUUOf.cjs → Construct-Cf_0pAvo.cjs} +2 -10
- package/dist/Construct-Cf_0pAvo.cjs.map +1 -0
- package/dist/{Construct-DsqpiAym.d.mts → Construct-CgluLUuF.d.mts} +7 -13
- package/dist/Construct-CgluLUuF.d.mts.map +1 -0
- package/dist/{Construct-D0uZvbr3.mjs → Construct-D5LZBFhb.mjs} +3 -11
- package/dist/Construct-D5LZBFhb.mjs.map +1 -0
- package/dist/{Endpoint-nhjtdDL3.d.cts → Endpoint-1u_i-nCT.d.cts} +2 -2
- package/dist/{Endpoint-nhjtdDL3.d.cts.map → Endpoint-1u_i-nCT.d.cts.map} +1 -1
- package/dist/{Endpoint-DEydLuBs.d.mts → Endpoint-CypDvYJW.d.mts} +3 -3
- package/dist/{Endpoint-DEydLuBs.d.mts.map → Endpoint-CypDvYJW.d.mts.map} +1 -1
- package/dist/{HonoEndpointAdaptor-CJfFQ5KR.mjs → HonoEndpointAdaptor-C-mPVfwk.mjs} +11 -18
- package/dist/HonoEndpointAdaptor-C-mPVfwk.mjs.map +1 -0
- package/dist/{HonoEndpointAdaptor-DCD0rhFg.cjs → HonoEndpointAdaptor-D_2UDPNs.cjs} +12 -19
- package/dist/HonoEndpointAdaptor-D_2UDPNs.cjs.map +1 -0
- package/dist/{Queue-Brw--scg.d.cts → Queue-Bl87Atny.d.cts} +2 -2
- package/dist/{Queue-Brw--scg.d.cts.map → Queue-Bl87Atny.d.cts.map} +1 -1
- package/dist/{Queue-D5z1bwFu.d.mts → Queue-Dhl8bc45.d.mts} +2 -2
- package/dist/{Queue-D5z1bwFu.d.mts.map → Queue-Dhl8bc45.d.mts.map} +1 -1
- package/dist/{Subscriber-DvKSp0N4.d.cts → Subscriber-YwPpHrBc.d.cts} +2 -2
- package/dist/{Subscriber-DvKSp0N4.d.cts.map → Subscriber-YwPpHrBc.d.cts.map} +1 -1
- package/dist/{Subscriber-CJ22tp8r.d.mts → Subscriber-panMf5vp.d.mts} +2 -2
- package/dist/{Subscriber-CJ22tp8r.d.mts.map → Subscriber-panMf5vp.d.mts.map} +1 -1
- package/dist/{Topic-BMfrIFdX.d.cts → Topic-DGtlTlu5.d.mts} +3 -3
- package/dist/{Topic-B-fxOsR2.d.mts.map → Topic-DGtlTlu5.d.mts.map} +1 -1
- package/dist/{Topic-B-fxOsR2.d.mts → Topic-dIcPqSLa.d.cts} +3 -3
- package/dist/{Topic-BMfrIFdX.d.cts.map → Topic-dIcPqSLa.d.cts.map} +1 -1
- package/dist/adaptors/aws.cjs +9 -14
- package/dist/adaptors/aws.cjs.map +1 -1
- package/dist/adaptors/aws.d.cts +6 -6
- package/dist/adaptors/aws.d.cts.map +1 -1
- package/dist/adaptors/aws.d.mts +8 -8
- package/dist/adaptors/aws.d.mts.map +1 -1
- package/dist/adaptors/aws.mjs +9 -14
- package/dist/adaptors/aws.mjs.map +1 -1
- package/dist/adaptors/hono.cjs +7 -7
- package/dist/adaptors/hono.d.cts +3 -3
- package/dist/adaptors/hono.d.cts.map +1 -1
- package/dist/adaptors/hono.d.mts +4 -4
- package/dist/adaptors/hono.d.mts.map +1 -1
- package/dist/adaptors/hono.mjs +7 -7
- package/dist/adaptors/testing.cjs +22 -29
- package/dist/adaptors/testing.cjs.map +1 -1
- package/dist/adaptors/testing.d.cts +5 -5
- package/dist/adaptors/testing.d.cts.map +1 -1
- package/dist/adaptors/testing.d.mts +6 -6
- package/dist/adaptors/testing.d.mts.map +1 -1
- package/dist/adaptors/testing.mjs +22 -29
- package/dist/adaptors/testing.mjs.map +1 -1
- package/dist/{clone-I_WIpiFf.cjs → clone-BQnYiRgm.cjs} +1 -1
- package/dist/{clone-I_WIpiFf.cjs.map → clone-BQnYiRgm.cjs.map} +1 -1
- package/dist/{clone-pdXxc0t3.mjs → clone-BWIwx6vL.mjs} +1 -1
- package/dist/{clone-pdXxc0t3.mjs.map → clone-BWIwx6vL.mjs.map} +1 -1
- package/dist/crons/index.cjs +31 -6
- package/dist/crons/index.cjs.map +1 -1
- package/dist/crons/index.d.cts +23 -9
- package/dist/crons/index.d.cts.map +1 -1
- package/dist/crons/index.d.mts +19 -5
- package/dist/crons/index.d.mts.map +1 -1
- package/dist/crons/index.mjs +31 -6
- package/dist/crons/index.mjs.map +1 -1
- package/dist/endpoints/index.cjs +7 -32
- package/dist/endpoints/index.cjs.map +1 -1
- package/dist/endpoints/index.d.cts +4 -12
- package/dist/endpoints/index.d.cts.map +1 -1
- package/dist/endpoints/index.d.mts +5 -13
- package/dist/endpoints/index.d.mts.map +1 -1
- package/dist/endpoints/index.mjs +7 -32
- package/dist/endpoints/index.mjs.map +1 -1
- package/dist/functions/index.cjs +3 -3
- package/dist/functions/index.d.cts +2 -2
- package/dist/functions/index.d.mts +2 -2
- package/dist/functions/index.mjs +3 -3
- package/dist/{functions-DhkDodl0.mjs → functions-CNJbRq0O.mjs} +4 -89
- package/dist/functions-CNJbRq0O.mjs.map +1 -0
- package/dist/{functions-B3OFv3QV.cjs → functions-DzjxHo1V.cjs} +3 -100
- package/dist/functions-DzjxHo1V.cjs.map +1 -0
- package/dist/{index-B8LNLQXD.d.mts → index-BUNi5ldW.d.mts} +2 -8
- package/dist/index-BUNi5ldW.d.mts.map +1 -0
- package/dist/{index-CXpim5mV.d.cts → index-zeG5G432.d.cts} +2 -8
- package/dist/index-zeG5G432.d.cts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/{lazyAccessors-C1n1QfEj.cjs → lazyAccessors-BZJl6bx_.cjs} +3 -3
- package/dist/{lazyAccessors-C1n1QfEj.cjs.map → lazyAccessors-BZJl6bx_.cjs.map} +1 -1
- package/dist/{lazyAccessors-fFIQrblM.mjs → lazyAccessors-BjAZqRqK.mjs} +3 -3
- package/dist/{lazyAccessors-fFIQrblM.mjs.map → lazyAccessors-BjAZqRqK.mjs.map} +1 -1
- package/dist/monitor-D3uHJ8DU.cjs +67 -0
- package/dist/monitor-D3uHJ8DU.cjs.map +1 -0
- package/dist/monitor-Dt7fiasS.mjs +61 -0
- package/dist/monitor-Dt7fiasS.mjs.map +1 -0
- package/dist/monitors/index.cjs +3 -3
- package/dist/monitors/index.cjs.map +1 -1
- package/dist/monitors/index.d.cts +3 -3
- package/dist/monitors/index.d.cts.map +1 -1
- package/dist/monitors/index.d.mts +3 -3
- package/dist/monitors/index.mjs +3 -3
- package/dist/monitors/index.mjs.map +1 -1
- package/dist/{processAudits-CwyaaSQc.mjs → processAudits-BEiSkgH3.mjs} +1 -1
- package/dist/{processAudits-CwyaaSQc.mjs.map → processAudits-BEiSkgH3.mjs.map} +1 -1
- package/dist/{processAudits-94Ar_EJF.cjs → processAudits-Cd912Eck.cjs} +2 -2
- package/dist/{processAudits-94Ar_EJF.cjs.map → processAudits-Cd912Eck.cjs.map} +1 -1
- package/dist/{publisher-BZgcrVPy.cjs → publisher-B6Wu-JNv.cjs} +1 -1
- package/dist/{publisher-BZgcrVPy.cjs.map → publisher-B6Wu-JNv.cjs.map} +1 -1
- package/dist/{publisher-CnpeMv-r.mjs → publisher-Cs7WcvJl.mjs} +1 -1
- package/dist/{publisher-CnpeMv-r.mjs.map → publisher-Cs7WcvJl.mjs.map} +1 -1
- package/dist/queue/index.cjs +2 -2
- package/dist/queue/index.d.cts +4 -4
- package/dist/queue/index.d.mts +2 -2
- package/dist/queue/index.mjs +2 -2
- package/dist/subscribers/index.cjs +2 -2
- package/dist/subscribers/index.d.cts +5 -5
- package/dist/subscribers/index.d.cts.map +1 -1
- package/dist/subscribers/index.d.mts +3 -3
- package/dist/subscribers/index.mjs +2 -2
- package/dist/{telemetry-C--EgYVr.d.mts → telemetry-Bp4DLvdQ.d.mts} +1 -1
- package/dist/{telemetry-C--EgYVr.d.mts.map → telemetry-Bp4DLvdQ.d.mts.map} +1 -1
- package/dist/topic/index.cjs +2 -2
- package/dist/topic/index.d.cts +2 -2
- package/dist/topic/index.d.mts +2 -2
- package/dist/topic/index.mjs +2 -2
- package/dist/{types-DNngH0fX.d.mts → types-j0ti0JTY.d.mts} +1 -1
- package/dist/{types-DNngH0fX.d.mts.map → types-j0ti0JTY.d.mts.map} +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +22 -22
- package/src/Construct.ts +2 -13
- package/src/__tests__/Construct.environment.spec.ts +2 -2
- package/src/__tests__/builder-reuse.spec.ts +2 -2
- package/src/__tests__/uptime.spec.ts +340 -0
- package/src/crons/Cron.ts +197 -60
- package/src/crons/CronBuilder.ts +25 -5
- package/src/crons/__tests__/AWSScheduledFunction.spec.ts +8 -6
- package/src/crons/__tests__/Cron.spec.ts +53 -91
- package/src/crons/__tests__/CronBuilder.state-isolation.spec.ts +28 -28
- package/src/crons/index.ts +2 -1
- package/src/endpoints/AmazonApiGatewayEndpointAdaptor.ts +2 -23
- package/src/endpoints/EndpointBuilder.ts +1 -4
- package/src/endpoints/EndpointFactory.ts +0 -31
- package/src/endpoints/HonoEndpointAdaptor.ts +1 -16
- package/src/endpoints/TestEndpointAdaptor.ts +4 -20
- package/src/functions/BaseFunctionBuilder.ts +0 -10
- package/src/functions/FunctionBuilder.ts +0 -2
- package/src/monitor.ts +14 -55
- package/src/monitors/heartbeat.ts +5 -3
- package/dist/AWSScheduledFunction-DPMzOjZP.d.cts.map +0 -1
- package/dist/AWSScheduledFunction-a6hRp3nk.d.mts.map +0 -1
- package/dist/Construct-D09WUUOf.cjs.map +0 -1
- package/dist/Construct-D0uZvbr3.mjs.map +0 -1
- package/dist/Construct-D_3yZ9m5.d.cts.map +0 -1
- package/dist/Construct-DsqpiAym.d.mts.map +0 -1
- package/dist/HonoEndpointAdaptor-CJfFQ5KR.mjs.map +0 -1
- package/dist/HonoEndpointAdaptor-DCD0rhFg.cjs.map +0 -1
- package/dist/functions-B3OFv3QV.cjs.map +0 -1
- package/dist/functions-DhkDodl0.mjs.map +0 -1
- package/dist/index-B8LNLQXD.d.mts.map +0 -1
- package/dist/index-CXpim5mV.d.cts.map +0 -1
- package/src/__tests__/monitor.spec.ts +0 -696
|
@@ -26,12 +26,12 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
26
26
|
|
|
27
27
|
// First cron with ServiceA and ServiceB
|
|
28
28
|
const cron1 = c
|
|
29
|
-
.schedule('
|
|
29
|
+
.schedule('cron(*/5 * * * *)')
|
|
30
30
|
.services([ServiceA, ServiceB])
|
|
31
31
|
.handle(async () => ({}));
|
|
32
32
|
|
|
33
33
|
// Second cron should not have any services from first
|
|
34
|
-
const cron2 = c.schedule('
|
|
34
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
35
35
|
|
|
36
36
|
expect(cron1.services.map((s) => s.serviceName)).toEqual(['a', 'b']);
|
|
37
37
|
expect(cron2.services.map((s) => s.serviceName)).toEqual([]);
|
|
@@ -43,12 +43,12 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
43
43
|
|
|
44
44
|
// First cron with custom logger
|
|
45
45
|
const cron1 = c
|
|
46
|
-
.schedule('
|
|
46
|
+
.schedule('cron(*/5 * * * *)')
|
|
47
47
|
.logger(customLogger)
|
|
48
48
|
.handle(async () => ({}));
|
|
49
49
|
|
|
50
50
|
// Second cron should have default logger (not the custom one)
|
|
51
|
-
const cron2 = c.schedule('
|
|
51
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
52
52
|
|
|
53
53
|
expect(cron1.logger).toBe(customLogger);
|
|
54
54
|
expect(cron2.logger).not.toBe(customLogger);
|
|
@@ -59,13 +59,13 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
59
59
|
const c = new CronBuilder();
|
|
60
60
|
|
|
61
61
|
// First cron with schedule
|
|
62
|
-
const cron1 = c.schedule('
|
|
62
|
+
const cron1 = c.schedule('cron(*/5 * * * *)').handle(async () => ({}));
|
|
63
63
|
|
|
64
64
|
// Second cron should not have schedule from first
|
|
65
|
-
const cron2 = c.schedule('
|
|
65
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
66
66
|
|
|
67
|
-
expect(cron1.schedule).toBe('
|
|
68
|
-
expect(cron2.schedule).toBe('
|
|
67
|
+
expect(cron1.schedule).toBe('cron(*/5 * * * *)');
|
|
68
|
+
expect(cron2.schedule).toBe('cron(*/10 * * * *)');
|
|
69
69
|
|
|
70
70
|
// Check internal state is reset
|
|
71
71
|
expect((c as any)._schedule).toBeUndefined();
|
|
@@ -75,7 +75,7 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
75
75
|
const c = new CronBuilder();
|
|
76
76
|
|
|
77
77
|
// Create first cron (events array should be empty initially)
|
|
78
|
-
const _cron1 = c.schedule('
|
|
78
|
+
const _cron1 = c.schedule('cron(*/5 * * * *)').handle(async () => ({}));
|
|
79
79
|
|
|
80
80
|
// Verify state was reset
|
|
81
81
|
expect((c as any)._events).toEqual([]);
|
|
@@ -90,13 +90,13 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
90
90
|
|
|
91
91
|
// First cron with schemas
|
|
92
92
|
const cron1 = c
|
|
93
|
-
.schedule('
|
|
93
|
+
.schedule('cron(*/5 * * * *)')
|
|
94
94
|
.input(inputSchema)
|
|
95
95
|
.output(outputSchema)
|
|
96
96
|
.handle(async () => ({}));
|
|
97
97
|
|
|
98
98
|
// Second cron should not have schemas
|
|
99
|
-
const cron2 = c.schedule('
|
|
99
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
100
100
|
|
|
101
101
|
expect(cron1.input).toBe(inputSchema);
|
|
102
102
|
expect(cron1.outputSchema).toBe(outputSchema);
|
|
@@ -109,12 +109,12 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
109
109
|
|
|
110
110
|
// First cron with custom timeout
|
|
111
111
|
const cron1 = c
|
|
112
|
-
.schedule('
|
|
112
|
+
.schedule('cron(*/5 * * * *)')
|
|
113
113
|
.timeout(5000)
|
|
114
114
|
.handle(async () => ({}));
|
|
115
115
|
|
|
116
116
|
// Second cron should have default timeout (30000)
|
|
117
|
-
const cron2 = c.schedule('
|
|
117
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
118
118
|
|
|
119
119
|
expect(cron1.timeout).toBe(5000);
|
|
120
120
|
expect(cron2.timeout).toBe(30000); // Default timeout
|
|
@@ -126,7 +126,7 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
126
126
|
const c = new CronBuilder();
|
|
127
127
|
|
|
128
128
|
const cron = c
|
|
129
|
-
.schedule('
|
|
129
|
+
.schedule('cron(*/5 * * * *)')
|
|
130
130
|
.services([ServiceA])
|
|
131
131
|
.services([ServiceB])
|
|
132
132
|
.handle(async () => ({}));
|
|
@@ -165,27 +165,27 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
165
165
|
const c = new CronBuilder();
|
|
166
166
|
|
|
167
167
|
const cron1 = c
|
|
168
|
-
.schedule('
|
|
168
|
+
.schedule('cron(*/5 * * * *)')
|
|
169
169
|
.services([ServiceA, ServiceB])
|
|
170
170
|
.handle(async () => ({ result: 1 }));
|
|
171
171
|
|
|
172
172
|
const cron2 = c
|
|
173
|
-
.schedule('
|
|
173
|
+
.schedule('cron(*/10 * * * *)')
|
|
174
174
|
.services([ServiceA])
|
|
175
175
|
.handle(async () => ({ result: 2 }));
|
|
176
176
|
|
|
177
177
|
const cron3 = c
|
|
178
|
-
.schedule('
|
|
178
|
+
.schedule('cron(*/15 * * * *)')
|
|
179
179
|
.handle(async () => ({ result: 3 }));
|
|
180
180
|
|
|
181
181
|
expect(cron1.services.map((s) => s.serviceName)).toEqual(['a', 'b']);
|
|
182
|
-
expect(cron1.schedule).toBe('
|
|
182
|
+
expect(cron1.schedule).toBe('cron(*/5 * * * *)');
|
|
183
183
|
|
|
184
184
|
expect(cron2.services.map((s) => s.serviceName)).toEqual(['a']);
|
|
185
|
-
expect(cron2.schedule).toBe('
|
|
185
|
+
expect(cron2.schedule).toBe('cron(*/10 * * * *)');
|
|
186
186
|
|
|
187
187
|
expect(cron3.services.map((s) => s.serviceName)).toEqual([]);
|
|
188
|
-
expect(cron3.schedule).toBe('
|
|
188
|
+
expect(cron3.schedule).toBe('cron(*/15 * * * *)');
|
|
189
189
|
});
|
|
190
190
|
});
|
|
191
191
|
|
|
@@ -200,11 +200,11 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
200
200
|
};
|
|
201
201
|
|
|
202
202
|
const cron1 = c
|
|
203
|
-
.schedule('
|
|
203
|
+
.schedule('cron(*/5 * * * *)')
|
|
204
204
|
.publisher(mockPublisher)
|
|
205
205
|
.handle(async () => ({}));
|
|
206
206
|
|
|
207
|
-
const cron2 = c.schedule('
|
|
207
|
+
const cron2 = c.schedule('cron(*/10 * * * *)').handle(async () => ({}));
|
|
208
208
|
|
|
209
209
|
expect((cron1 as any).publisherService).toBe(mockPublisher);
|
|
210
210
|
expect((cron2 as any).publisherService).toBeUndefined();
|
|
@@ -215,13 +215,13 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
215
215
|
it('should handle different schedule expression formats', () => {
|
|
216
216
|
const c = new CronBuilder();
|
|
217
217
|
|
|
218
|
-
const cron1 = c.schedule('
|
|
218
|
+
const cron1 = c.schedule('cron(*/5 * * * *)').handle(async () => ({}));
|
|
219
219
|
const cron2 = c.schedule('cron(0 12 * * ? *)').handle(async () => ({}));
|
|
220
|
-
const cron3 = c.schedule('
|
|
220
|
+
const cron3 = c.schedule('cron(0 * * * *)').handle(async () => ({}));
|
|
221
221
|
|
|
222
|
-
expect(cron1.schedule).toBe('
|
|
222
|
+
expect(cron1.schedule).toBe('cron(*/5 * * * *)');
|
|
223
223
|
expect(cron2.schedule).toBe('cron(0 12 * * ? *)');
|
|
224
|
-
expect(cron3.schedule).toBe('
|
|
224
|
+
expect(cron3.schedule).toBe('cron(0 * * * *)');
|
|
225
225
|
});
|
|
226
226
|
});
|
|
227
227
|
|
|
@@ -230,7 +230,7 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
230
230
|
const base = new CronBuilder()
|
|
231
231
|
.logger(customLogger)
|
|
232
232
|
.timeout(60_000)
|
|
233
|
-
.schedule('
|
|
233
|
+
.schedule('cron(0 0 * * *)');
|
|
234
234
|
|
|
235
235
|
const first = base.services([ServiceA]).handle(async () => ({}));
|
|
236
236
|
const second = base.services([ServiceB]).handle(async () => ({}));
|
|
@@ -254,7 +254,7 @@ describe('CronBuilder - State Isolation', () => {
|
|
|
254
254
|
const base = new CronBuilder()
|
|
255
255
|
.logger(customLogger)
|
|
256
256
|
.timeout(60_000)
|
|
257
|
-
.schedule('
|
|
257
|
+
.schedule('cron(0 0 * * *)');
|
|
258
258
|
|
|
259
259
|
// No clone in between: `.handle()` is called straight on the base,
|
|
260
260
|
// twice. Cloning the config methods was not enough on its own —
|
package/src/crons/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export { AWSScheduledFunction } from './AWSScheduledFunction';
|
|
2
2
|
export {
|
|
3
|
+
type AwsCronExpression,
|
|
3
4
|
Cron,
|
|
4
5
|
type CronExpression,
|
|
5
|
-
type RateExpression,
|
|
6
6
|
type ScheduleExpression,
|
|
7
|
+
type UnixCronExpression,
|
|
7
8
|
} from './Cron';
|
|
8
9
|
export { CronBuilder } from './CronBuilder';
|
|
9
10
|
|
|
@@ -37,7 +37,6 @@ import {
|
|
|
37
37
|
ServiceDiscovery,
|
|
38
38
|
type ServiceRecord,
|
|
39
39
|
} from '@geekmidas/services';
|
|
40
|
-
import { startMonitors } from '../monitor';
|
|
41
40
|
import { publishConstructEvents } from '../publisher';
|
|
42
41
|
import type { MappedAudit } from './audit';
|
|
43
42
|
import type { CookieFn, HeaderFn } from './Endpoint';
|
|
@@ -134,8 +133,6 @@ export abstract class AmazonApiGatewayEndpoint<
|
|
|
134
133
|
'Error processing request',
|
|
135
134
|
);
|
|
136
135
|
const wrappedError = wrapError(req.error);
|
|
137
|
-
// Kept for the monitors, which only see the response otherwise.
|
|
138
|
-
(req.event as any).__error = req.error;
|
|
139
136
|
|
|
140
137
|
// Set the response with the proper status code from the HttpError
|
|
141
138
|
req.response = {
|
|
@@ -595,26 +592,8 @@ export abstract class AmazonApiGatewayEndpoint<
|
|
|
595
592
|
|
|
596
593
|
// Cast event to any since Middy middlewares enrich the event with
|
|
597
594
|
// services, logger, session, etc. during the middleware chain execution
|
|
598
|
-
return runWithRequestContext(
|
|
599
|
-
|
|
600
|
-
async () => {
|
|
601
|
-
const monitor = await startMonitors(
|
|
602
|
-
this.endpoint,
|
|
603
|
-
ServiceDiscovery.getInstance(this.envParser),
|
|
604
|
-
logger,
|
|
605
|
-
);
|
|
606
|
-
const response = await chain(
|
|
607
|
-
event as unknown as Parameters<typeof chain>[0],
|
|
608
|
-
context,
|
|
609
|
-
);
|
|
610
|
-
// Awaited: Lambda freezes the process once the response is
|
|
611
|
-
// returned, so an unawaited ping may never be sent.
|
|
612
|
-
await monitor.settle(
|
|
613
|
-
(response as any)?.statusCode ?? 200,
|
|
614
|
-
(event as any).__error,
|
|
615
|
-
);
|
|
616
|
-
return response;
|
|
617
|
-
},
|
|
595
|
+
return runWithRequestContext({ logger, requestId, startTime }, () =>
|
|
596
|
+
chain(event as unknown as Parameters<typeof chain>[0], context),
|
|
618
597
|
);
|
|
619
598
|
};
|
|
620
599
|
|
|
@@ -715,7 +715,7 @@ export class EndpointBuilder<
|
|
|
715
715
|
}
|
|
716
716
|
}
|
|
717
717
|
|
|
718
|
-
|
|
718
|
+
return new Endpoint({
|
|
719
719
|
fn,
|
|
720
720
|
method: this.method,
|
|
721
721
|
route: this.route,
|
|
@@ -742,8 +742,5 @@ export class EndpointBuilder<
|
|
|
742
742
|
rlsBypass: this._rlsBypass,
|
|
743
743
|
responseType: this._responseType,
|
|
744
744
|
});
|
|
745
|
-
endpoint.monitors = this._monitors;
|
|
746
|
-
|
|
747
|
-
return endpoint;
|
|
748
745
|
}
|
|
749
746
|
}
|
|
@@ -8,8 +8,6 @@ import type { Logger } from '@geekmidas/logger';
|
|
|
8
8
|
import { ConsoleLogger } from '@geekmidas/logger/console';
|
|
9
9
|
import type { Service } from '@geekmidas/services';
|
|
10
10
|
import uniqBy from 'lodash.uniqby';
|
|
11
|
-
import { cloneWith } from '../clone';
|
|
12
|
-
import { addMonitor, type MonitorService } from '../monitor';
|
|
13
11
|
import type { HttpMethod } from '../types';
|
|
14
12
|
import {
|
|
15
13
|
type Authorizer,
|
|
@@ -81,7 +79,6 @@ export class EndpointFactory<
|
|
|
81
79
|
private defaultActorExtractor?: ActorExtractor<TServices, TSession, TLogger>;
|
|
82
80
|
private customSecuritySchemes: TSecuritySchemes = {} as TSecuritySchemes;
|
|
83
81
|
private defaultRlsConfig?: TRlsConfig;
|
|
84
|
-
private defaultMonitors: MonitorService[] = [];
|
|
85
82
|
|
|
86
83
|
constructor({
|
|
87
84
|
basePath,
|
|
@@ -98,7 +95,6 @@ export class EndpointFactory<
|
|
|
98
95
|
defaultActorExtractor,
|
|
99
96
|
customSecuritySchemes = {} as TSecuritySchemes,
|
|
100
97
|
defaultRlsConfig,
|
|
101
|
-
defaultMonitors = [],
|
|
102
98
|
}: EndpointFactoryOptions<
|
|
103
99
|
TServices,
|
|
104
100
|
TBasePath,
|
|
@@ -132,17 +128,6 @@ export class EndpointFactory<
|
|
|
132
128
|
this.defaultActorExtractor = defaultActorExtractor;
|
|
133
129
|
this.customSecuritySchemes = customSecuritySchemes;
|
|
134
130
|
this.defaultRlsConfig = defaultRlsConfig;
|
|
135
|
-
this.defaultMonitors = defaultMonitors;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Report every endpoint's requests to a monitor. Endpoints built from this
|
|
140
|
-
* factory inherit it, and can add their own with `.monitor()`.
|
|
141
|
-
*/
|
|
142
|
-
monitor(monitor: MonitorService): this {
|
|
143
|
-
return cloneWith(this, {
|
|
144
|
-
defaultMonitors: addMonitor(this.defaultMonitors, monitor),
|
|
145
|
-
});
|
|
146
131
|
}
|
|
147
132
|
|
|
148
133
|
static joinPaths<TBasePath extends string, P extends string>(
|
|
@@ -227,7 +212,6 @@ export class EndpointFactory<
|
|
|
227
212
|
defaultLogger: this.defaultLogger,
|
|
228
213
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
229
214
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
230
|
-
defaultMonitors: this.defaultMonitors,
|
|
231
215
|
availableAuthorizers: authorizerConfigs,
|
|
232
216
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
233
217
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -294,7 +278,6 @@ export class EndpointFactory<
|
|
|
294
278
|
defaultLogger: this.defaultLogger,
|
|
295
279
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
296
280
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
297
|
-
defaultMonitors: this.defaultMonitors,
|
|
298
281
|
availableAuthorizers: this.availableAuthorizers,
|
|
299
282
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
300
283
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -377,7 +360,6 @@ export class EndpointFactory<
|
|
|
377
360
|
defaultLogger: this.defaultLogger,
|
|
378
361
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
379
362
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
380
|
-
defaultMonitors: this.defaultMonitors,
|
|
381
363
|
availableAuthorizers: this.availableAuthorizers,
|
|
382
364
|
defaultAuthorizerName:
|
|
383
365
|
name === 'none' ? undefined : (name as TAuthorizers[number]),
|
|
@@ -431,7 +413,6 @@ export class EndpointFactory<
|
|
|
431
413
|
defaultLogger: this.defaultLogger,
|
|
432
414
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
433
415
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
434
|
-
defaultMonitors: this.defaultMonitors,
|
|
435
416
|
availableAuthorizers: this.availableAuthorizers,
|
|
436
417
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
437
418
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -483,7 +464,6 @@ export class EndpointFactory<
|
|
|
483
464
|
defaultLogger: this.defaultLogger,
|
|
484
465
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
485
466
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
486
|
-
defaultMonitors: this.defaultMonitors,
|
|
487
467
|
availableAuthorizers: this.availableAuthorizers,
|
|
488
468
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
489
469
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -541,7 +521,6 @@ export class EndpointFactory<
|
|
|
541
521
|
| SessionFn<[...S, ...TServices], TLogger, TSession, TDatabase>
|
|
542
522
|
| undefined,
|
|
543
523
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
544
|
-
defaultMonitors: this.defaultMonitors,
|
|
545
524
|
availableAuthorizers: this.availableAuthorizers,
|
|
546
525
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
547
526
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -605,7 +584,6 @@ export class EndpointFactory<
|
|
|
605
584
|
TSession
|
|
606
585
|
>,
|
|
607
586
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
608
|
-
defaultMonitors: this.defaultMonitors,
|
|
609
587
|
availableAuthorizers: this.availableAuthorizers,
|
|
610
588
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
611
589
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -666,7 +644,6 @@ export class EndpointFactory<
|
|
|
666
644
|
defaultLogger: this.defaultLogger,
|
|
667
645
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
668
646
|
defaultEventPublisher: publisher,
|
|
669
|
-
defaultMonitors: this.defaultMonitors,
|
|
670
647
|
availableAuthorizers: this.availableAuthorizers,
|
|
671
648
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
672
649
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -721,7 +698,6 @@ export class EndpointFactory<
|
|
|
721
698
|
defaultLogger: this.defaultLogger,
|
|
722
699
|
defaultSessionExtractor: session,
|
|
723
700
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
724
|
-
defaultMonitors: this.defaultMonitors,
|
|
725
701
|
availableAuthorizers: this.availableAuthorizers,
|
|
726
702
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
727
703
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -789,7 +765,6 @@ export class EndpointFactory<
|
|
|
789
765
|
| SessionFn<TServices, TLogger, TSession, T>
|
|
790
766
|
| undefined,
|
|
791
767
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
792
|
-
defaultMonitors: this.defaultMonitors,
|
|
793
768
|
availableAuthorizers: this.availableAuthorizers,
|
|
794
769
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
795
770
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -844,7 +819,6 @@ export class EndpointFactory<
|
|
|
844
819
|
defaultLogger: this.defaultLogger,
|
|
845
820
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
846
821
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
847
|
-
defaultMonitors: this.defaultMonitors,
|
|
848
822
|
availableAuthorizers: this.availableAuthorizers,
|
|
849
823
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
850
824
|
defaultAuditorStorage: storage,
|
|
@@ -904,7 +878,6 @@ export class EndpointFactory<
|
|
|
904
878
|
defaultLogger: this.defaultLogger,
|
|
905
879
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
906
880
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
907
|
-
defaultMonitors: this.defaultMonitors,
|
|
908
881
|
availableAuthorizers: this.availableAuthorizers,
|
|
909
882
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
910
883
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -973,7 +946,6 @@ export class EndpointFactory<
|
|
|
973
946
|
defaultLogger: this.defaultLogger,
|
|
974
947
|
defaultSessionExtractor: this.defaultSessionExtractor,
|
|
975
948
|
defaultEventPublisher: this.defaultEventPublisher,
|
|
976
|
-
defaultMonitors: this.defaultMonitors,
|
|
977
949
|
availableAuthorizers: this.availableAuthorizers,
|
|
978
950
|
defaultAuthorizerName: this.defaultAuthorizerName,
|
|
979
951
|
defaultAuditorStorage: this.defaultAuditorStorage,
|
|
@@ -1047,8 +1019,6 @@ export class EndpointFactory<
|
|
|
1047
1019
|
builder._setPublisher(this.defaultEventPublisher);
|
|
1048
1020
|
}
|
|
1049
1021
|
|
|
1050
|
-
builder._monitors = [...this.defaultMonitors];
|
|
1051
|
-
|
|
1052
1022
|
// Set available authorizers and default
|
|
1053
1023
|
builder._availableAuthorizers = this.availableAuthorizers;
|
|
1054
1024
|
if (this.defaultAuthorizerName) {
|
|
@@ -1173,7 +1143,6 @@ export interface EndpointFactoryOptions<
|
|
|
1173
1143
|
defaultActorExtractor?: ActorExtractor<TServices, TSession, TLogger>;
|
|
1174
1144
|
customSecuritySchemes?: TSecuritySchemes;
|
|
1175
1145
|
defaultRlsConfig?: TRlsConfig;
|
|
1176
|
-
defaultMonitors?: MonitorService[];
|
|
1177
1146
|
}
|
|
1178
1147
|
|
|
1179
1148
|
export const e = new EndpointFactory();
|
|
@@ -18,7 +18,6 @@ import { logger as honoLogger } from 'hono/logger';
|
|
|
18
18
|
import { timing } from 'hono/timing';
|
|
19
19
|
import type { ContentfulStatusCode } from 'hono/utils/http-status';
|
|
20
20
|
import { validator } from 'hono/validator';
|
|
21
|
-
import { startMonitors } from '../monitor';
|
|
22
21
|
import { publishConstructEvents } from '../publisher';
|
|
23
22
|
import type { HttpMethod, LowerHttpMethod } from '../types';
|
|
24
23
|
import type { MappedAudit } from './audit';
|
|
@@ -67,7 +66,6 @@ interface EndpointFeatures {
|
|
|
67
66
|
hasEvents: boolean;
|
|
68
67
|
hasRateLimit: boolean;
|
|
69
68
|
hasRls: boolean;
|
|
70
|
-
hasMonitors: boolean;
|
|
71
69
|
}
|
|
72
70
|
|
|
73
71
|
/**
|
|
@@ -113,7 +111,6 @@ function analyzeEndpointFeatures(
|
|
|
113
111
|
hasEvents: (endpoint.events?.length ?? 0) > 0,
|
|
114
112
|
hasRateLimit: !!endpoint.rateLimit,
|
|
115
113
|
hasRls: !!endpoint.rlsConfig && !endpoint.rlsBypass,
|
|
116
|
-
hasMonitors: endpoint.monitors.length > 0,
|
|
117
114
|
};
|
|
118
115
|
}
|
|
119
116
|
|
|
@@ -364,13 +361,8 @@ export class HonoEndpoint<
|
|
|
364
361
|
// Set response header
|
|
365
362
|
c.header('X-Request-ID', requestId);
|
|
366
363
|
|
|
367
|
-
const monitor = features.hasMonitors
|
|
368
|
-
? await startMonitors(endpoint, serviceDiscovery, logger)
|
|
369
|
-
: undefined;
|
|
370
|
-
let failure: unknown;
|
|
371
|
-
|
|
372
364
|
// Wrap entire handler in request context for services to access
|
|
373
|
-
|
|
365
|
+
return runWithRequestContext(
|
|
374
366
|
{ logger, requestId, startTime },
|
|
375
367
|
async () => {
|
|
376
368
|
try {
|
|
@@ -639,7 +631,6 @@ export class HonoEndpoint<
|
|
|
639
631
|
return c.json(error, error.statusCode as ContentfulStatusCode);
|
|
640
632
|
}
|
|
641
633
|
} catch (e: any) {
|
|
642
|
-
failure = e;
|
|
643
634
|
logger.error(e, 'Error processing endpoint request');
|
|
644
635
|
const error = wrapError(e, 500, 'Internal Server Error');
|
|
645
636
|
if (HonoEndpoint.isDev) {
|
|
@@ -652,12 +643,6 @@ export class HonoEndpoint<
|
|
|
652
643
|
}
|
|
653
644
|
},
|
|
654
645
|
);
|
|
655
|
-
|
|
656
|
-
// Not awaited: a server stays up after responding, so the request
|
|
657
|
-
// does not wait on the monitoring service. `settle` never throws.
|
|
658
|
-
void monitor?.settle(response.status, failure);
|
|
659
|
-
|
|
660
|
-
return response;
|
|
661
646
|
};
|
|
662
647
|
|
|
663
648
|
// Register route with conditional validators
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
} from '@geekmidas/audit';
|
|
6
6
|
import { DefaultAuditor } from '@geekmidas/audit';
|
|
7
7
|
import { EnvironmentParser } from '@geekmidas/envkit';
|
|
8
|
-
import { ForbiddenError
|
|
8
|
+
import { ForbiddenError } from '@geekmidas/errors';
|
|
9
9
|
import type { EventPublisher } from '@geekmidas/events';
|
|
10
10
|
import type { Logger } from '@geekmidas/logger';
|
|
11
11
|
import type {
|
|
@@ -19,7 +19,6 @@ import {
|
|
|
19
19
|
type ServiceRecord,
|
|
20
20
|
} from '@geekmidas/services';
|
|
21
21
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
22
|
-
import { startMonitors } from '../monitor';
|
|
23
22
|
import { publishConstructEvents } from '../publisher';
|
|
24
23
|
import type { HttpMethod } from '../types';
|
|
25
24
|
import type { MappedAudit } from './audit';
|
|
@@ -200,25 +199,10 @@ export class TestEndpointAdaptor<
|
|
|
200
199
|
// Get database from context for session extraction
|
|
201
200
|
const rawDb = (ctx as any).database as TDatabase;
|
|
202
201
|
|
|
203
|
-
const monitor = await startMonitors(
|
|
204
|
-
this.endpoint,
|
|
205
|
-
this.serviceDiscovery,
|
|
206
|
-
logger,
|
|
207
|
-
);
|
|
208
|
-
|
|
209
202
|
// Wrap handler execution in request context
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
async () =>
|
|
214
|
-
this.run(ctx, { body, query, params, header, cookie, logger, rawDb }),
|
|
215
|
-
);
|
|
216
|
-
await monitor.settle(response.status);
|
|
217
|
-
return response;
|
|
218
|
-
} catch (error) {
|
|
219
|
-
await monitor.settle(wrapError(error).statusCode, error);
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
203
|
+
return runWithRequestContext({ logger, requestId, startTime }, async () =>
|
|
204
|
+
this.run(ctx, { body, query, params, header, cookie, logger, rawDb }),
|
|
205
|
+
);
|
|
222
206
|
}
|
|
223
207
|
|
|
224
208
|
private async run(
|
|
@@ -11,7 +11,6 @@ import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
|
11
11
|
import get from 'lodash.get';
|
|
12
12
|
import { ConstructType } from '../Construct';
|
|
13
13
|
import { cloneWith } from '../clone';
|
|
14
|
-
import { addMonitor, type MonitorService } from '../monitor';
|
|
15
14
|
|
|
16
15
|
const DEFAULT_LOGGER = new ConsoleLogger() as any;
|
|
17
16
|
|
|
@@ -38,7 +37,6 @@ export abstract class BaseFunctionBuilder<
|
|
|
38
37
|
protected _publisher?: Service<TEventPublisherServiceName, TEventPublisher>;
|
|
39
38
|
protected _auditorStorage?: Service<TAuditStorageServiceName, TAuditStorage>;
|
|
40
39
|
protected _databaseService?: Service<TDatabaseServiceName, TDatabase>;
|
|
41
|
-
public _monitors: MonitorService[] = [];
|
|
42
40
|
|
|
43
41
|
static isStandardSchemaV1(s: unknown): s is StandardSchemaV1 {
|
|
44
42
|
const schema = (s as StandardSchemaV1)['~standard'];
|
|
@@ -91,14 +89,6 @@ export abstract class BaseFunctionBuilder<
|
|
|
91
89
|
|
|
92
90
|
abstract input<T extends ComposableStandardSchema>(schema: T): any;
|
|
93
91
|
|
|
94
|
-
/**
|
|
95
|
-
* Report each run to a monitor — a heartbeat, an uptime check, an error
|
|
96
|
-
* tracker. Can be called more than once; every monitor is told.
|
|
97
|
-
*/
|
|
98
|
-
monitor(monitor: MonitorService): this {
|
|
99
|
-
return cloneWith(this, { _monitors: addMonitor(this._monitors, monitor) });
|
|
100
|
-
}
|
|
101
|
-
|
|
102
92
|
event<TEvent extends MappedEvent<TEventPublisher, OutSchema>>(
|
|
103
93
|
event: TEvent,
|
|
104
94
|
): this {
|
package/src/monitor.ts
CHANGED
|
@@ -3,17 +3,18 @@ import type { Service, ServiceDiscovery } from '@geekmidas/services';
|
|
|
3
3
|
import type { ConstructType } from './Construct';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* What a monitor is told about the
|
|
6
|
+
* What a monitor is told about the cron whose run it is reporting.
|
|
7
7
|
*/
|
|
8
8
|
export interface MonitorContext {
|
|
9
|
-
/** The kind of construct
|
|
9
|
+
/** The kind of construct, always a cron. */
|
|
10
10
|
type: ConstructType;
|
|
11
11
|
logger: Logger;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Reports a
|
|
16
|
-
*
|
|
15
|
+
* Reports a cron's runs to an uptime service, set with `.uptime()`, so a cron
|
|
16
|
+
* that stops running is noticed. A failure is already logged where it
|
|
17
|
+
* happens; what only a monitor can report is a run that never happened.
|
|
17
18
|
*
|
|
18
19
|
* `start` runs before the handler and whatever it returns is handed back to
|
|
19
20
|
* `success` or `failure`, so a provider that pairs the two (a check-in id) can.
|
|
@@ -22,12 +23,8 @@ export interface MonitorContext {
|
|
|
22
23
|
*/
|
|
23
24
|
export interface ConstructMonitor<TRun = unknown> {
|
|
24
25
|
start?(ctx: MonitorContext): Promise<TRun>;
|
|
25
|
-
success(
|
|
26
|
-
|
|
27
|
-
): Promise<void>;
|
|
28
|
-
failure(
|
|
29
|
-
ctx: MonitorContext & { run: TRun; error: unknown; status?: number },
|
|
30
|
-
): Promise<void>;
|
|
26
|
+
success(ctx: MonitorContext & { run: TRun; output: unknown }): Promise<void>;
|
|
27
|
+
failure(ctx: MonitorContext & { run: TRun; error: unknown }): Promise<void>;
|
|
31
28
|
}
|
|
32
29
|
|
|
33
30
|
/**
|
|
@@ -50,41 +47,13 @@ export type MonitorService = Service<string, ConstructMonitor<any>> & {
|
|
|
50
47
|
|
|
51
48
|
/** A run in progress, closed by exactly one of its two methods. */
|
|
52
49
|
export interface MonitoredRun {
|
|
53
|
-
success(output: unknown
|
|
54
|
-
failure(error: unknown
|
|
55
|
-
/**
|
|
56
|
-
* Close an HTTP run from its response status: a 5xx is a failure, anything
|
|
57
|
-
* else is a success. A 4xx is the caller's mistake, not an outage.
|
|
58
|
-
*/
|
|
59
|
-
settle(status: number, error?: unknown): Promise<void>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Add a monitor to a construct's list, refusing one that is already there.
|
|
64
|
-
*
|
|
65
|
-
* Services are cached by name, so two monitors with one name would silently
|
|
66
|
-
* share an instance: two heartbeats reading `HEARTBEAT_URL` would both ping
|
|
67
|
-
* whichever was registered first, and the other provider would never hear
|
|
68
|
-
* from the construct.
|
|
69
|
-
*/
|
|
70
|
-
export function addMonitor(
|
|
71
|
-
monitors: readonly MonitorService[],
|
|
72
|
-
monitor: MonitorService,
|
|
73
|
-
): MonitorService[] {
|
|
74
|
-
if (monitors.some((m) => m.serviceName === monitor.serviceName)) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
`Monitor "${monitor.serviceName}" is already set on this construct. ` +
|
|
77
|
-
'Two heartbeats reading the same env var would share one URL; ' +
|
|
78
|
-
"give one its own, e.g. heartbeat.healthchecks('HEALTHCHECKS_HEARTBEAT_URL').",
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
return [...monitors, monitor];
|
|
50
|
+
success(output: unknown): Promise<void>;
|
|
51
|
+
failure(error: unknown): Promise<void>;
|
|
82
52
|
}
|
|
83
53
|
|
|
84
54
|
const NOOP_RUN: MonitoredRun = {
|
|
85
55
|
async success() {},
|
|
86
56
|
async failure() {},
|
|
87
|
-
async settle() {},
|
|
88
57
|
};
|
|
89
58
|
|
|
90
59
|
/**
|
|
@@ -142,20 +111,10 @@ export async function startMonitors(
|
|
|
142
111
|
}
|
|
143
112
|
};
|
|
144
113
|
|
|
145
|
-
|
|
146
|
-
success: (output
|
|
147
|
-
report((monitor,
|
|
148
|
-
|
|
149
|
-
),
|
|
150
|
-
failure: (error, status) =>
|
|
151
|
-
report((monitor, r) =>
|
|
152
|
-
monitor.failure({ ...ctx, run: r, error, status }),
|
|
153
|
-
),
|
|
154
|
-
settle: (status, error) =>
|
|
155
|
-
status >= 500
|
|
156
|
-
? run.failure(error ?? new Error(`Responded with ${status}`), status)
|
|
157
|
-
: run.success(undefined, status),
|
|
114
|
+
return {
|
|
115
|
+
success: (output) =>
|
|
116
|
+
report((monitor, run) => monitor.success({ ...ctx, run, output })),
|
|
117
|
+
failure: (error) =>
|
|
118
|
+
report((monitor, run) => monitor.failure({ ...ctx, run, error })),
|
|
158
119
|
};
|
|
159
|
-
|
|
160
|
-
return run;
|
|
161
120
|
}
|