Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduled tasks #655

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"lodash": "^4.17.21",
"memoizee": "^0.4.15",
"pinejs-client-core": "^6.13.0",
"node-schedule": "^2.1.1",
"randomstring": "^1.2.3",
"typed-error": "^3.2.2"
},
Expand All @@ -71,6 +72,7 @@
"@types/mocha": "^10.0.1",
"@types/on-finished": "^2.3.1",
"@types/request": "^2.48.8",
"@types/node-schedule": "^2.1.0",
"@types/supertest": "^2.0.12",
"@types/terser-webpack-plugin": "^5.2.0",
"@types/type-is": "^1.6.3",
Expand Down
6 changes: 6 additions & 0 deletions src/config-loader/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,9 @@ export const migrator = {
*/
asyncMigrationIsEnabled: boolVar('PINEJS_ASYNC_MIGRATION_ENABLED', true),
};

export const tasks = {
pollIntervalMS: process.env.TASKS_POLL_INTERVAL_MS
? parseInt(process.env.TASKS_POLL_INTERVAL_MS, 10)
: 2000,
};
5 changes: 5 additions & 0 deletions src/sbvr-api/sbvr-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { generateODataMetadata } from '../odata-metadata/odata-metadata-generato

// eslint-disable-next-line @typescript-eslint/no-var-requires
const devModel = require('./dev.sbvr');
import * as tasks from './tasks';
import * as permissions from './permissions';
import {
BadRequestError,
Expand Down Expand Up @@ -77,6 +78,7 @@ export {
addPureHook,
addSideEffectHook,
} from './hooks';
export { addTaskHandler } from './tasks';

import memoizeWeak = require('memoizee/weak');
import * as controlFlow from './control-flow';
Expand Down Expand Up @@ -1944,6 +1946,7 @@ export const executeStandardModels = async (tx: Db.Tx): Promise<void> => {
},
});
await executeModels(tx, permissions.config.models);
await executeModels(tx, tasks.config.models);
console.info('Successfully executed standard models.');
} catch (err: any) {
console.error('Failed to execute standard models.', err);
Expand All @@ -1960,7 +1963,9 @@ export const setup = async (
await db.transaction(async (tx) => {
await executeStandardModels(tx);
await permissions.setup();
await tasks.setup(tx);
});
await tasks.postSetup(db);
} catch (err: any) {
console.error('Could not execute standard models', err);
process.exit(1);
Expand Down
77 changes: 77 additions & 0 deletions src/sbvr-api/tasks.sbvr
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Vocabulary: tasks

Term: actor
Concept Type: Integer (Type)
Term: cron expression
Concept Type: Short Text (Type)
Term: end time
Concept Type: Date Time (Type)
Term: error
Concept Type: Short Text (Type)
Term: error count
Concept Type: Integer (Type)
Term: handler
Concept Type: Short Text (Type)
Term: key
Concept Type: Short Text (Type)
Term: last error
Concept Type: Short Text (Type)
Term: last run time
Concept Type: Date Time (Type)
Term: model name
Concept Type: Short Text (Type)
Term: parameter set
Concept Type: JSON (Type)
Term: priority
Concept Type: Integer (Type)
Term: retry limit
Concept Type: Integer (Type)
Term: run count
Concept Type: Integer (Type)
Term: start time
Concept Type: Date Time (Type)
Term: status
Concept Type: Short Text (Type)

Term: task
Fact Type: task has error count
Necessity: each task has exactly one error count
Fact Type: task is executed by handler
Necessity: each task is executed by exactly one handler
Fact Type: task is executed with parameter set
Necessity: each task is executed with at most one parameter set
Fact Type: task is for model name
Necessity: each task is for exactly one model name
Fact Type: task is scheduled by actor
Necessity: each task is scheduled by exactly one actor
Fact Type: task is scheduled with cron expression
Necessity: each task is scheduled with at most one cron expression
Fact Type: task has key
Necessity: each task has exactly one key
Fact Type: task has last error
Necessity: each task has at most one last error
Fact Type: task has priority
Necessity: each task has exactly one priority
Necessity: each task has a priority that is greater than or equal to 0
Fact Type: task has retry limit
Necessity: each task has exactly one retry limit
Fact Type: task has run count
Necessity: each task has exactly one run count
Fact Type: task has start time
Necessity: each task has exactly one start time
Fact Type: task is complete

Term: task run
Fact Type: task run has error
Necessity: each task run has at most one error
Fact Type: task run is for task
Synonymous Form: task was executed by task run
Necessity: each task run is for exactly one task
Reference Type: informative
Fact Type: task run has start time
Necessity: each task run has exactly one start time
Fact Type: task run has end time
Necessity: each task run has at most one end time
Fact Type: task run has status
Necessity: each task run has exactly one status
Definition: "running" or "success" or "failed"
Loading