@rvoh/psychic-workers 0.2.9 → 0.3.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 (46) hide show
  1. package/dist/cjs/src/background/BaseBackgroundedModel.js +105 -0
  2. package/dist/cjs/src/background/BaseBackgroundedService.js +71 -0
  3. package/dist/cjs/src/background/BaseScheduledService.js +51 -0
  4. package/dist/cjs/src/background/helpers/nameToRedisQueueName.js +10 -0
  5. package/dist/cjs/src/background/index.js +137 -68
  6. package/dist/cjs/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +11 -0
  7. package/dist/cjs/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +17 -0
  8. package/dist/cjs/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +11 -0
  9. package/dist/cjs/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +16 -0
  10. package/dist/cjs/src/index.js +3 -3
  11. package/dist/cjs/src/psychic-app-workers/cache.js +17 -0
  12. package/dist/cjs/src/{psychic-application-workers → psychic-app-workers}/index.js +9 -9
  13. package/dist/cjs/src/types/utils.js +2 -0
  14. package/dist/esm/src/background/BaseBackgroundedModel.js +105 -0
  15. package/dist/esm/src/background/BaseBackgroundedService.js +71 -0
  16. package/dist/esm/src/background/BaseScheduledService.js +51 -0
  17. package/dist/esm/src/background/helpers/nameToRedisQueueName.js +7 -0
  18. package/dist/esm/src/background/index.js +133 -64
  19. package/dist/esm/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +8 -0
  20. package/dist/esm/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +14 -0
  21. package/dist/esm/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +8 -0
  22. package/dist/esm/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +13 -0
  23. package/dist/esm/src/index.js +2 -2
  24. package/dist/esm/src/psychic-app-workers/cache.js +12 -0
  25. package/dist/esm/src/{psychic-application-workers → psychic-app-workers}/index.js +8 -8
  26. package/dist/esm/src/types/utils.js +1 -0
  27. package/dist/types/src/background/BaseBackgroundedModel.d.ts +108 -3
  28. package/dist/types/src/background/BaseBackgroundedService.d.ts +73 -2
  29. package/dist/types/src/background/BaseScheduledService.d.ts +53 -2
  30. package/dist/types/src/background/helpers/nameToRedisQueueName.d.ts +2 -0
  31. package/dist/types/src/background/index.d.ts +115 -30
  32. package/dist/types/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.d.ts +3 -0
  33. package/dist/types/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.d.ts +5 -0
  34. package/dist/types/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +3 -0
  35. package/dist/types/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +5 -0
  36. package/dist/types/src/index.d.ts +3 -2
  37. package/dist/types/src/psychic-app-workers/cache.d.ts +4 -0
  38. package/dist/types/src/{psychic-application-workers → psychic-app-workers}/index.d.ts +12 -12
  39. package/dist/types/src/types/background.d.ts +47 -0
  40. package/package.json +12 -12
  41. package/dist/cjs/src/psychic-application-workers/cache.js +0 -17
  42. package/dist/esm/src/psychic-application-workers/cache.js +0 -12
  43. package/dist/types/src/psychic-application-workers/cache.d.ts +0 -4
  44. /package/dist/cjs/src/{background/types.js → types/background.js} +0 -0
  45. /package/dist/esm/src/{background/types.js → types/background.js} +0 -0
  46. /package/dist/types/src/{background/types.d.ts → types/utils.d.ts} +0 -0
@@ -1,35 +1,35 @@
1
- import { PsychicApplication } from '@rvoh/psychic';
1
+ import { PsychicApp } from '@rvoh/psychic';
2
2
  import { QueueOptions, WorkerOptions } from 'bullmq';
3
3
  import { Cluster, Redis } from 'ioredis';
