@balena/pinejs 17.2.0-build-joshbwlng-tasks-2f4d8f53b469a2eb62ffb71cb3f9e82258e9430c-1 → 17.2.0-build-select-model-04b8e99a84456e8bdb6102de7527f3edad7cf0f5-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.
Files changed (59) hide show
  1. package/.pinejs-cache.json +1 -1
  2. package/.versionbot/CHANGELOG.yml +29 -10
  3. package/CHANGELOG.md +11 -2
  4. package/out/bin/abstract-sql-compiler.js +4 -3
  5. package/out/bin/abstract-sql-compiler.js.map +1 -1
  6. package/out/bin/odata-compiler.js +6 -4
  7. package/out/bin/odata-compiler.js.map +1 -1
  8. package/out/bin/utils.d.ts +1 -1
  9. package/out/bin/utils.js +6 -2
  10. package/out/bin/utils.js.map +1 -1
  11. package/out/config-loader/env.d.ts +0 -4
  12. package/out/config-loader/env.js +1 -5
  13. package/out/config-loader/env.js.map +1 -1
  14. package/out/data-server/sbvr-server.d.ts +3 -2
  15. package/out/data-server/sbvr-server.js +347 -384
  16. package/out/data-server/sbvr-server.js.map +1 -1
  17. package/out/database-layer/db.d.ts +0 -3
  18. package/out/database-layer/db.js +0 -17
  19. package/out/database-layer/db.js.map +1 -1
  20. package/out/sbvr-api/sbvr-utils.d.ts +0 -1
  21. package/out/sbvr-api/sbvr-utils.js +1 -3
  22. package/out/sbvr-api/sbvr-utils.js.map +1 -1
  23. package/out/server-glue/module.d.ts +0 -1
  24. package/out/server-glue/module.js +1 -4
  25. package/out/server-glue/module.js.map +1 -1
  26. package/package.json +6 -9
  27. package/src/bin/abstract-sql-compiler.ts +12 -2
  28. package/src/bin/odata-compiler.ts +25 -5
  29. package/src/bin/utils.ts +6 -1
  30. package/src/config-loader/env.ts +1 -6
  31. package/src/data-server/{sbvr-server.js → sbvr-server.ts} +47 -49
  32. package/src/database-layer/db.ts +0 -25
  33. package/src/migrator/migrations.ts +1 -1
  34. package/src/sbvr-api/dev.ts +1 -1
  35. package/src/sbvr-api/sbvr-utils.ts +1 -2
  36. package/src/sbvr-api/user.ts +1 -1
  37. package/src/server-glue/module.ts +0 -3
  38. package/out/tasks/common.d.ts +0 -4
  39. package/out/tasks/common.js +0 -11
  40. package/out/tasks/common.js.map +0 -1
  41. package/out/tasks/index.d.ts +0 -7
  42. package/out/tasks/index.js +0 -160
  43. package/out/tasks/index.js.map +0 -1
  44. package/out/tasks/out.d.ts +0 -40
  45. package/out/tasks/out.js +0 -3
  46. package/out/tasks/out.js.map +0 -1
  47. package/out/tasks/tasks.sbvr +0 -55
  48. package/out/tasks/types.d.ts +0 -40
  49. package/out/tasks/types.js +0 -3
  50. package/out/tasks/types.js.map +0 -1
  51. package/out/tasks/worker.d.ts +0 -32
  52. package/out/tasks/worker.js +0 -204
  53. package/out/tasks/worker.js.map +0 -1
  54. package/src/tasks/common.ts +0 -9
  55. package/src/tasks/index.ts +0 -176
  56. package/src/tasks/out.ts +0 -46
  57. package/src/tasks/tasks.sbvr +0 -55
  58. package/src/tasks/types.ts +0 -44
  59. package/src/tasks/worker.ts +0 -282
@@ -1,9 +1,15 @@
1
1
  import { getAbstractSqlModelFromFile, version, writeAll } from './utils';
2
- import type { SqlResult } from '@balena/abstract-sql-compiler';
2
+ import type {
3
+ AbstractSqlModel,
4
+ SqlResult,
5
+ } from '@balena/abstract-sql-compiler';
3
6
 
4
7
  import { program } from 'commander';
5
8
 
