@balena/pinejs 18.0.0 → 18.1.0-build-joshbwlng-tasks-671c1667086b4af10d82d4c08df482c8b0a83384-1

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.
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Worker = void 0;
27
+ const promises_1 = require("node:timers/promises");
28
+ const env_1 = require("../config-loader/env");
29
+ const permissions = __importStar(require("../sbvr-api/permissions"));
30
+ const sbvr_utils_1 = require("../sbvr-api/sbvr-utils");
31
+ const module_1 = require("../server-glue/module");
32
+ const common_1 = require("./common");
33
+ const selectColumns = Object.entries({
34
+ id: 'id',
35
+ 'is executed by-handler': 'is_executed_by__handler',
36
+ 'is executed with-parameter set': 'is_executed_with__parameter_set',
37
+ 'is scheduled with-cron expression': 'is_scheduled_with__cron_expression',
38
+ 'attempt count': 'attempt_count',
39
+ 'attempt limit': 'attempt_limit',
40
+ 'is created by-actor': 'is_created_by__actor',
41
+ })
42
+ .map(([key, value]) => `t."${key}" AS "${value}"`)
43
+ .join(', ');
44
+ class Worker {
45
+ client;
46
+ handlers = {};
47
+ concurrency;
48
+ interval;
49
+ executing = 0;
50
+ constructor(client) {
51
+ this.client = client;
52
+ this.concurrency = env_1.tasks.queueConcurrency;
53
+ this.interval = env_1.tasks.queueIntervalMS;
54
+ }
55
+ canExecute() {
56
+ return (this.executing < this.concurrency && Object.keys(this.handlers).length > 0);
57
+ }
58
+ async execute(task, tx) {
59
+ this.executing++;
60
+ try {
61
+ const handler = this.handlers[task.is_executed_by__handler];
62
+ const startedOnTime = new Date();
63
+ if (handler == null) {
64
+ await this.update(tx, task, startedOnTime, 'failed', 'Matching task handler not found, this should never happen!');
65
+ return;
66
+ }
67
+ if (handler.validate != null &&
68
+ !handler.validate(task.is_executed_with__parameter_set)) {
69
+ await this.update(tx, task, startedOnTime, 'failed', `Invalid parameter set: ${common_1.ajv.errorsText(handler.validate.errors)}`);
70
+ return;
71
+ }
72
+ let status = 'queued';
73
+ let error;
74
+ try {
75
+ const results = await handler.fn({
76
+ api: new sbvr_utils_1.PinejsClient({}),
77
+ params: task.is_executed_with__parameter_set ?? {},
78
+ });
79
+ status = results.status;
80
+ error = results.error;
81
+ }
82
+ finally {
83
+ await this.update(tx, task, startedOnTime, status, error);
84
+ }
85
+ }
86
+ catch (err) {
87
+ console.error(`Failed to execute task ${task.id} with handler ${task.is_executed_by__handler}:`, err);
88
+ process.exit(1);
89
+ }
90
+ finally {
91
+ this.executing--;
92
+ }
93
+ }
94
+ async update(tx, task, startedOnTime, status, errorMessage) {
95
+ const attemptCount = task.attempt_count + 1;
96
+ const body = {
97
+ started_on__time: startedOnTime,
98
+ ended_on__time: new Date(),
99
+ status,
100
+ attempt_count: attemptCount,
101
+ ...(errorMessage != null && { error_message: errorMessage }),
102
+ };
103
+ if (status === 'failed' && attemptCount < task.attempt_limit) {
104
+ body.status = 'queued';
105
+ body.is_scheduled_to_execute_on__time =
106
+ this.getNextAttemptTime(attemptCount);
107
+ }
108
+ await this.client.patch({
109
+ resource: 'task',
110
+ passthrough: {
111
+ tx,
112
+ req: permissions.root,
113
+ },
114
+ id: task.id,
115
+ body,
116
+ });
117
+ if (body.status != null &&
118
+ ['failed', 'succeeded'].includes(body.status) &&
119
+ task.is_scheduled_with__cron_expression != null) {
120
+ await this.client.post({
121
+ resource: 'task',
122
+ passthrough: {
123
+ tx,
124
+ req: permissions.root,
125
+ },
126
+ options: {
127
+ returnResource: false,
128
+ },
129
+ body: {
130
+ attempt_limit: task.attempt_limit,
131
+ is_created_by__actor: task.is_created_by__actor,
132
+ is_executed_by__handler: task.is_executed_by__handler,
133
+ is_executed_with__parameter_set: task.is_executed_with__parameter_set,
134
+ is_scheduled_with__cron_expression: task.is_scheduled_with__cron_expression,
135
+ },
136
+ });
137
+ }
138
+ }
139
+ getNextAttemptTime(attempt) {
140
+ const delay = Math.ceil(Math.exp(Math.min(10, attempt)));
141
+ return new Date(Date.now() + delay);
142
+ }
143
+ poll() {
144
+ let executed = false;
145
+ void (async () => {
146
+ try {
147
+ if (!this.canExecute()) {
148
+ return;
149
+ }
150
+ const handlerNames = Object.keys(this.handlers);
151
+ await module_1.sbvrUtils.db.transaction(async (tx) => {
152
+ const result = await module_1.sbvrUtils.db.executeSql(`SELECT ${selectColumns}
153
+ FROM task AS t
154
+ WHERE
155
+ t."is executed by-handler" IN (${handlerNames.map((_, index) => `$${index + 1}`).join(', ')}) AND
156
+ t."status" = 'queued' AND
157
+ t."attempt count" <= t."attempt limit" AND
158
+ (
159
+ t."is scheduled to execute on-time" IS NULL OR
160
+ t."is scheduled to execute on-time" <= CURRENT_TIMESTAMP + $${handlerNames.length + 1} * INTERVAL '1 SECOND'
161
+ )
162
+ ORDER BY
163
+ t."is scheduled to execute on-time" ASC,
164
+ t."id" ASC
165
+ LIMIT 1 FOR UPDATE SKIP LOCKED`, [...handlerNames, Math.ceil(this.interval / 1000)]);
166
+ if (result.rows.length > 0) {
167
+ await this.execute(result.rows[0], tx);
168
+ executed = true;
169
+ }
170
+ });
171
+ }
172
+ catch (err) {
173
+ console.error('Failed polling for tasks:', err);
174
+ }
175
+ finally {
176
+ if (!executed) {
177
+ await (0, promises_1.setTimeout)(this.interval);
178
+ }
179
+ this.poll();
180
+ }
181
+ })();
182
+ }
183
+ async start() {
184
+ if (module_1.sbvrUtils.db.engine !== 'postgres' || module_1.sbvrUtils.db.on == null) {
185
+ throw new Error('Database does not support tasks, giving up on starting worker');
186
+ }
187
+ const tasksWithUnknownHandlers = await this.client.get({
188
+ resource: 'task',
189
+ passthrough: {
190
+ req: permissions.root,
191
+ },
192
+ options: {
193
+ $filter: {
194
+ status: 'queued',
195
+ $not: {
196
+ is_executed_by__handler: { $in: Object.keys(this.handlers) },
197
+ },
198
+ },
199
+ },
200
+ });
201
+ if (tasksWithUnknownHandlers.length > 0) {
202
+ throw new Error(`Found tasks with unknown handlers: ${tasksWithUnknownHandlers
203
+ .map((task) => `${task.id}(${task.is_executed_by__handler})`)
204
+ .join(', ')}`);
205
+ }
206
+ module_1.sbvrUtils.db.on('notification', async (msg) => {
207
+ if (this.canExecute()) {
208
+ await module_1.sbvrUtils.db.transaction(async (tx) => {
209
+ const result = await module_1.sbvrUtils.db.executeSql(`SELECT ${selectColumns} FROM task AS t WHERE id = $1 FOR UPDATE SKIP LOCKED`, [msg.payload]);
210
+ if (result.rows.length > 0) {
211
+ await this.execute(result.rows[0], tx);
212
+ }
213
+ });
214
+ }
215
+ }, {
216
+ channel: common_1.channel,
217
+ });
218
+ for (let i = 0; i < this.concurrency; i++) {
219
+ this.poll();
220
+ }
221
+ }
222
+ }
223
+ exports.Worker = Worker;
224
+ //# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.js","sourceRoot":"","sources":["../../src/tasks/worker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,m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AAG,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,uBAAuB,GAAG,CAAC;iBAC5D,IAAI,CAAC,IAAI,CAAC,EAAE,CACd,CAAC;QACH,CAAC;QAED,kBAAS,CAAC,EAAE,CAAC,EAAE,CACd,cAAc,EACd,KAAK,EAAE,GAAG,EAAE,EAAE;YACb,IAAI,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;gBACvB,MAAM,kBAAS,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;oBAC3C,MAAM,MAAM,GAAG,MAAM,kBAAS,CAAC,EAAE,CAAC,UAAU,CAC3C,UAAU,aAAa,sDAAsD,EAC7E,CAAC,GAAG,CAAC,OAAO,CAAC,CACb,CAAC;oBACF,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;wBAC5B,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC;oBACvD,CAAC;gBACF,CAAC,CAAC,CAAC;YACJ,CAAC;QACF,CAAC,EACD;YACC,OAAO,EAAP,gBAAO;SACP,CACD,CAAC;QAGF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3C,IAAI,CAAC,IAAI,EAAE,CAAC;QACb,CAAC;IACF,CAAC;CACD;AA/PD,wBA+PC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@balena/pinejs",
3
- "version": "18.0.0",
3
+ "version": "18.1.0-build-joshbwlng-tasks-671c1667086b4af10d82d4c08df482c8b0a83384-1",
4
4
  "main": "out/server-glue/module",