4
- import { PsychicBackgroundOptions } from '../background/index.js';
5
- export default class PsychicApplicationWorkers {
6
- static init(psychicApp: PsychicApplication, cb: (app: PsychicApplicationWorkers) => void | Promise<void>): Promise<PsychicApplicationWorkers>;
4
+ import { PsychicBackgroundOptions } from '../types/background.js';
5
+ export default class PsychicAppWorkers {
6
+ static init(psychicApp: PsychicApp, cb: (app: PsychicAppWorkers) => void | Promise<void>): Promise<PsychicAppWorkers>;
7
7
  /**
8
8
  * Returns the cached psychic application if it has been set.
9
9
  * If it has not been set, an exception is raised.
10
10
  *
11
- * The psychic application can be set by calling PsychicApplication#init
11
+ * The psychic application can be set by calling PsychicApp#init
12
12
  */
13
- static getOrFail(): PsychicApplicationWorkers;
14
- psychicApp: PsychicApplication;
15
- constructor(psychicApp: PsychicApplication);
13
+ static getOrFail(): PsychicAppWorkers;
14
+ psychicApp: PsychicApp;
15
+ constructor(psychicApp: PsychicApp);
16
16
  /**
17
17
  * Returns the background options provided by the user
18
18
  */
19
19
  get backgroundOptions(): PsychicBackgroundOptions;
20
20
  private _backgroundOptions;
21
21
  private _hooks;
22
- get hooks(): PsychicWorkersApplicationHooks;
22
+ get hooks(): PsychicWorkersAppHooks;
23
23
  on<T extends PsychicWorkersHookEventType>(hookEventType: T, cb: T extends 'workers:shutdown' ? () => void | Promise<void> : never): void;
24
- set<Opt extends PsychicWorkersApplicationOption>(option: Opt, value: unknown): void;
24
+ set<Opt extends PsychicWorkersAppOption>(option: Opt, value: Opt extends 'background' ? PsychicBackgroundOptions : unknown): void;
25
25
  }
26
26
  export interface PsychicWorkersTypeSync {
27
27
  workstreamNames: string[];
28
28
  queueGroupMap: Record<string, string[]>;
29
29
  }
30
- export type PsychicWorkersApplicationOption = 'background';
30
+ export type PsychicWorkersAppOption = 'background';
31
31
  export type PsychicWorkersHookEventType = 'workers:shutdown';
32
- export interface PsychicWorkersApplicationHooks {
32
+ export interface PsychicWorkersAppHooks {
33
33
  workerShutdown: (() => void | Promise<void>)[];
34
34
  }
35
35
  export interface BullMQNativeWorkerOptions extends WorkerOptions {
@@ -0,0 +1,47 @@
1
+ import { IdType } from '@rvoh/dream';
2
+ import BaseBackgroundedService from '../background/BaseBackgroundedService.js';
3
+ import BaseScheduledService from '../background/BaseScheduledService.js';
4
+ import { PsychicBackgroundNativeBullMQOptions, PsychicBackgroundSimpleOptions } from '../psychic-app-workers/index.js';
5
+ import { Either } from './utils.js';
6
+ export interface BackgroundJobData {
7
+ /**
8
+ * the id of the background job. This is provided by BullMQ
9
+ */
10
+ id?: IdType;
11
+ /**
12
+ * the method name of the method on the provided class
13
+ */
14
+ method?: string;
15
+ /**
16
+ * the arguments that are fed into your background job
17
+ */
18
+ args: any;
19
+ /**
20
+ * the path to the file containing the class you are backgrounding
21
+ */
22
+ filepath?: string;
23
+ /**
24
+ * the import key of the class, which will be 'default' if the class is the default
25
+ * export of the file, and will otherwise be the name of the exported class.
26
+ */
27
+ importKey?: string;
28
+ /**
29
+ * the globalName value of the service that is being backgrounded.
30
+ */
31
+ globalName?: string;
32
+ }
33
+ export type JobTypes = 'BackgroundJobQueueFunctionJob' | 'BackgroundJobQueueStaticJob' | 'BackgroundJobQueueModelInstanceJob';
34
+ export type BackgroundQueuePriority = 'default' | 'urgent' | 'not_urgent' | 'last';
35
+ interface BaseBackgroundJobConfig {
36
+ priority?: BackgroundQueuePriority;
37
+ }
38
+ export interface WorkstreamBackgroundJobConfig<T extends BaseScheduledService | BaseBackgroundedService> extends BaseBackgroundJobConfig {
39
+ workstream?: T['psychicTypes']['workstreamNames'][number];
40
+ }
41
+ export interface QueueBackgroundJobConfig<T extends BaseScheduledService | BaseBackgroundedService, PsyTypes extends T['psychicTypes'] = T['psychicTypes'], QueueGroupMap = PsyTypes['queueGroupMap'], Queue extends keyof QueueGroupMap = keyof QueueGroupMap, Groups extends QueueGroupMap[Queue] = QueueGroupMap[Queue], GroupId = Groups[number & keyof Groups]> extends BaseBackgroundJobConfig {
42
+ groupId?: GroupId;
43
+ queue?: Queue;
44
+ }
45
+ export type BackgroundJobConfig<T extends BaseScheduledService | BaseBackgroundedService> = Either<WorkstreamBackgroundJobConfig<T>, QueueBackgroundJobConfig<T>>;
46
+ export type PsychicBackgroundOptions = (PsychicBackgroundSimpleOptions & Partial<Record<Exclude<keyof PsychicBackgroundNativeBullMQOptions, keyof PsychicBackgroundSimpleOptions>, never>>) | (PsychicBackgroundNativeBullMQOptions & Partial<Record<Exclude<keyof PsychicBackgroundSimpleOptions, keyof PsychicBackgroundNativeBullMQOptions>, never>>);
47
+ export {};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@rvoh/psychic-workers",
4
4
  "description": "Background job system for Psychic applications",
5
- "version": "0.2.9",
5
+ "version": "0.3.1",
6
6
  "author": "RVO Health",
7
7
  "repository": {
8
8
  "type": "git",
@@ -23,17 +23,17 @@
23
23
  "dist/**/*"
24
24
  ],
25
25
  "scripts": {
26
- "psy": "NODE_ENV=${NODE_ENV:-test} yarn psyts",
27
- "psyjs": "node ./dist/test-app/src/cli/index.js",
28
- "psyts": "node --experimental-specifier-resolution=node --import ./bin/esm.js ./test-app/src/cli/index.ts",
26
+ "psy": "NODE_ENV=${NODE_ENV:-test} yarn psy:ts",
27
+ "psy:js": "node ./dist/test-app/src/cli/index.js",
28
+ "psy:ts": "tsx ./test-app/src/cli/index.ts",
29
29
  "build": "echo \"building cjs...\" && rm -rf dist && npx tsc -p ./tsconfig.cjs.build.json && echo \"building esm...\" && npx tsc -p ./tsconfig.esm.build.json",
30
30
  "uspec": "vitest --config ./spec/unit/vite.config.ts",
31
31
  "format": "yarn run prettier . --write",
32
32
  "lint": "yarn run eslint --no-warn-ignored \"src/**/*.ts\" && yarn run prettier . --check",
33
33
  "console": "tsx ./src/conf/repl.ts",
34
- "consolejs": "node ./dist/src/conf/repl.js",
34
+ "console:js": "node ./dist/src/conf/repl.js",
35
35
  "prepack": "yarn build",
36
- "dev-worker": "NODE_ENV=development WORKER_COUNT=1 ts-node --transpile-only ./test-app/worker.ts"
36
+ "dev-worker": "NODE_ENV=development WORKER_COUNT=1 tsx ./test-app/worker.ts"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@rvoh/dream": "*",
@@ -43,10 +43,10 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@eslint/js": "=9.0.0",
46
- "@rvoh/dream": "^0.33.0",
47
- "@rvoh/dream-spec-helpers": "^0.2.0",
48
- "@rvoh/psychic": "^0.28.0",
49
- "@rvoh/psychic-spec-helpers": "^0.3.0",
46
+ "@rvoh/dream": "^0.39.0",
47
+ "@rvoh/dream-spec-helpers": "^0.2.4",
48
+ "@rvoh/psychic": "^0.31.0",
49
+ "@rvoh/psychic-spec-helpers": "^0.6.0",
50
50
  "@socket.io/redis-adapter": "^8.3.0",
51
51
  "@socket.io/redis-emitter": "^5.1.0",
52
52
  "@types/express": "^4",
@@ -65,12 +65,12 @@
65
65
  "socket.io": "^4.8.1",
66
66
  "socket.io-adapter": "^2.5.5",
67
67
  "supertest": "^7.0.0",
68
- "ts-node": "^10.9.2",
69
68
  "tslib": "^2.7.0",
69
+ "tsx": "^4.19.3",
70
70
  "typedoc": "^0.26.6",
71
71
  "typescript": "^5.8.2",
72
72
  "typescript-eslint": "=7.18.0",
73
73
  "vitest": "^3.1.1"
74
74
  },
75
75
  "packageManager": "yarn@4.7.0"
76
- }
76
+ }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cachePsychicWorkersApplication = cachePsychicWorkersApplication;
4
- exports.getCachedPsychicWorkersApplication = getCachedPsychicWorkersApplication;
5
- exports.getCachedPsychicWorkersApplicationOrFail = getCachedPsychicWorkersApplicationOrFail;
6
- let _psychicWorkersApp = undefined;
7
- function cachePsychicWorkersApplication(psychicWorkersApp) {
8
- _psychicWorkersApp = psychicWorkersApp;
9
- }
10
- function getCachedPsychicWorkersApplication() {
11
- return _psychicWorkersApp;
12
- }
13
- function getCachedPsychicWorkersApplicationOrFail() {
14
- if (!_psychicWorkersApp)
15
- throw new Error('must call `cachePsychicWorkersApplication` before loading cached psychic application workers');
16
- return _psychicWorkersApp;
17
- }
@@ -1,12 +0,0 @@
1
- let _psychicWorkersApp = undefined;
2
- export function cachePsychicWorkersApplication(psychicWorkersApp) {
3
- _psychicWorkersApp = psychicWorkersApp;
4
- }
5
- export function getCachedPsychicWorkersApplication() {
6
- return _psychicWorkersApp;
7
- }
8
- export function getCachedPsychicWorkersApplicationOrFail() {
9
- if (!_psychicWorkersApp)
10
- throw new Error('must call `cachePsychicWorkersApplication` before loading cached psychic application workers');
11
- return _psychicWorkersApp;
12
- }
@@ -1,4 +0,0 @@
1
- import PsychicApplicationWorkers from './index.js';
2
- export declare function cachePsychicWorkersApplication(psychicWorkersApp: PsychicApplicationWorkers): void;
3
- export declare function getCachedPsychicWorkersApplication(): PsychicApplicationWorkers | undefined;
4
- export declare function getCachedPsychicWorkersApplicationOrFail(): PsychicApplicationWorkers;