@balena/pinejs 17.2.0-build-add-large-file-uploads-interfaces-971bf14a0a43ac87997af442136f384f224ba6d7-1 → 17.2.0-build-joshbwlng-tasks-b246a5244eeb1c020c533c54d6fb2f112a0ed972-1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. package/.pinejs-cache.json +1 -1
  2. package/.versionbot/CHANGELOG.yml +28 -4
  3. package/CHANGELOG.md +11 -1
  4. package/out/config-loader/env.d.ts +4 -0
  5. package/out/config-loader/env.js +5 -1
  6. package/out/config-loader/env.js.map +1 -1
  7. package/out/database-layer/db.d.ts +3 -0
  8. package/out/database-layer/db.js +17 -0
  9. package/out/database-layer/db.js.map +1 -1
  10. package/out/sbvr-api/sbvr-utils.d.ts +1 -0
  11. package/out/sbvr-api/sbvr-utils.js +3 -1
  12. package/out/sbvr-api/sbvr-utils.js.map +1 -1
  13. package/out/server-glue/module.d.ts +1 -0
  14. package/out/server-glue/module.js +4 -1
  15. package/out/server-glue/module.js.map +1 -1
  16. package/out/tasks/common.d.ts +4 -0
  17. package/out/tasks/common.js +11 -0
  18. package/out/tasks/common.js.map +1 -0
  19. package/out/tasks/index.d.ts +7 -0
  20. package/out/tasks/index.js +160 -0
  21. package/out/tasks/index.js.map +1 -0
  22. package/out/tasks/out.d.ts +40 -0
  23. package/out/tasks/out.js +3 -0
  24. package/out/tasks/out.js.map +1 -0
  25. package/out/tasks/tasks.sbvr +55 -0
  26. package/out/tasks/types.d.ts +40 -0
  27. package/out/tasks/types.js +3 -0
  28. package/out/tasks/types.js.map +1 -0
  29. package/out/tasks/worker.d.ts +32 -0
  30. package/out/tasks/worker.js +204 -0
  31. package/out/tasks/worker.js.map +1 -0
  32. package/out/webresource-handler/index.d.ts +0 -25
  33. package/out/webresource-handler/index.js +2 -3
  34. package/out/webresource-handler/index.js.map +1 -1
  35. package/package.json +9 -6
  36. package/src/config-loader/env.ts +6 -1
  37. package/src/database-layer/db.ts +25 -0
  38. package/src/sbvr-api/sbvr-utils.ts +2 -1
  39. package/src/server-glue/module.ts +3 -0
  40. package/src/tasks/common.ts +9 -0
  41. package/src/tasks/index.ts +176 -0
  42. package/src/tasks/out.ts +46 -0
  43. package/src/tasks/tasks.sbvr +55 -0
  44. package/src/tasks/types.ts +44 -0
  45. package/src/tasks/worker.ts +282 -0
  46. package/src/webresource-handler/index.ts +2 -38