5
5
  "type": "commonjs",
6
6
  "repository": "git@github.com:balena-io/pinejs.git",
@@ -20,11 +20,11 @@
20
20
  "webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
21
21
  "lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
22
22
  "test": "npm run lint && npm run build && npm run webpack-build && npm run test:compose && npm run test:generated-types",
23
- "test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 npm run mocha",
23
+ "test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 npm run mocha",
24
24
  "test:generated-types": "npm run generate-types && git diff --exit-code ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts",
25
25
  "mocha": "TS_NODE_FILES=true mocha",
26
26
  "lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.ts",
27
- "generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
27
+ "generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
28
28
  },
29
29
  "dependencies": {
30
30
  "@balena/abstract-sql-compiler": "^9.2.0",
@@ -53,8 +53,10 @@
53
53
  "@types/pg": "^8.11.6",
54
54
  "@types/randomstring": "^1.3.0",
55
55
  "@types/websql": "^0.0.30",
56
+ "ajv": "^8.12.0",
56
57
  "busboy": "^1.6.0",
57
58
  "commander": "^11.1.0",
59
+ "cron-parser": "^4.9.0",
58
60
  "deep-freeze": "^0.0.1",
59
61
  "eventemitter3": "^5.0.1",
60
62
  "express-session": "^1.18.0",
@@ -91,6 +93,7 @@
91
93
  "grunt-ts": "^6.0.0-beta.22",
92
94
  "grunt-webpack": "^6.0.0",
93
95
  "husky": "^9.1.4",
96
+ "json-schema-to-ts": "^3.1.0",
94
97
  "lint-staged": "^15.2.7",
95
98
  "load-grunt-tasks": "^5.1.0",
96
99
  "mocha": "^10.7.0",
@@ -146,6 +149,6 @@
146
149
  "recursive": true
147
150
  },