6
- const generateAbstractSqlQuery = async (modelFile: string, odata: string) => {
9
+ const generateAbstractSqlQuery = async (
10
+ abstractSqlModel: AbstractSqlModel,
11
+ odata: string,
12
+ ) => {
7
13
  const { memoizedParseOdata, translateUri } = await import(
8
14
  '../sbvr-api/uri-parser.js'
9
15
  );
@@ -19,7 +25,7 @@ const generateAbstractSqlQuery = async (modelFile: string, odata: string) => {
19
25
  odataBinds: odataAST.binds,
20
26
  values: {},
21
27
  vocabulary,
22
- abstractSqlModel: getAbstractSqlModelFromFile(modelFile),
28
+ abstractSqlModel,
23
29
  custom: {},
24
30
  translateVersions: [vocabulary],
25
31
  });
@@ -37,7 +43,10 @@ const translateOData = async (
37
43
  odata: string,
38
44
  outputFile?: string,
39
45
  ) => {
40
- const request = await generateAbstractSqlQuery(modelFile, odata);
46
+ const request = await generateAbstractSqlQuery(
47
+ getAbstractSqlModelFromFile(modelFile, program.opts().model),
48
+ odata,
49
+ );
41
50
  const json = JSON.stringify(request.abstractSqlQuery, null, 2);
42
51
  writeAll(json, outputFile);
43
52
  };
@@ -58,7 +67,10 @@ const compileOData = async (
58
67
  odata: string,
59
68
  outputFile?: string,
60
69
  ) => {
61
- const translatedRequest = await generateAbstractSqlQuery(modelFile, odata);
70
+ const translatedRequest = await generateAbstractSqlQuery(
71
+ getAbstractSqlModelFromFile(modelFile, program.opts().model),
72
+ odata,
73
+ );
62
74
  const { compileRequest } = await import('../sbvr-api/abstract-sql.js');
63
75
  const compiledRequest = compileRequest(translatedRequest);
64
76
  let output;
@@ -88,11 +100,19 @@ program
88
100
  program
89
101
  .command('translate <model-file> <input-url> [output-file]')
90
102
  .description('translate the input OData URL into abstract SQL')
103
+ .option(
104
+ '-m, --model <model-name>',
105
+ 'The target model for config files with multiple models, default: first model',
106
+ )
91
107
  .action(translateOData);
92
108
 
93
109
  program
94
110
  .command('compile <model-file> <input-url> [output-file]')
95
111
  .description('compile the input OData URL into SQL')
112
+ .option(
113
+ '-m, --model <model-name>',
114
+ 'The target model for config files with multiple models, default: first model',
115
+ )
96
116
  .action(compileOData);
97
117
 
98
118
  program
package/src/bin/utils.ts CHANGED
@@ -50,6 +50,7 @@ ${rule.sql}`,
50
50
 
51
51
  export const getAbstractSqlModelFromFile = (
52
52
  modelFile: string,
53
+ modelName?: string,
53
54
  ): AbstractSqlModel => {
54
55
  let fileContents: string | Model | AbstractSqlModel | Config;
55
56
  try {
@@ -68,7 +69,11 @@ export const getAbstractSqlModelFromFile = (
68
69
  return fileContents;
69
70
  }
70
71
  const configModel =
71
- 'models' in fileContents ? fileContents.models[0] : fileContents;
72
+ 'models' in fileContents
73
+ ? modelName
74
+ ? fileContents.models.find((m) => m.modelName === modelName)!
75
+ : fileContents.models[0]
76
+ : fileContents;
72
77
  if ('abstractSql' in configModel && configModel.abstractSql != null) {
73
78
  return configModel.abstractSql as AbstractSqlModel;
74
79
  } else if ('modelText' in configModel && configModel.modelText != null) {
@@ -49,7 +49,7 @@ export const cache = {
49
49
  apiKeyActorId: false as CacheOpts,
50
50
  };
51
51
 
52
- import { boolVar, intVar } from '@balena/env-parsing';
52
+ import { boolVar } 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,8 +146,3 @@ 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
- };
@@ -1,4 +1,8 @@
1
1
  import * as permissions from '../sbvr-api/permissions';
2
+ import type { Resolvable } from '../sbvr-api/common-types';
3
+ import type { Handler } from 'express';
4
+ import type { Config, SetupFunction } from '../config-loader/config-loader';
5
+ import type { Tx } from '../database-layer/db';
2
6
 
3
7
  const uiModel = `\
4
8
  Vocabulary: ui
@@ -19,12 +23,11 @@ Fact type: textarea has text
19
23
 
20
24
  // Middleware
21
25
  const isServerOnAir = (() => {
22
- /** @type { ((thenableOrResult?: import('../sbvr-api/common-types').Resolvable<boolean>) => void) | undefined } */
23
- let resolve;
24
- let promise = new Promise(($resolve) => {
26
+ let resolve: ((thenableOrResult?: Resolvable<boolean>) => void) | undefined;
27
+ let promise = new Promise<boolean>(($resolve) => {
25
28
  resolve = $resolve;
26
29
  });
27
- return (/** @type {boolean} */ value) => {
30
+ return (value?: boolean) => {
28
31
  if (value != null) {
29
32
  if (resolve != null) {
30
33
  resolve(value);
@@ -37,8 +40,7 @@ const isServerOnAir = (() => {
37
40
  };
38
41
  })();
39
42
 
40
- /** @type { import('express').Handler } */
41
- const serverIsOnAir = async (_req, _res, next) => {
43
+ const serverIsOnAir: Handler = async (_req, _res, next) => {
42
44
  const onAir = await isServerOnAir();
43
45
  if (onAir) {
44
46
  next();
@@ -47,48 +49,10 @@ const serverIsOnAir = async (_req, _res, next) => {
47
49
  }
48
50
  };
49
51
 
50
- /** @type {import('../config-loader/config-loader').Config} */
51
- export const config = {
52
- models: [
53
- {
54
- modelName: 'ui',
55
- modelText: uiModel,
56
- apiRoot: 'ui',
57
- customServerCode: { setup },
58
- migrations: {
59
- '11.0.0-modified-at': `\
60
- ALTER TABLE "textarea"
61
- ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;\
62
- `,
63
- '15.0.0-data-types': async (tx, sbvrUtils) => {
64
- switch (sbvrUtils.db.engine) {
65
- case 'mysql':
66
- await tx.executeSql(`\
67
- ALTER TABLE "textarea"
68
- MODIFY "is disabled" BOOLEAN NOT NULL;`);
69
- break;
70
- case 'postgres':
71
- await tx.executeSql(`\
72
- ALTER TABLE "textarea"
73
- ALTER COLUMN "is disabled" DROP DEFAULT,
74
- ALTER COLUMN "is disabled" SET DATA TYPE BOOLEAN USING "is disabled"::BOOLEAN,
75
- ALTER COLUMN "is disabled" SET DEFAULT FALSE;`);
76
- break;
77
- // No need to migrate for websql
78
- }
79
- },
80
- },
81
- },
82
- ],
83
- };
84
-
85
- /** @type { import('../config-loader/config-loader').SetupFunction } */
86
- export async function setup(app, sbvrUtils, db) {
52
+ export const setup: SetupFunction = async (app, sbvrUtils, db) => {
87
53
  const uiApi = sbvrUtils.api.ui;
88
54
  const devApi = sbvrUtils.api.dev;
89
- const setupModels = async (
90
- /** @type { import('../database-layer/db').Tx } */ tx,
91
- ) => {
55
+ const setupModels = async (tx: Tx) => {
92
56
  try {
93
57
  const uiApiTx = uiApi.clone({
94
58
  passthrough: {
@@ -138,10 +102,10 @@ export async function setup(app, sbvrUtils, db) {
138
102
  throw new Error('No SE data model found');
139
103
  }
140
104
  const instance = result[0];
105
+ const modelValue = instance.model_value as { [key: string]: string };
141
106
  await sbvrUtils.executeModel(tx, {
142
107
  apiRoot: instance.is_of__vocabulary,
143
- // prettier-ignore
144
- modelText: /** @type { string } */ (instance.model_value.value),
108
+ modelText: modelValue.value,
145
109
  });
146
110
  });
147
111
  await isServerOnAir(true);
@@ -411,4 +375,38 @@ export async function setup(app, sbvrUtils, db) {
411
375
  });
412
376
 
413
377
  await db.transaction(setupModels);
414
- }
378
+ };
379
+
380
+ export const config: Config = {
381
+ models: [
382
+ {
383
+ modelName: 'ui',
384
+ modelText: uiModel,
385
+ apiRoot: 'ui',
386
+ customServerCode: { setup },
387
+ migrations: {
388
+ '11.0.0-modified-at': `\
389
+ ALTER TABLE "textarea"
390
+ ADD COLUMN IF NOT EXISTS "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;\
391
+ `,
392
+ '15.0.0-data-types': async (tx, sbvrUtils) => {
393
+ switch (sbvrUtils.db.engine) {
394
+ case 'mysql':
395
+ await tx.executeSql(`\
396
+ ALTER TABLE "textarea"
397
+ MODIFY "is disabled" BOOLEAN NOT NULL;`);
398
+ break;
399
+ case 'postgres':
400
+ await tx.executeSql(`\
401
+ ALTER TABLE "textarea"
402
+ ALTER COLUMN "is disabled" DROP DEFAULT,
403
+ ALTER COLUMN "is disabled" SET DATA TYPE BOOLEAN USING "is disabled"::BOOLEAN,
404
+ ALTER COLUMN "is disabled" SET DEFAULT FALSE;`);
405
+ break;
406
+ // No need to migrate for websql
407
+ }
408
+ },
409
+ },
410
+ },
411
+ ],
412
+ };
@@ -98,13 +98,6 @@ export interface Database extends BaseDatabase {
98
98
  ) => Promise<Result>;
99
99
  transaction: TransactionFn;
100
100
  readTransaction: TransactionFn;
101
- on?: (
102
- name: 'notification',
103
- fn: (...args: any[]) => Promise<void>,
104
- options?: {
105
- channel?: string;
106
- },
107
- ) => void;
108
101
  }
109
102
 
110
103
  interface EngineParams {
@@ -696,24 +689,6 @@ if (maybePg != null) {
696
689
  return {
697
690
  engine: Engines.postgres,
698
691
  executeSql: atomicExecuteSql,
699
- on: async (name, fn, options) => {
700
- if (name === 'notification' && options?.channel === undefined) {
701
- throw new Error('Missing channel option for notification listener');
702
- }
703
-
704
- const client = await pool.connect();
705
- client.on(name, async (msg) => {
706
- try {
707
- await fn(msg);
708
- } catch (error) {
709
- console.error('Error handling message:', error);
710
- }
711
- });
712
-
713
- if (name === 'notification' && options?.channel !== undefined) {
714
- await client.query(`LISTEN "${options.channel}";`);
715
- }
716
- },
717
692
  transaction: createTransaction(async (stackTraceErr) => {
718
693
  const client = await pool.connect();
719
694
  const tx = new PostgresTx(client, false, stackTraceErr);
@@ -1,4 +1,4 @@
1
- // These types were generated by @balena/abstract-sql-to-typescript v3.2.1
1
+ // These types were generated by @balena/abstract-sql-to-typescript v3.2.3
2
2
 
3
3
  import type { Types } from '@balena/abstract-sql-to-typescript';
4
4
 
@@ -1,4 +1,4 @@
1
- // These types were generated by @balena/abstract-sql-to-typescript v3.2.1
1
+ // These types were generated by @balena/abstract-sql-to-typescript v3.2.3
2
2
 
3
3
  import type { Types } from '@balena/abstract-sql-to-typescript';
4
4
 
@@ -83,7 +83,6 @@ export {
83
83
  addPureHook,
84
84
  addSideEffectHook,
85
85
  } from './hooks';
86
- export { addTaskHandler } from '../tasks';
87
86
 
88
87
  import memoizeWeak = require('memoizee/weak');
89
88
  import * as controlFlow from './control-flow';
@@ -780,7 +779,7 @@ export const postExecuteModels = async (tx: Db.Tx): Promise<void> => {
780
779
  // Hence, skipped migrations from earlier models are not set as executed as the `migration` table is missing
781
780
  // Here the skipped migrations that haven't been set properly are covered
782
781
  // This is mostly an edge case when running on an empty database schema and migrations model hasn't been executed, yet.
783
- // One specific case are tests to run tests against migrated and unmigrated database states
782
+ // One specifc case are tests to run tests against migrated and unmigrated database states
784
783
 
785
784
  for (const modelKey of Object.keys(models)) {
786
785
  const pendingToSetExecutedMigrations =
@@ -1,4 +1,4 @@
1
- // These types were generated by @balena/abstract-sql-to-typescript v3.2.1
1
+ // These types were generated by @balena/abstract-sql-to-typescript v3.2.3
2
2
 
3
3
  import type { Types } from '@balena/abstract-sql-to-typescript';
4
4
 
@@ -6,7 +6,6 @@ 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';
10
9
 
11
10
  import * as sbvrUtils from '../sbvr-api/sbvr-utils';
12
11
  import { PINEJS_ADVISORY_LOCK } from '../config-loader/env';
@@ -20,7 +19,6 @@ export * as errors from '../sbvr-api/errors';
20
19
  export * as env from '../config-loader/env';
21
20
  export * as types from '../sbvr-api/common-types';
22
21
  export * as hooks from '../sbvr-api/hooks';
23
- export * as tasks from '../tasks';
24
22
  export * as webResourceHandler from '../webresource-handler';
25
23
  export type { configLoader as ConfigLoader };
26
24
  export type { migratorUtils as Migrator };
@@ -65,7 +63,6 @@ export const init = async <T extends string>(
65
63
  await sbvrUtils.setup(app, db);
66
64
  const cfgLoader = await configLoader.setup(app);
67
65
  await cfgLoader.loadConfig(migrator.config);
68
- await cfgLoader.loadConfig(tasks.config);
69
66
 
70
67
  const promises: Array<Promise<void>> = [];
71
68
  if (process.env.SBVR_SERVER_ENABLED) {
@@ -1,4 +0,0 @@
1
- import Ajv from 'ajv';
2
- export declare const apiRoot = "tasks";
3
- export declare const channel = "pinejs$task_insert";
4
- export declare const ajv: Ajv;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ajv = exports.channel = exports.apiRoot = void 0;
7
- const ajv_1 = __importDefault(require("ajv"));
8
- exports.apiRoot = 'tasks';
9
- exports.channel = 'pinejs$task_insert';
10
- exports.ajv = new ajv_1.default();
11
- //# sourceMappingURL=common.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/tasks/common.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAsB;AAGT,QAAA,OAAO,GAAG,OAAO,CAAC;AAGlB,QAAA,OAAO,GAAG,oBAAoB,CAAC;AAE/B,QAAA,GAAG,GAAG,IAAI,aAAG,EAAE,CAAC"}
@@ -1,7 +0,0 @@
1
- import type { Schema } from 'ajv';
2
- import type { ConfigLoader } from '../server-glue/module';
3
- import type { TaskHandler } from './worker';
4
- export * from './types';
5
- export declare const config: ConfigLoader.Config;
6
- export declare function setup(): Promise<void>;
7
- export declare function addTaskHandler(name: string, fn: TaskHandler['fn'], schema?: Schema): void;
@@ -1,160 +0,0 @@
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
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.config = void 0;
30
- exports.setup = setup;
31
- exports.addTaskHandler = addTaskHandler;
32
- const cronParser = __importStar(require("cron-parser"));
33
- const env_1 = require("../config-loader/env");
34
- const errors_1 = require("../sbvr-api/errors");
35
- const hooks_1 = require("../sbvr-api/hooks");
36
- const sbvrUtils = __importStar(require("../sbvr-api/sbvr-utils"));
37
- const common_1 = require("./common");
38
- const worker_1 = require("./worker");
39
- __exportStar(require("./types"), exports);
40
- const modelText = require('./tasks.sbvr');
41
- const initSql = `
42
- CREATE OR REPLACE FUNCTION notify_task_insert()
43
- RETURNS TRIGGER AS $$
44
- BEGIN
45
- PERFORM pg_notify('${common_1.channel}', NEW.id::text);
46
- RETURN NEW;
47
- END;
48
- $$ LANGUAGE plpgsql;
49
-
50
- CREATE OR REPLACE TRIGGER task_insert_trigger
51
- AFTER INSERT ON task
52
- FOR EACH ROW WHEN (NEW.status = 'queued' AND NEW."is scheduled to execute on-time" IS NULL)
53
- EXECUTE FUNCTION notify_task_insert();
54
-
55
- CREATE INDEX IF NOT EXISTS idx_task_poll ON task USING btree (
56
- "is executed by-handler",
57
- "is scheduled to execute on-time" ASC,
58
- "id" ASC
59
- ) WHERE status = 'queued';
60
- `;
61
- exports.config = {
62
- models: [
63
- {
64
- modelName: common_1.apiRoot,
65
- apiRoot: common_1.apiRoot,
66
- modelText,
67
- customServerCode: exports,
68
- initSql,
69
- },
70
- ],
71
- };
72
- let worker = null;
73
- async function setup() {
74
- if (sbvrUtils.db.engine !== 'postgres') {
75
- console.warn('Skipping task setup as database not supported');
76
- return;
77
- }
78
- const client = sbvrUtils.api[common_1.apiRoot];
79
- worker = new worker_1.Worker(client);
80
- (0, hooks_1.addPureHook)('POST', common_1.apiRoot, 'task', {
81
- POSTPARSE: async ({ req, request }) => {
82
- request.values.is_created_by__actor =
83
- req.user?.actor ?? req.apiKey?.actor;
84
- if (request.values.is_created_by__actor == null) {
85
- throw new errors_1.BadRequestError('Creating tasks with missing actor on req is not allowed');
86
- }
87
- request.values.status = 'queued';
88
- request.values.attempt_count = 0;
89
- request.values.attempt_limit ??= 1;
90
- if (request.values.is_scheduled_with__cron_expression != null &&
91
- request.values.is_scheduled_to_execute_on__time == null) {
92
- try {
93
- request.values.is_scheduled_to_execute_on__time = cronParser
94
- .parseExpression(request.values.is_scheduled_with__cron_expression)
95
- .next()
96
- .toDate()
97
- .toISOString();
98
- }
99
- catch {
100
- throw new errors_1.BadRequestError(`Invalid cron expression: ${request.values.is_scheduled_with__cron_expression}`);
101
- }
102
- }
103
- if (request.values.is_scheduled_to_execute_on__time != null) {
104
- const now = new Date(Date.now() + env_1.tasks.queueIntervalMS);
105
- const startTime = new Date(request.values.is_scheduled_to_execute_on__time);
106
- if (startTime < now) {
107
- throw new errors_1.BadRequestError(`Task scheduled start time must be greater than ${env_1.tasks.queueIntervalMS} milliseconds in the future`);
108
- }
109
- }
110
- const handlerName = request.values.is_executed_by__handler;
111
- if (handlerName == null) {
112
- throw new errors_1.BadRequestError(`Must specify a task handler to execute`);
113
- }
114
- const handler = worker?.handlers[handlerName];
115
- if (handler == null) {
116
- throw new errors_1.BadRequestError(`No task handler with name '${handlerName}' registered`);
117
- }
118
- if (handler.validate != null) {
119
- if (!handler.validate(request.values.is_executed_with__parameter_set)) {
120
- throw new errors_1.BadRequestError(`Invalid parameter set: ${common_1.ajv.errorsText(handler.validate.errors)}`);
121
- }
122
- }
123
- },
124
- });
125
- (0, hooks_1.addPureHook)('all', common_1.apiRoot, 'task', {
126
- PRERESPOND: async ({ response }) => {
127
- if (typeof response.body === 'object') {
128
- convertBigIntsToStrings(response.body);
129
- }
130
- },
131
- });
132
- worker.start();
133
- }
134
- function convertBigIntsToStrings(obj) {
135
- for (const [key, value] of Object.entries(obj)) {
136
- if (value != null) {
137
- const typeOfValue = typeof value;
138
- if (typeOfValue === 'bigint') {
139
- obj[key] = value.toString();
140
- }
141
- else if (typeOfValue === 'object') {
142
- convertBigIntsToStrings(value);
143
- }
144
- }
145
- }
146
- }
147
- function addTaskHandler(name, fn, schema) {
148
- if (worker == null) {
149
- throw new Error('Database does not support tasks');
150
- }
151
- if (worker.handlers[name] != null) {
152
- throw new Error(`Task handler with name '${name}' already registered`);
153
- }
154
- worker.handlers[name] = {
155
- name,
156
- fn,
157
- validate: schema != null ? common_1.ajv.compile(schema) : undefined,
158
- };
159
- }
160
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tasks/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,sBAyFC;AAiBD,wCAiBC;AA9KD,wDAA0C;AAC1C,8CAAyD;AACzD,+CAAqD;AACrD,6CAAgD;AAChD,kEAAoD;AAEpD,qCAAiD;AAEjD,qCAAkC;AAElC,0CAAwB;AAGxB,MAAM,SAAS,GAAW,OAAO,CAAC,cAAc,CAAC,CAAC;AAIlD,MAAM,OAAO,GAAG;;;;sBAIM,gBAAO;;;;;;;;;;;;;;;CAe5B,CAAC;AAEW,QAAA,MAAM,GAAwB;IAC1C,MAAM,EAAE;QACP;YACC,SAAS,EAAE,gBAAO;YAClB,OAAO,EAAP,gBAAO;YACP,SAAS;YACT,gBAAgB,EAAE,OAAO;YACzB,OAAO;SACP;KACD;CACD,CAAC;AAEF,IAAI,MAAM,GAAkB,IAAI,CAAC;AAC1B,KAAK,UAAU,KAAK;IAE1B,IAAI,SAAS,CAAC,EAAE,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QACxC,OAAO,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;QAC9D,OAAO;IACR,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,gBAAO,CAAC,CAAC;IACtC,MAAM,GAAG,IAAI,eAAM,CAAC,MAAM,CAAC,CAAC;IAG5B,IAAA,mBAAW,EAAC,MAA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AK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAChD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,WAAW,GAAG,OAAO,KAAK,CAAC;YACjC,IAAI,WAAW,KAAK,QAAQ,EAAE,CAAC;gBAC9B,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;YAC7B,CAAC;iBAAM,IAAI,WAAW,KAAK,QAAQ,EAAE,CAAC;gBACrC,uBAAuB,CAAC,KAAK,CAAC,CAAC;YAChC,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC;AAGD,SAAgB,cAAc,CAC7B,IAAY,EACZ,EAAqB,EACrB,MAAe;IAEf,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACpD,CAAC;IAED,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,2BAA2B,IAAI,sBAAsB,CAAC,CAAC;IACxE,CAAC;IACD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG;QACvB,IAAI;QACJ,EAAE;QACF,QAAQ,EAAE,MAAM,IAAI,IAAI,CAAC,CAAC,CAAC,YAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS;KAC1D,CAAC;AACH,CAAC"}
@@ -1,40 +0,0 @@
1
- import type { Types } from '@balena/abstract-sql-to-typescript';
2
- export interface Task {
3
- Read: {
4
- created_at: Types['Date Time']['Read'];
5
- modified_at: Types['Date Time']['Read'];
6
- id: Types['Big Serial']['Read'];
7
- key: Types['Short Text']['Read'] | null;
8
- is_created_by__actor: Types['Integer']['Read'];
9
- is_executed_by__handler: Types['Short Text']['Read'];
10
- is_executed_with__parameter_set: Types['JSON']['Read'] | null;
11
- is_scheduled_with__cron_expression: Types['Short Text']['Read'] | null;
12
- is_scheduled_to_execute_on__time: Types['Date Time']['Read'] | null;
13
- status: 'queued' | 'cancelled' | 'succeeded' | 'failed';
14
- started_on__time: Types['Date Time']['Read'] | null;
15
- ended_on__time: Types['Date Time']['Read'] | null;
16
- error_message: Types['Short Text']['Read'] | null;
17
- attempt_count: Types['Integer']['Read'];
18
- attempt_limit: Types['Integer']['Read'];
19
- };
20
- Write: {
21
- created_at: Types['Date Time']['Write'];
22
- modified_at: Types['Date Time']['Write'];
23
- id: Types['Big Serial']['Write'];
24
- key: Types['Short Text']['Write'] | null;
25
- is_created_by__actor: Types['Integer']['Write'];
26
- is_executed_by__handler: Types['Short Text']['Write'];
27
- is_executed_with__parameter_set: Types['JSON']['Write'] | null;
28
- is_scheduled_with__cron_expression: Types['Short Text']['Write'] | null;
29
- is_scheduled_to_execute_on__time: Types['Date Time']['Write'] | null;
30
- status: 'queued' | 'cancelled' | 'succeeded' | 'failed';
31
- started_on__time: Types['Date Time']['Write'] | null;
32
- ended_on__time: Types['Date Time']['Write'] | null;
33
- error_message: Types['Short Text']['Write'] | null;
34
- attempt_count: Types['Integer']['Write'];
35
- attempt_limit: Types['Integer']['Write'];
36
- };
37
- }
38
- export default interface $Model {
39
- task: Task;
40
- }
package/out/tasks/out.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=out.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"out.js","sourceRoot":"","sources":["../../src/tasks/out.ts"],"names":[],"mappings":""}
@@ -1,55 +0,0 @@
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
-