@@ -0,0 +1,282 @@
1
+ import type { ValidateFunction } from 'ajv';
2
+ import { setTimeout } from 'node:timers/promises';
3
+ import type { AnyObject } from 'pinejs-client-core';
4
+ import { tasks as tasksEnv } from '../config-loader/env';
5
+ import type * as Db from '../database-layer/db';
6
+ import * as permissions from '../sbvr-api/permissions';
7
+ import { PinejsClient } from '../sbvr-api/sbvr-utils';
8
+ import { sbvrUtils } from '../server-glue/module';
9
+ import { ajv, channel } from './common';
10
+ import type { Task } from './types';
11
+
12
+ interface TaskArgs {
13
+ api: PinejsClient;
14
+ params: AnyObject;
15
+ }
16
+
17
+ type TaskResponse = Promise<{
18
+ status: Task['Read']['status'];
19
+ error?: string;
20
+ }>;
21
+
22
+ export interface TaskHandler {
23
+ name: string;
24
+ fn: (options: TaskArgs) => TaskResponse;
25
+ validate?: ValidateFunction;
26
+ }
27
+
28
+ type PartialTask = Pick<
29
+ Task['Read'],
30
+ | 'id'
31
+ | 'is_created_by__actor'
32
+ | 'is_executed_by__handler'
33
+ | 'is_executed_with__parameter_set'
34
+ | 'is_scheduled_with__cron_expression'
35
+ | 'attempt_count'
36
+ | 'attempt_limit'
37
+ >;
38
+
39
+ // Map of column names with SBVR names used in SELECT queries
40
+ const selectColumns = Object.entries({
41
+ id: 'id',
42
+ 'is executed by-handler': 'is_executed_by__handler',
43
+ 'is executed with-parameter set': 'is_executed_with__parameter_set',
44
+ 'is scheduled with-cron expression': 'is_scheduled_with__cron_expression',
45
+ 'attempt count': 'attempt_count',
46
+ 'attempt limit': 'attempt_limit',
47
+ 'is created by-actor': 'is_created_by__actor',
48
+ })
49
+ .map(([key, value]) => `t."${key}" AS "${value}"`)
50
+ .join(', ');
51
+
52
+ // The worker is responsible for executing tasks in the queue. It listens for
53
+ // notifications and polls the database for tasks to execute. It will execute
54
+ // tasks in parallel up to a certain concurrency limit.
55
+ export class Worker {
56
+ public handlers: Record<string, TaskHandler> = {};
57
+ private readonly concurrency: number;
58
+ private readonly interval: number;
59
+ private executing = 0;
60
+
61
+ constructor(private readonly client: PinejsClient) {
62
+ this.concurrency = tasksEnv.queueConcurrency;
63
+ this.interval = tasksEnv.queueIntervalMS;
64
+ }
65
+
66
+ // Check if instance can execute more tasks
67
+ private canExecute(): boolean {
68
+ return (
69
+ this.executing < this.concurrency && Object.keys(this.handlers).length > 0
70
+ );
71
+ }
72
+
73
+ private async execute(task: PartialTask, tx: Db.Tx): Promise<void> {
74
+ this.executing++;
75
+ try {
76
+ // Get specified handler
77
+ const handler = this.handlers[task.is_executed_by__handler];
78
+ const startedOnTime = new Date();
79
+ if (handler == null) {
80
+ await this.update(
81
+ tx,
82
+ task,
83
+ startedOnTime,
84
+ 'failed',
85
+ 'Matching task handler not found',
86
+ );
87
+ return;
88
+ }
89
+
90
+ // Validate parameters before execution so we can fail early if
91
+ // the parameter set is invalid. This can happen if the handler
92
+ // definition changes after a task is added to the queue.
93
+ if (
94
+ handler.validate != null &&
95
+ !handler.validate(task.is_executed_with__parameter_set)
96
+ ) {
97
+ await this.update(
98
+ tx,
99
+ task,
100
+ startedOnTime,
101
+ 'failed',
102
+ `Invalid parameter set: ${ajv.errorsText(handler.validate.errors)}`,
103
+ );
104
+ return;
105
+ }
106
+
107
+ // Execute handler and update task with results
108
+ let status: Task['Read']['status'] = 'queued';
109
+ let error: string | undefined;
110
+ try {
111
+ const results = await handler.fn({
112
+ api: new PinejsClient({}),
113
+ params: task.is_executed_with__parameter_set ?? {},
114
+ });
115
+ status = results.status;
116
+ error = results.error;
117
+ } finally {
118
+ await this.update(tx, task, startedOnTime, status, error);
119
+ }
120
+ } catch (err) {
121
+ // This shouldn't happen, but if it does we want to log and kill the process
122
+ console.error(
123
+ `Failed to execute task ${task.id} with handler ${task.is_executed_by__handler}:`,
124
+ err,
125
+ );
126
+ process.exit(1);
127
+ } finally {
128
+ this.executing--;
129
+ }
130
+ }
131
+
132
+ // Update task and schedule next attempt if needed
133
+ private async update(
134
+ tx: Db.Tx,
135
+ task: PartialTask,
136
+ startedOnTime: Date,
137
+ status: Task['Read']['status'],
138
+ errorMessage?: string,
139
+ ): Promise<void> {
140
+ const attemptCount = task.attempt_count + 1;
141
+ const body: AnyObject = {
142
+ started_on__time: startedOnTime,
143
+ ended_on__time: new Date(),
144
+ status,
145
+ attempt_count: attemptCount,
146
+ ...(errorMessage != null && { error_message: errorMessage }),
147
+ };
148
+
149
+ // Re-enqueue if the task failed but has retries left, remember that
150
+ // attemptCount includes the initial attempt while attempt_limit does not
151
+ if (status === 'failed' && attemptCount < task.attempt_limit) {
152
+ body.status = 'queued';
153
+
154
+ // Schedule next attempt using exponential backoff
155
+ body.is_scheduled_to_execute_on__time =
156
+ this.getNextAttemptTime(attemptCount);
157
+ }
158
+
159
+ // Patch current task
160
+ await this.client.patch({
161
+ resource: 'task',
162
+ passthrough: {
163
+ tx,
164
+ req: permissions.root,
165
+ },
166
+ id: task.id,
167
+ body,
168
+ });
169
+
170
+ // Create new task with same configuration if previous
171
+ // iteration completed and has a cron expression
172
+ if (
173
+ ['failed', 'succeeded'].includes(body.status) &&
174
+ task.is_scheduled_with__cron_expression != null
175
+ ) {
176
+ await this.client.post({
177
+ resource: 'task',
178
+ passthrough: {
179
+ tx,
180
+ req: permissions.root,
181
+ },
182
+ options: {
183
+ returnResource: false,
184
+ },
185
+ body: {
186
+ attempt_limit: task.attempt_limit,
187
+ is_created_by__actor: task.is_created_by__actor,
188
+ is_executed_by__handler: task.is_executed_by__handler,
189
+ is_executed_with__parameter_set: task.is_executed_with__parameter_set,
190
+ is_scheduled_with__cron_expression:
191
+ task.is_scheduled_with__cron_expression,
192
+ },
193
+ });
194
+ }
195
+ }
196
+
197
+ // Calculate next attempt time using exponential backoff
198
+ private getNextAttemptTime(attempt: number): Date | null {
199
+ const delay = Math.ceil(Math.exp(Math.min(10, attempt)));
200
+ return new Date(Date.now() + delay);
201
+ }
202
+
203
+ // Poll for tasks and execute them
204
+ // This is recursive and is spawned once per concurrency limit
205
+ private poll(): void {
206
+ let executed = false;
207
+ void (async () => {
208
+ try {
209
+ if (!this.canExecute()) {
210
+ return;
211
+ }
212
+ const handlerNames = Object.keys(this.handlers);
213
+ await sbvrUtils.db.transaction(async (tx) => {
214
+ const result = await sbvrUtils.db.executeSql(
215
+ `SELECT ${selectColumns}
216
+ FROM task AS t
217
+ WHERE
218
+ t."is executed by-handler" IN (${handlerNames.map((_, index) => `$${index + 1}`).join(', ')}) AND
219
+ t."status" = 'queued' AND
220
+ t."attempt count" <= t."attempt limit" AND
221
+ (
222
+ t."is scheduled to execute on-time" IS NULL OR
223
+ t."is scheduled to execute on-time" <= CURRENT_TIMESTAMP + $${handlerNames.length + 1} * INTERVAL '1 SECOND'
224
+ )
225
+ ORDER BY
226
+ t."is scheduled to execute on-time" ASC,
227
+ t."id" ASC
228
+ LIMIT 1 FOR UPDATE SKIP LOCKED`,
229
+ [...handlerNames, Math.ceil(this.interval / 1000)],
230
+ );
231
+
232
+ // Execute task if one was found
233
+ if (result.rows.length > 0) {
234
+ await this.execute(result.rows[0] as PartialTask, tx);
235
+ executed = true;
236
+ }
237
+ });
238
+ } catch (err) {
239
+ console.error('Failed polling for tasks:', err);
240
+ } finally {
241
+ if (!executed) {
242
+ await setTimeout(this.interval);
243
+ }
244
+ this.poll();
245
+ }
246
+ })();
247
+ }
248
+
249
+ // Start listening and polling for tasks
250
+ public start(): void {
251
+ // Tasks only support postgres for now
252
+ if (sbvrUtils.db.engine !== 'postgres' || sbvrUtils.db.on == null) {
253
+ throw new Error(
254
+ 'Database does not support tasks, giving up on starting worker',
255
+ );
256
+ }
257
+ sbvrUtils.db.on(
258
+ 'notification',
259
+ async (msg) => {
260
+ if (this.canExecute()) {
261
+ await sbvrUtils.db.transaction(async (tx) => {
262
+ const result = await sbvrUtils.db.executeSql(
263
+ `SELECT ${selectColumns} FROM task AS t WHERE id = $1 FOR UPDATE SKIP LOCKED`,
264
+ [msg.payload],
265
+ );
266
+ if (result.rows.length > 0) {
267
+ await this.execute(result.rows[0] as PartialTask, tx);
268
+ }
269
+ });
270
+ }
271
+ },
272
+ {
273
+ channel,
274
+ },
275
+ );
276
+
277
+ // Spawn children to poll for and execute tasks
278
+ for (let i = 0; i < this.concurrency; i++) {
279
+ this.poll();
280
+ }
281
+ }
282
+ }
@@ -1,7 +1,6 @@
1
1
  import type * as Express from 'express';