148
151
  "versionist": {
149
- "publishedAt": "2024-08-05T08:47:15.114Z"
152
+ "publishedAt": "2024-08-06T00:38:06.175Z"
150
153
  }
151
154
  }
@@ -49,7 +49,7 @@ export const cache = {
49
49
  apiKeyActorId: false as CacheOpts,
50
50
  };
51
51
 
52
- import { boolVar } from '@balena/env-parsing';
52
+ import { boolVar, intVar } from '@balena/env-parsing';
53
53
  import memoize from 'memoizee';
54
54
  import memoizeWeak = require('memoizee/weak');
55
55
  export const createCache = <T extends (...args: any[]) => any>(
@@ -146,3 +146,8 @@ export const migrator = {
146
146
  */
147
147
  asyncMigrationIsEnabled: boolVar('PINEJS_ASYNC_MIGRATION_ENABLED', true),
148
148
  };
149
+
150
+ export const tasks = {
151
+ queueConcurrency: intVar('PINEJS_QUEUE_CONCURRENCY', 0),
152
+ queueIntervalMS: intVar('PINEJS_QUEUE_INTERVAL_MS', 1000),
153
+ };
@@ -106,6 +106,13 @@ export interface Database extends BaseDatabase {
106
106
  ) => Promise<Result>;
107
107
  transaction: TransactionFn;
108
108
  readTransaction: TransactionFn;
109
+ on?: (
110
+ name: 'notification',
111
+ fn: (...args: any[]) => Promise<void>,
112
+ options?: {
113
+ channel?: string;
114
+ },
115
+ ) => void;
109
116
  }
