@rvoh/psychic-workers 0.2.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 (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/dist/cjs/src/background/BaseBackgroundedModel.js +50 -0
  4. package/dist/cjs/src/background/BaseBackgroundedService.js +40 -0
  5. package/dist/cjs/src/background/BaseScheduledService.js +31 -0
  6. package/dist/cjs/src/background/index.js +529 -0
  7. package/dist/cjs/src/background/types.js +2 -0
  8. package/dist/cjs/src/error/background/NoQueueForSpecifiedQueueName.js +15 -0
  9. package/dist/cjs/src/error/background/NoQueueForSpecifiedWorkstream.js +15 -0
  10. package/dist/cjs/src/helpers/EnvInternal.js +5 -0
  11. package/dist/cjs/src/index.js +19 -0
  12. package/dist/cjs/src/psychic-application-workers/cache.js +17 -0
  13. package/dist/cjs/src/psychic-application-workers/index.js +77 -0
  14. package/dist/esm/src/background/BaseBackgroundedModel.js +47 -0
  15. package/dist/esm/src/background/BaseBackgroundedService.js +37 -0
  16. package/dist/esm/src/background/BaseScheduledService.js +28 -0
  17. package/dist/esm/src/background/index.js +524 -0
  18. package/dist/esm/src/background/types.js +1 -0
  19. package/dist/esm/src/error/background/NoQueueForSpecifiedQueueName.js +12 -0
  20. package/dist/esm/src/error/background/NoQueueForSpecifiedWorkstream.js +12 -0
  21. package/dist/esm/src/helpers/EnvInternal.js +3 -0
  22. package/dist/esm/src/index.js +7 -0
  23. package/dist/esm/src/psychic-application-workers/cache.js +12 -0
  24. package/dist/esm/src/psychic-application-workers/index.js +74 -0
  25. package/dist/types/src/background/BaseBackgroundedModel.d.ts +15 -0
  26. package/dist/types/src/background/BaseBackgroundedService.d.ts +17 -0
  27. package/dist/types/src/background/BaseScheduledService.d.ts +11 -0
  28. package/dist/types/src/background/index.d.ts +101 -0
  29. package/dist/types/src/background/types.d.ts +7 -0
  30. package/dist/types/src/error/background/NoQueueForSpecifiedQueueName.d.ts +5 -0
  31. package/dist/types/src/error/background/NoQueueForSpecifiedWorkstream.d.ts +5 -0
  32. package/dist/types/src/helpers/EnvInternal.d.ts +6 -0
  33. package/dist/types/src/index.d.ts +7 -0
  34. package/dist/types/src/psychic-application-workers/cache.d.ts +4 -0
  35. package/dist/types/src/psychic-application-workers/index.d.ts +200 -0
  36. package/package.json +74 -0
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bullmq_1 = require("bullmq");
4
+ const index_js_1 = require("../background/index.js");
5
+ const cache_js_1 = require("./cache.js");
6
+ class PsychicApplicationWorkers {
7
+ static async init(psychicApp, cb) {
8
+ const psychicWorkersApp = new PsychicApplicationWorkers(psychicApp);
9
+ await cb(psychicWorkersApp);
10
+ psychicApp.on('sync', () => {
11
+ index_js_1.default.connect();
12
+ const output = {
13
+ workstreamNames: [...index_js_1.default['workstreamNames']],
14
+ queueGroupMap: { ...index_js_1.default['groupNames'] },
15
+ };
16
+ return output;
17
+ });
18
+ psychicApp.on('server:shutdown', async () => {
19
+ await index_js_1.default.closeAllRedisConnections();
20
+ });
21
+ (0, cache_js_1.cachePsychicWorkersApplication)(psychicWorkersApp);
22
+ return psychicWorkersApp;
23
+ }
24
+ /**
25
+ * Returns the cached psychic application if it has been set.
26
+ * If it has not been set, an exception is raised.
27
+ *
28
+ * The psychic application can be set by calling PsychicApplication#init
29
+ */
30
+ static getOrFail() {
31
+ return (0, cache_js_1.getCachedPsychicWorkersApplicationOrFail)();
32
+ }
33
+ psychicApp;
34
+ constructor(psychicApp) {
35
+ this.psychicApp = psychicApp;
36
+ }
37
+ /**
38
+ * Returns the background options provided by the user
39
+ */
40
+ get backgroundOptions() {
41
+ return this._backgroundOptions;
42
+ }
43
+ _backgroundOptions;
44
+ _hooks = {
45
+ workerShutdown: [],
46
+ };
47
+ get hooks() {
48
+ return this._hooks;
49
+ }
50
+ on(hookEventType, cb) {
51
+ switch (hookEventType) {
52
+ case 'workers:shutdown':
53
+ this._hooks.workerShutdown.push(cb);
54
+ break;
55
+ default:
56
+ throw new Error(`unrecognized event provided to PsychicWorkersApplication#on: ${hookEventType}`);
57
+ }
58
+ }
59
+ set(option, value) {
60
+ switch (option) {
61
+ case 'background':
62
+ this._backgroundOptions = {
63
+ ...{
64
+ providers: {
65
+ Queue: bullmq_1.Queue,
66
+ Worker: bullmq_1.Worker,
67
+ },
68
+ },
69
+ ...value,
70
+ };
71
+ break;
72
+ default:
73
+ throw new Error(`Unhandled option type passed to PsychicWorkersApplication#set: ${option}`);
74
+ }
75
+ }
76
+ }
77
+ exports.default = PsychicApplicationWorkers;
@@ -0,0 +1,47 @@
1
+ import { Dream } from '@rvoh/dream';
2
+ import background from './index.js';
3
+ export default class BaseBackgroundedModel extends Dream {
4
+ static get backgroundJobConfig() {
5
+ return {};
6
+ }
7
+ get backgroundJobConfig() {
8
+ const klass = this.constructor;
9
+ return klass.backgroundJobConfig;
10
+ }
11
+ static async background(methodName, ...args) {
12
+ const safeThis = this;
13
+ return await background.staticMethod(safeThis, methodName, {
14
+ globalName: safeThis.globalName,
15
+ args,
16
+ jobConfig: safeThis.backgroundJobConfig,
17
+ });
18
+ }
19
+ static async backgroundWithDelay(delaySeconds, methodName, ...args) {
20
+ const safeThis = this;
21
+ return await background.staticMethod(safeThis, methodName, {
22
+ globalName: safeThis.globalName,
23
+ delaySeconds,
24
+ args,
25
+ jobConfig: safeThis.backgroundJobConfig,
26
+ });
27
+ }
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ get psychicTypes() {
30
+ throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
31
+ }
32
+ async background(methodName, ...args) {
33
+ const safeThis = this;
34
+ return await background.modelInstanceMethod(safeThis, methodName, {
35
+ args,
36
+ jobConfig: safeThis.backgroundJobConfig,
37
+ });
38
+ }
39
+ async backgroundWithDelay(delaySeconds, methodName, ...args) {
40
+ const safeThis = this;
41
+ return await background.modelInstanceMethod(safeThis, methodName, {
42
+ args,
43
+ delaySeconds,
44
+ jobConfig: safeThis.backgroundJobConfig,
45
+ });
46
+ }
47
+ }
@@ -0,0 +1,37 @@
1
+ import { GlobalNameNotSet } from '@rvoh/dream';
2
+ import background from './index.js';
3
+ export default class BaseBackgroundedService {
4
+ static get backgroundJobConfig() {
5
+ return {};
6
+ }
7
+ static get globalName() {
8
+ if (!this._globalName)
9
+ throw new GlobalNameNotSet(this);
10
+ return this._globalName;
11
+ }
12
+ static setGlobalName(globalName) {
13
+ this._globalName = globalName;
14
+ }
15
+ static _globalName;
16
+ static async background(methodName, ...args) {
17
+ const safeThis = this;
18
+ return await background.staticMethod(safeThis, methodName, {
19
+ globalName: safeThis.globalName,
20
+ args,
21
+ jobConfig: safeThis.backgroundJobConfig,
22
+ });
23
+ }
24
+ static async backgroundWithDelay(delaySeconds, methodName, ...args) {
25
+ const safeThis = this;
26
+ return await background.staticMethod(safeThis, methodName, {
27
+ globalName: safeThis.globalName,
28
+ delaySeconds,
29
+ args,
30
+ jobConfig: safeThis.backgroundJobConfig,
31
+ });
32
+ }
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ get psychicTypes() {
35
+ throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
36
+ }
37
+ }
@@ -0,0 +1,28 @@
1
+ import { GlobalNameNotSet } from '@rvoh/dream';
2
+ import background from './index.js';
3
+ export default class BaseScheduledService {
4
+ static get backgroundJobConfig() {
5
+ return {};
6
+ }
7
+ static get globalName() {
8
+ if (!this._globalName)
9
+ throw new GlobalNameNotSet(this);
10
+ return this._globalName;
11
+ }
12
+ static setGlobalName(globalName) {
13
+ this._globalName = globalName;
14
+ }
15
+ static _globalName;
16
+ static async schedule(pattern, methodName, ...args) {
17
+ const safeThis = this;
18
+ return await background.scheduledMethod(safeThis, pattern, methodName, {
19
+ globalName: safeThis.globalName,
20
+ args,
21
+ jobConfig: safeThis.backgroundJobConfig,
22
+ });
23
+ }
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ get psychicTypes() {
26
+ throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
27
+ }
28
+ }