2
2
  import busboy from 'busboy';
3
3
  import type * as stream from 'node:stream';
4
- import type { AnyObject } from '../sbvr-api/common-types';
5
4
  import * as uriParser from '../sbvr-api/uri-parser';
6
5
  import * as sbvrUtils from '../sbvr-api/sbvr-utils';
7
6
  import type { HookArgs } from '../sbvr-api/hooks';
@@ -31,43 +30,10 @@ export interface UploadResponse {
31
30
  filename: string;
32
31
  }
33
32
 
34
- export interface BeginMultipartUploadPayload {
35
- filename: string;
36
- content_type: string;
37
- size: number;
38
- chunk_size: number;
39
- }
40
-
41
- export interface UploadPart {
42
- url: string;
43
- chunkSize: number;
44
- partNumber: number;
45
- }
46
-
47
- export interface BeginMultipartUploadHandlerResponse {
48
- uploadParts: UploadPart[];
49
- fileKey: string;
50
- uploadId: string;
51
- }
52
-
53
- export interface CommitMultipartUploadPayload {
54
- fileKey: string;
55
- uploadId: string;
56
- filename: string;
57
- providerCommitData?: AnyObject;
58
- }
59
-
60
33
  export interface WebResourceHandler {
61
34
  handleFile: (resource: IncomingFile) => Promise<UploadResponse>;
62
35
  removeFile: (fileReference: string) => Promise<void>;
63
36
  onPreRespond: (webResource: WebResource) => Promise<WebResource>;
64
- beginMultipartUpload?: (
65
- fieldName: string,
66
- payload: BeginMultipartUploadPayload,
67
- ) => Promise<BeginMultipartUploadHandlerResponse>;
68
- commitMultipartUpload?: (
69
- commitInfo: CommitMultipartUploadPayload,
70
- ) => Promise<WebResource>;
71
37
  }
72
38
 
73
39
  export class WebResourceError extends TypedError {}
@@ -308,14 +274,12 @@ const getCreateWebResourceHooks = (
308
274
  };
309
275
  };
310
276
 
311
- const isDefined = <T>(x: T | undefined | null): x is T => x != null;
312
-
313
277
  const getWebResourcesHrefs = (
314
278
  webResources?: WebResourcesDbResponse[] | null,
315
279
  ): string[] => {
316
280
  const hrefs = (webResources ?? []).flatMap((resource) =>
317
281
  Object.values(resource ?? {})
318
- .filter(isDefined)
282
+ .filter((resourceKey) => resourceKey != null)
319
283
  .map((resourceKey) => normalizeHref(resourceKey.href)),
320
284
  );
321
285
  return hrefs;
@@ -331,7 +295,7 @@ const getWebResourcesKeysFromRequest = (
331
295
  ): string[] => {
332
296
  return webResourceFields
333
297
  .map((field) => values[field]?.href)
334
- .filter(isDefined);
298
+ .filter((href) => href != null);
335
299
  };
336
300
 
337
301
  const getRemoveWebResourceHooks = (