110
117
 
111
118
  interface EngineParams {
@@ -710,6 +717,29 @@ if (maybePg != null) {
710
717
  return {
711
718
  engine: Engines.postgres,
712
719
  executeSql: atomicExecuteSql,
720
+ on: async (name, fn, options) => {
721
+ if (name === 'notification' && options?.channel === undefined) {
722
+ throw new Error('Missing channel option for notification listener');
723
+ }
724
+
725
+ const client = await pool.connect();
726
+ client.on(name, async (msg) => {
727
+ try {
728
+ await fn(msg);
729
+ } catch (error) {
730
+ console.error('Error handling message:', error);
731
+ }
732
+ });
733
+
734
+ if (name === 'notification' && options?.channel !== undefined) {
735
+ if (options.channel.includes('"')) {
736
+ throw new Error(
737
+ `Invalid channel name for task LISTEN: ${options.channel}`,
738
+ );
739
+ }
740
+ await client.query(`LISTEN "${options.channel}";`);
741
+ }
742
+ },
713
743
  transaction: createTransaction(async (stackTraceErr, timeoutMS) => {
714
744
  const client = await pool.connect();
715
745
  const tx = new PostgresTx(client, false, stackTraceErr, timeoutMS);
@@ -777,7 +777,7 @@ export const postExecuteModels = async (tx: Db.Tx): Promise<void> => {
777
777
  // Hence, skipped migrations from earlier models are not set as executed as the `migration` table is missing
778
778
  // Here the skipped migrations that haven't been set properly are covered
779
779
  // This is mostly an edge case when running on an empty database schema and migrations model hasn't been executed, yet.
780
- // One specifc case are tests to run tests against migrated and unmigrated database states
780
+ // One specific case are tests to run tests against migrated and unmigrated database states
781
781
 
782
782
  for (const modelKey of Object.keys(models)) {
783
783
  const pendingToSetExecutedMigrations =
@@ -6,6 +6,7 @@ import * as dbModule from '../database-layer/db';
6
6
  import * as configLoader from '../config-loader/config-loader';
7
7
  import * as migrator from '../migrator/sync';
8
8
  import type * as migratorUtils from '../migrator/utils';
9
+ import * as tasks from '../tasks';
9
10
 
10
11
  import * as sbvrUtils from '../sbvr-api/sbvr-utils';
11
12
  import { PINEJS_ADVISORY_LOCK } from '../config-loader/env';
@@ -19,6 +20,7 @@ export * as errors from '../sbvr-api/errors';
19
20
  export * as env from '../config-loader/env';
20
21
  export * as types from '../sbvr-api/common-types';
21
22
  export * as hooks from '../sbvr-api/hooks';
23
+ export * as tasks from '../tasks';
22
24
  export * as webResourceHandler from '../webresource-handler';
23
25
  export type { configLoader as ConfigLoader };
24
26
  export type { migratorUtils as Migrator };
@@ -63,6 +65,7 @@ export const init = async <T extends string>(
63
65
  await sbvrUtils.setup(app, db);
64
66
  const cfgLoader = configLoader.setup(app);
65
67
  await cfgLoader.loadConfig(migrator.config);
68
+ await cfgLoader.loadConfig(tasks.config);
66
69
 
67
70
  const promises: Array<Promise<void>> = [];
68
71
  if (process.env.SBVR_SERVER_ENABLED) {
@@ -0,0 +1,9 @@
1
+ import Ajv from 'ajv';
2
+
3
+ // Root path for the tasks API
4
+ export const apiRoot = 'tasks';
5
+
6
+ // Channel name for task insert notifications
7
+ export const channel = 'pinejs$task_insert';
8
+
9
+ export const ajv = new Ajv();
@@ -0,0 +1,152 @@
1
+ import type { Schema } from 'ajv';
2
+ import * as cronParser from 'cron-parser';
3
+ import { tasks as tasksEnv } from '../config-loader/env';
4
+ import { addPureHook } from '../sbvr-api/hooks';
5
+ import * as sbvrUtils from '../sbvr-api/sbvr-utils';
6
+ import type { ConfigLoader } from '../server-glue/module';
7
+ import { ajv, apiRoot, channel } from './common';
8
+ import type { TaskHandler } from './worker';
9
+ import { Worker } from './worker';
10
+
11
+ export type * from './tasks';
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
14
+ const modelText: string = require('./tasks.sbvr');
15
+
16
+ // Create trigger for handling new tasks
17
+ // Create index for polling tasks table
18
+ const initSql = `
19
+ CREATE OR REPLACE FUNCTION notify_task_insert()
20
+ RETURNS TRIGGER AS $$
21
+ BEGIN
22
+ PERFORM pg_notify('${channel}', NEW.id::text);
23
+ RETURN NEW;
24
+ END;
25
+ $$ LANGUAGE plpgsql;
26
+
27
+ CREATE OR REPLACE TRIGGER task_insert_trigger
28
+ AFTER INSERT ON task
29
+ FOR EACH ROW WHEN (NEW.status = 'queued' AND NEW."is scheduled to execute on-time" IS NULL)
30
+ EXECUTE FUNCTION notify_task_insert();
31
+
32
+ CREATE INDEX IF NOT EXISTS idx_task_poll ON task USING btree (
33
+ "is executed by-handler",
34
+ "is scheduled to execute on-time" ASC,
35
+ "id" ASC
36
+ ) WHERE status = 'queued';
37
+ `;
38
+
39
+ export const config: ConfigLoader.Config = {
40
+ models: [
41
+ {
42
+ modelName: apiRoot,
43
+ apiRoot,
44
+ modelText,
45
+ customServerCode: exports,
46
+ initSql,
47
+ },
48
+ ],
49
+ };
50
+
51
+ export let worker: Worker | null = null;
52
+ export async function setup(): Promise<void> {
53
+ // Async task functionality is only supported on Postgres
54
+ if (sbvrUtils.db.engine !== 'postgres') {
55
+ console.warn('Skipping task setup as database not supported');
56
+ return;
57
+ }
58
+
59
+ const client = sbvrUtils.api[apiRoot];
60
+ worker = new Worker(client);
61
+
62
+ // Add resource hooks
63
+ addPureHook('POST', apiRoot, 'task', {
64
+ POSTPARSE: async ({ req, request }) => {
65
+ // Set the actor
66
+ request.values.is_created_by__actor =
67
+ req.user?.actor ?? req.apiKey?.actor;
68
+ if (request.values.is_created_by__actor == null) {
69
+ throw new Error(
70
+ 'Creating tasks with missing actor on req is not allowed',
71
+ );
72
+ }
73
+
74
+ // Set defaults
75
+ request.values.status = 'queued';
76
+ request.values.attempt_count = 0;
77
+ request.values.attempt_limit ??= 1;
78
+
79
+ // Set scheduled start time using cron expression if provided
80
+ if (
81
+ request.values.is_scheduled_with__cron_expression != null &&
82
+ request.values.is_scheduled_to_execute_on__time == null
83
+ ) {
84
+ try {
85
+ request.values.is_scheduled_to_execute_on__time = cronParser
86
+ .parseExpression(request.values.is_scheduled_with__cron_expression)
87
+ .next()
88
+ .toDate()
89
+ .toISOString();
90
+ } catch {
91
+ throw new Error(
92
+ `Invalid cron expression: ${request.values.is_scheduled_with__cron_expression}`,
93
+ );
94
+ }
95
+ }
96
+
97
+ // Assert that the provided start time is far enough in the future
98
+ if (request.values.is_scheduled_to_execute_on__time != null) {
99
+ const now = new Date(Date.now() + tasksEnv.queueIntervalMS);
100
+ const startTime = new Date(
101
+ request.values.is_scheduled_to_execute_on__time,
102
+ );
103
+ if (startTime < now) {
104
+ throw new Error(
105
+ `Task scheduled start time must be greater than ${tasksEnv.queueIntervalMS} milliseconds in the future`,
106
+ );
107
+ }
108
+ }
109
+
110
+ // Assert that the requested handler exists
111
+ const handlerName = request.values.is_executed_by__handler;
112
+ if (handlerName == null) {
113
+ throw new Error(`Must specify a task handler to execute`);
114
+ }
115
+ const handler = worker?.handlers[handlerName];
116
+ if (handler == null) {
117
+ throw new Error(
118
+ `No task handler with name '${handlerName}' registered`,
119
+ );
120
+ }
121
+
122
+ // Assert that the provided parameter set is valid
123
+ if (handler.validate != null) {
124
+ if (!handler.validate(request.values.is_executed_with__parameter_set)) {
125
+ throw new Error(
126
+ `Invalid parameter set: ${ajv.errorsText(handler.validate.errors)}`,
127
+ );
128
+ }
129
+ }
130
+ },
131
+ });
132
+ }
133
+
134
+ // Register a task handler
135
+ export function addTaskHandler(
136
+ name: string,
137
+ fn: TaskHandler['fn'],
138
+ schema?: Schema,
139
+ ): void {
140
+ if (worker == null) {
141
+ throw new Error('Database does not support tasks');
142
+ }
143
+
144
+ if (worker.handlers[name] != null) {
145
+ throw new Error(`Task handler with name '${name}' already registered`);
146
+ }
147
+ worker.handlers[name] = {
148
+ name,
149
+ fn,
150
+ validate: schema != null ? ajv.compile(schema) : undefined,
151
+ };
152
+ }
@@ -0,0 +1,55 @@
1
+ Vocabulary: tasks
2
+
3
+ Term: id
4
+ Concept Type: Big Serial (Type)
5
+ Term: actor
6
+ Concept Type: Integer (Type)
7
+ Term: attempt count
8
+ Concept Type: Integer (Type)
9
+ Term: attempt limit
10
+ Concept Type: Integer (Type)
11
+ Term: cron expression
12
+ Concept Type: Short Text (Type)
13
+ Term: error message
14
+ Concept Type: Short Text (Type)
15
+ Term: handler
16
+ Concept Type: Short Text (Type)
17
+ Term: key
18
+ Concept Type: Short Text (Type)
19
+ Term: parameter set
20
+ Concept Type: JSON (Type)
21
+ Term: status
22
+ Concept Type: Short Text (Type)
23
+ Term: time
24
+ Concept Type: Date Time (Type)
25
+
26
+ Term: task
27
+ Fact type: task has id
28
+ Necessity: each task has exactly one id
29
+ Fact type: task has key
30
+ Necessity: each task has at most one key
31
+ Fact type: task is created by actor
32
+ Necessity: each task is created by exactly one actor
33
+ Fact type: task is executed by handler
34
+ Necessity: each task is executed by exactly one handler
35
+ Fact type: task is executed with parameter set
36
+ Necessity: each task is executed with at most one parameter set
37
+ Fact type: task is scheduled with cron expression
38
+ Necessity: each task is scheduled with at most one cron expression
39
+ Fact type: task is scheduled to execute on time
40
+ Necessity: each task is scheduled to execute on at most one time
41
+ Fact type: task has status
42
+ Necessity: each task has exactly one status
43
+ Definition: "queued" or "cancelled" or "succeeded" or "failed"
44
+ Fact type: task started on time
45
+ Necessity: each task started on at most one time
46
+ Fact type: task ended on time
47
+ Necessity: each task ended on at most one time
48
+ Fact type: task has error message
49
+ Necessity: each task has at most one error message
50
+ Fact type: task has attempt count
51
+ Necessity: each task has exactly one attempt count
52
+ Fact type: task has attempt limit
53
+ Necessity: each task has exactly one attempt limit
54
+ Necessity: each task has an attempt limit that is greater than or equal to 1
55
+
@@ -0,0 +1,46 @@
1
+ // These types were generated by @balena/abstract-sql-to-typescript v3.3.1
2
+
3
+ import type { Types } from '@balena/abstract-sql-to-typescript';
4
+
5
+ export interface Task {
6
+ Read: {
7
+ created_at: Types['Date Time']['Read'];
8
+ modified_at: Types['Date Time']['Read'];
9
+ id: Types['Big Serial']['Read'];
10
+ key: Types['Short Text']['Read'] | null;
11
+ is_created_by__actor: Types['Integer']['Read'];
12
+ is_executed_by__handler: Types['Short Text']['Read'];
13
+ is_executed_with__parameter_set: Types['JSON']['Read'] | null;
14
+ is_scheduled_with__cron_expression: Types['Short Text']['Read'] | null;
15
+ is_scheduled_to_execute_on__time: Types['Date Time']['Read'] | null;
16
+ status: 'queued' | 'cancelled' | 'succeeded' | 'failed';
17
+ started_on__time: Types['Date Time']['Read'] | null;
18
+ ended_on__time: Types['Date Time']['Read'] | null;
19
+ error_message: Types['Short Text']['Read'] | null;
20
+ attempt_count: Types['Integer']['Read'];
21
+ attempt_limit: Types['Integer']['Read'];
22
+ };
23
+ Write: {
24
+ created_at: Types['Date Time']['Write'];
25
+ modified_at: Types['Date Time']['Write'];
26
+ id: Types['Big Serial']['Write'];
27
+ key: Types['Short Text']['Write'] | null;
28
+ is_created_by__actor: Types['Integer']['Write'];
29
+ is_executed_by__handler: Types['Short Text']['Write'];
30
+ is_executed_with__parameter_set: Types['JSON']['Write'] | null;
31
+ is_scheduled_with__cron_expression: Types['Short Text']['Write'] | null;
32
+ is_scheduled_to_execute_on__time: Types['Date Time']['Write'] | null;
33
+ status: 'queued' | 'cancelled' | 'succeeded' | 'failed';
34
+ started_on__time: Types['Date Time']['Write'] | null;
35
+ ended_on__time: Types['Date Time']['Write'] | null;
36
+ error_message: Types['Short Text']['Write'] | null;
37
+ attempt_count: Types['Integer']['Write'];
38
+ attempt_limit: Types['Integer']['Write'];
39
+ };
40
+ }
41
+
42
+ export default interface $Model {
43
+ task: Task;
44
+
45
+
46
+ }