@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.
- package/dist/cjs/src/background/BaseBackgroundedModel.js +105 -0
- package/dist/cjs/src/background/BaseBackgroundedService.js +71 -0
- package/dist/cjs/src/background/BaseScheduledService.js +51 -0
- package/dist/cjs/src/background/helpers/nameToRedisQueueName.js +10 -0
- package/dist/cjs/src/background/index.js +137 -68
- package/dist/cjs/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +11 -0
- package/dist/cjs/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +17 -0
- package/dist/cjs/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +11 -0
- package/dist/cjs/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +16 -0
- package/dist/cjs/src/index.js +3 -3
- package/dist/cjs/src/psychic-app-workers/cache.js +17 -0
- package/dist/cjs/src/{psychic-application-workers → psychic-app-workers}/index.js +9 -9
- package/dist/cjs/src/types/utils.js +2 -0
- package/dist/esm/src/background/BaseBackgroundedModel.js +105 -0
- package/dist/esm/src/background/BaseBackgroundedService.js +71 -0
- package/dist/esm/src/background/BaseScheduledService.js +51 -0
- package/dist/esm/src/background/helpers/nameToRedisQueueName.js +7 -0
- package/dist/esm/src/background/index.js +133 -64
- package/dist/esm/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +8 -0
- package/dist/esm/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +14 -0
- package/dist/esm/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +8 -0
- package/dist/esm/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +13 -0
- package/dist/esm/src/index.js +2 -2
- package/dist/esm/src/psychic-app-workers/cache.js +12 -0
- package/dist/esm/src/{psychic-application-workers → psychic-app-workers}/index.js +8 -8
- package/dist/esm/src/types/utils.js +1 -0
- package/dist/types/src/background/BaseBackgroundedModel.d.ts +108 -3
- package/dist/types/src/background/BaseBackgroundedService.d.ts +73 -2
- package/dist/types/src/background/BaseScheduledService.d.ts +53 -2
- package/dist/types/src/background/helpers/nameToRedisQueueName.d.ts +2 -0
- package/dist/types/src/background/index.d.ts +115 -30
- package/dist/types/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.d.ts +3 -0
- package/dist/types/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.d.ts +5 -0
- package/dist/types/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +3 -0
- package/dist/types/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +5 -0
- package/dist/types/src/index.d.ts +3 -2
- package/dist/types/src/psychic-app-workers/cache.d.ts +4 -0
- package/dist/types/src/{psychic-application-workers → psychic-app-workers}/index.d.ts +12 -12
- package/dist/types/src/types/background.d.ts +47 -0
- package/package.json +12 -12
- package/dist/cjs/src/psychic-application-workers/cache.js +0 -17
- package/dist/esm/src/psychic-application-workers/cache.js +0 -12
- package/dist/types/src/psychic-application-workers/cache.d.ts +0 -4
- /package/dist/cjs/src/{background/types.js → types/background.js} +0 -0
- /package/dist/esm/src/{background/types.js → types/background.js} +0 -0
- /package/dist/types/src/{background/types.d.ts → types/utils.d.ts} +0 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
export default class ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection extends Error {
|
2
|
+
queueName;
|
3
|
+
constructor(queueName) {
|
4
|
+
super();
|
5
|
+
this.queueName = queueName;
|
6
|
+
}
|
7
|
+
get message() {
|
8
|
+
return `
|
9
|
+
defaultWorkerConnection is missing, and the ${this.queueName} queue does not
|
10
|
+
specify a workerConnection. A worker connection isrequired when activating workers.
|
11
|
+
For example, it may be omitted on webserver instances, but is required on worker instances.
|
12
|
+
`;
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export default class DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection extends Error {
|
2
|
+
get message() {
|
3
|
+
return `
|
4
|
+
Native BullMQ options don't include a default queue connection, and the
|
5
|
+
default config does not include a queue connection
|
6
|
+
`;
|
7
|
+
}
|
8
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
export default class NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection extends Error {
|
2
|
+
queueName;
|
3
|
+
constructor(queueName) {
|
4
|
+
super();
|
5
|
+
this.queueName = queueName;
|
6
|
+
}
|
7
|
+
get message() {
|
8
|
+
return `
|
9
|
+
Native BullMQ options don't include a default queue connection, and the
|
10
|
+
${this.queueName} queue does not include a queue connection
|
11
|
+
`;
|
12
|
+
}
|
13
|
+
}
|
package/dist/esm/src/index.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
export { default as background, Background, stopBackgroundWorkers
|
1
|
+
export { default as background, Background, stopBackgroundWorkers } from './background/index.js';
|
2
2
|
export { default as BaseBackgroundedModel } from './background/BaseBackgroundedModel.js';
|
3
3
|
export { default as BaseBackgroundedService } from './background/BaseBackgroundedService.js';
|
4
4
|
export { default as BaseScheduledService } from './background/BaseScheduledService.js';
|
5
|
-
export { default as
|
5
|
+
export { default as PsychicAppWorkers, } from './psychic-app-workers/index.js';
|
6
6
|
export { default as NoQueueForSpecifiedQueueName } from './error/background/NoQueueForSpecifiedQueueName.js';
|
7
7
|
export { default as NoQueueForSpecifiedWorkstream } from './error/background/NoQueueForSpecifiedWorkstream.js';
|
@@ -0,0 +1,12 @@
|
|
1
|
+
let _psychicWorkersApp = undefined;
|
2
|
+
export function cachePsychicWorkersApp(psychicWorkersApp) {
|
3
|
+
_psychicWorkersApp = psychicWorkersApp;
|
4
|
+
}
|
5
|
+
export function getCachedPsychicWorkersApp() {
|
6
|
+
return _psychicWorkersApp;
|
7
|
+
}
|
8
|
+
export function getCachedPsychicWorkersAppOrFail() {
|
9
|
+
if (!_psychicWorkersApp)
|
10
|
+
throw new Error('must call `cachePsychicWorkersApp` before loading cached psychic application workers');
|
11
|
+
return _psychicWorkersApp;
|
12
|
+
}
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { Queue, Worker } from 'bullmq';
|
2
|
+
import { cachePsychicWorkersApp, getCachedPsychicWorkersAppOrFail } from './cache.js';
|
2
3
|
import background from '../background/index.js';
|
3
|
-
|
4
|
-
export default class PsychicApplicationWorkers {
|
4
|
+
export default class PsychicAppWorkers {
|
5
5
|
static async init(psychicApp, cb) {
|
6
|
-
const psychicWorkersApp = new
|
6
|
+
const psychicWorkersApp = new PsychicAppWorkers(psychicApp);
|
7
7
|
await cb(psychicWorkersApp);
|
8
8
|
psychicApp.on('sync', () => {
|
9
9
|
background.connect();
|
@@ -16,17 +16,17 @@ export default class PsychicApplicationWorkers {
|
|
16
16
|
psychicApp.on('server:shutdown', async () => {
|
17
17
|
await background.closeAllRedisConnections();
|
18
18
|
});
|
19
|
-
|
19
|
+
cachePsychicWorkersApp(psychicWorkersApp);
|
20
20
|
return psychicWorkersApp;
|
21
21
|
}
|
22
22
|
/**
|
23
23
|
* Returns the cached psychic application if it has been set.
|
24
24
|
* If it has not been set, an exception is raised.
|
25
25
|
*
|
26
|
-
* The psychic application can be set by calling
|
26
|
+
* The psychic application can be set by calling PsychicApp#init
|
27
27
|
*/
|
28
28
|
static getOrFail() {
|
29
|
-
return
|
29
|
+
return getCachedPsychicWorkersAppOrFail();
|
30
30
|
}
|
31
31
|
psychicApp;
|
32
32
|
constructor(psychicApp) {
|
@@ -51,7 +51,7 @@ export default class PsychicApplicationWorkers {
|
|
51
51
|
this._hooks.workerShutdown.push(cb);
|
52
52
|
break;
|
53
53
|
default:
|
54
|
-
throw new Error(`unrecognized event provided to
|
54
|
+
throw new Error(`unrecognized event provided to PsychicWorkersApp#on: ${hookEventType}`);
|
55
55
|
}
|
56
56
|
}
|
57
57
|
set(option, value) {
|
@@ -68,7 +68,7 @@ export default class PsychicApplicationWorkers {
|
|
68
68
|
};
|
69
69
|
break;
|
70
70
|
default:
|
71
|
-
throw new Error(`Unhandled option type passed to
|
71
|
+
throw new Error(`Unhandled option type passed to PsychicWorkersApp#set: ${option}`);
|
72
72
|
}
|
73
73
|
}
|
74
74
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -1,14 +1,119 @@
|
|
1
1
|
import { Dream } from '@rvoh/dream';
|
2
|
+
import { BackgroundJobConfig } from '../types/background.js';
|
3
|
+
import { FunctionPropertyNames } from '../types/utils.js';
|
2
4
|
import { BackgroundableMethodArgs } from './BaseBackgroundedService.js';
|
3
|
-
import { BackgroundJobConfig } from './index.js';
|
4
|
-
import { FunctionPropertyNames } from './types.js';
|
5
5
|
export default class BaseBackgroundedModel extends Dream {
|
6
|
+
/**
|
7
|
+
* A getter meant to be overridden in child classes. This does
|
8
|
+
* not have to be explicitly provided, but if so, it would allow
|
9
|
+
* you to override the default behavior of anything backgrounded
|
10
|
+
* by this service, such as the priority or workstream.
|
11
|
+
*
|
12
|
+
* @returns {object} config - the background job config
|
13
|
+
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
14
|
+
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
15
|
+
* @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
16
|
+
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
17
|
+
*/
|
6
18
|
static get backgroundJobConfig(): BackgroundJobConfig<BaseBackgroundedModel>;
|
7
|
-
|
19
|
+
/**
|
20
|
+
* @internal
|
21
|
+
*
|
22
|
+
* shadows the static `backgroundJobConfig` getter provided by the user.
|
23
|
+
* This should never be overridden, and is meant to provide easy access
|
24
|
+
* to the config from within an instance.
|
25
|
+
*/
|
26
|
+
protected get backgroundJobConfig(): BackgroundJobConfig<BaseBackgroundedModel>;
|
27
|
+
/**
|
28
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
29
|
+
* sending in the provided args.
|
30
|
+
*
|
31
|
+
* ```ts
|
32
|
+
* await User.background('myMethod', 'abc', 123)
|
33
|
+
* ```
|
34
|
+
* though calling background must be awaited, the resolution of the promise
|
35
|
+
* is an indication that the job was put in the queue, not that it has
|
36
|
+
* completed.
|
37
|
+
*
|
38
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
39
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
40
|
+
* before making assertions.
|
41
|
+
*
|
42
|
+
* @param methodName - the name of the static method you wish to run in the background
|
43
|
+
* @param args - a variadic list of arguments to be sent to your method
|
44
|
+
*/
|
8
45
|
static background<T, MethodName extends PsychicBackgroundedModelStaticMethods<T & typeof BaseBackgroundedModel>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
46
|
+
/**
|
47
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
48
|
+
* sending in the provided args, including a delay in seconds, which
|
49
|
+
* can be used to hold off the job for a certain amount of time after
|
50
|
+
* it is entered into the queue.
|
51
|
+
*
|
52
|
+
* ```ts
|
53
|
+
* await User.backgroundWithDelay('myMethod', 'abc', 123)
|
54
|
+
* ```
|
55
|
+
* though calling background must be awaited, the resolution of the promise
|
56
|
+
* is an indication that the job was put in the queue, not that it has
|
57
|
+
* completed.
|
58
|
+
*
|
59
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
60
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
61
|
+
* before making assertions.
|
62
|
+
*
|
63
|
+
* @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
|
64
|
+
* @param methodName - the name of the static method you wish to run in the background
|
65
|
+
* @param args - a variadic list of arguments to be sent to your method
|
66
|
+
*/
|
9
67
|
static backgroundWithDelay<T, MethodName extends PsychicBackgroundedModelStaticMethods<T & typeof BaseBackgroundedModel>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, delaySeconds: number, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
68
|
+
/**
|
69
|
+
* types composed by psychic must be provided, since psychic-workers leverages
|
70
|
+
* the sync command in psychic to read your backgroundable services and extract
|
71
|
+
* metadata, which can be used to help provide types for the underlying methods
|
72
|
+
* in psychic-workers.
|
73
|
+
*/
|
10
74
|
get psychicTypes(): any;
|
75
|
+
/**
|
76
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
77
|
+
* sending in the provided args.
|
78
|
+
*
|
79
|
+
* ```ts
|
80
|
+
* const user = await User.lastOrFail()
|
81
|
+
* await user.background('myMethod', 'abc', 123)
|
82
|
+
* ```
|
83
|
+
* though calling background must be awaited, the resolution of the promise
|
84
|
+
* is an indication that the job was put in the queue, not that it has
|
85
|
+
* completed.
|
86
|
+
*
|
87
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
88
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
89
|
+
* before making assertions.
|
90
|
+
*
|
91
|
+
* @param methodName - the name of the static method you wish to run in the background
|
92
|
+
* @param args - a variadic list of arguments to be sent to your method
|
93
|
+
*/
|
11
94
|
background<T, MethodName extends PsychicBackgroundedServiceInstanceMethods<T & BaseBackgroundedModel>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
95
|
+
/**
|
96
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
97
|
+
* sending in the provided args, including a delay in seconds, which
|
98
|
+
* can be used to hold off the job for a certain amount of time after
|
99
|
+
* it is entered into the queue.
|
100
|
+
*
|
101
|
+
* ```ts
|
102
|
+
* const user = await User.lastOrFail()
|
103
|
+
* await user.backgroundWithDelay('myMethod', 'abc', 123)
|
104
|
+
* ```
|
105
|
+
* though calling background must be awaited, the resolution of the promise
|
106
|
+
* is an indication that the job was put in the queue, not that it has
|
107
|
+
* completed.
|
108
|
+
*
|
109
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
110
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
111
|
+
* before making assertions.
|
112
|
+
*
|
113
|
+
* @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
|
114
|
+
* @param methodName - the name of the static method you wish to run in the background
|
115
|
+
* @param args - a variadic list of arguments to be sent to your method
|
116
|
+
*/
|
12
117
|
backgroundWithDelay<T, MethodName extends PsychicBackgroundedServiceInstanceMethods<T & BaseBackgroundedModel>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, delaySeconds: number, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
13
118
|
}
|
14
119
|
export type PsychicBackgroundedModelStaticMethods<T extends typeof BaseBackgroundedModel> = Exclude<FunctionPropertyNames<Required<T>>, FunctionPropertyNames<typeof BaseBackgroundedModel>>;
|
@@ -1,13 +1,84 @@
|
|
1
1
|
import { Job } from 'bullmq';
|
2
|
-
import { BackgroundJobConfig } from '
|
3
|
-
import { FunctionPropertyNames } from '
|
2
|
+
import { BackgroundJobConfig } from '../types/background.js';
|
3
|
+
import { FunctionPropertyNames } from '../types/utils.js';
|
4
4
|
export default class BaseBackgroundedService {
|
5
|
+
/**
|
6
|
+
* A getter meant to be overridden in child classes. This does
|
7
|
+
* not have to be explicitly provided, but if so, it would allow
|
8
|
+
* you to override the default behavior of anything backgrounded
|
9
|
+
* by this service, such as the priority or workstream.
|
10
|
+
*
|
11
|
+
* @returns {object} config - the background job config
|
12
|
+
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
13
|
+
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
14
|
+
* @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
15
|
+
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
16
|
+
*/
|
5
17
|
static get backgroundJobConfig(): BackgroundJobConfig<BaseBackgroundedService>;
|
18
|
+
/**
|
19
|
+
* @internal
|
20
|
+
*
|
21
|
+
* Returns a unique global name for the given service.
|
22
|
+
*
|
23
|
+
* @returns A string representing a unique key for this service
|
24
|
+
*/
|
6
25
|
static get globalName(): string;
|
26
|
+
/**
|
27
|
+
* @internal
|
28
|
+
*
|
29
|
+
* Used by PsychicApplicationWorkers during the load process
|
30
|
+
* for services to assign unique global names to each service
|
31
|
+
* based on the file name of that model.
|
32
|
+
*/
|
7
33
|
static setGlobalName(globalName: string): void;
|
8
34
|
private static _globalName;
|
35
|
+
/**
|
36
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
37
|
+
* sending in the provided args.
|
38
|
+
*
|
39
|
+
* ```ts
|
40
|
+
* await MyBackgroundableClass.background('myMethod', 'abc', 123)
|
41
|
+
* ```
|
42
|
+
* though calling background must be awaited, the resolution of the promise
|
43
|
+
* is an indication that the job was put in the queue, not that it has
|
44
|
+
* completed.
|
45
|
+
*
|
46
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
47
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
48
|
+
* before making assertions.
|
49
|
+
*
|
50
|
+
* @param methodName - the name of the static method you wish to run in the background
|
51
|
+
* @param args - a variadic list of arguments to be sent to your method
|
52
|
+
*/
|
9
53
|
static background<T, MethodName extends PsychicBackgroundedServiceStaticMethods<T & typeof BaseBackgroundedService>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
54
|
+
/**
|
55
|
+
* runs the specified method in a background queue, driven by BullMQ,
|
56
|
+
* sending in the provided args, including a delay in seconds, which
|
57
|
+
* can be used to hold off the job for a certain amount of time after
|
58
|
+
* it is entered into the queue.
|
59
|
+
*
|
60
|
+
* ```ts
|
61
|
+
* await MyBackgroundableClass.backgroundWithDelay('myMethod', 'abc', 123)
|
62
|
+
* ```
|
63
|
+
* though calling background must be awaited, the resolution of the promise
|
64
|
+
* is an indication that the job was put in the queue, not that it has
|
65
|
+
* completed.
|
66
|
+
*
|
67
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
68
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
69
|
+
* before making assertions.
|
70
|
+
*
|
71
|
+
* @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
|
72
|
+
* @param methodName - the name of the static method you wish to run in the background
|
73
|
+
* @param args - a variadic list of arguments to be sent to your method
|
74
|
+
*/
|
10
75
|
static backgroundWithDelay<T, MethodName extends PsychicBackgroundedServiceStaticMethods<T & typeof BaseBackgroundedService>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends BackgroundableMethodArgs<MethodFunc>>(this: T, delaySeconds: number, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
76
|
+
/**
|
77
|
+
* types composed by psychic must be provided, since psychic-workers leverages
|
78
|
+
* the sync command in psychic to read your backgroundable services and extract
|
79
|
+
* metadata, which can be used to help provide types for the underlying methods
|
80
|
+
* in psychic-workers.
|
81
|
+
*/
|
11
82
|
get psychicTypes(): any;
|
12
83
|
}
|
13
84
|
export type PsychicBackgroundedServiceStaticMethods<T extends typeof BaseBackgroundedService> = Exclude<FunctionPropertyNames<Required<T>>, FunctionPropertyNames<typeof BaseBackgroundedService>>;
|
@@ -1,11 +1,62 @@
|
|
1
|
-
import { BackgroundJobConfig } from '
|
2
|
-
import { FunctionPropertyNames } from '
|
1
|
+
import { BackgroundJobConfig } from '../types/background.js';
|
2
|
+
import { FunctionPropertyNames } from '../types/utils.js';
|
3
3
|
export default class BaseScheduledService {
|
4
|
+
/**
|
5
|
+
* A getter meant to be overridden in child classes. This does
|
6
|
+
* not have to be explicitly provided, but if so, it would allow
|
7
|
+
* you to override the default behavior of anything backgrounded
|
8
|
+
* by this service, such as the priority or workstream.
|
9
|
+
*
|
10
|
+
* @returns {object} config - the background job config
|
11
|
+
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
12
|
+
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
13
|
+
* @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
14
|
+
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
15
|
+
*/
|
4
16
|
static get backgroundJobConfig(): BackgroundJobConfig<BaseScheduledService>;
|
17
|
+
/**
|
18
|
+
* @internal
|
19
|
+
*
|
20
|
+
* Returns a unique global name for the given service.
|
21
|
+
*
|
22
|
+
* @returns A string representing a unique key for this service
|
23
|
+
*/
|
5
24
|
static get globalName(): string;
|
25
|
+
/**
|
26
|
+
* @internal
|
27
|
+
*
|
28
|
+
* Used by PsychicApplicationWorkers during the load process
|
29
|
+
* for services to assign unique global names to each service
|
30
|
+
* based on the file name of that model.
|
31
|
+
*/
|
6
32
|
static setGlobalName(globalName: string): void;
|
7
33
|
private static _globalName;
|
34
|
+
/**
|
35
|
+
* Schedules a job to be run repeatedly at a certain cron interval
|
36
|
+
* sending in the provided args.
|
37
|
+
*
|
38
|
+
* ```ts
|
39
|
+
* await MySchedulableClass.schedule('0 * * * *', 'myHourlyMethod', 'abc', 123)
|
40
|
+
* ```
|
41
|
+
* though calling background must be awaited, the resolution of the promise
|
42
|
+
* is an indication that the job was put in the queue, not that it has
|
43
|
+
* completed.
|
44
|
+
*
|
45
|
+
* NOTE: in test environments, psychic will immediately invoke the underlying
|
46
|
+
* method, preventing you from needing to explicitly wait for queues to flush
|
47
|
+
* before making assertions.
|
48
|
+
*
|
49
|
+
* @param pattern - A cron string representing the time interval you wish this to run on
|
50
|
+
* @param methodName - the name of the static method you wish to run in the background
|
51
|
+
* @param args - a variadic list of arguments to be sent to your method
|
52
|
+
*/
|
8
53
|
static schedule<T, MethodName extends FunctionPropertyNames<Required<T>>, MethodFunc extends T[MethodName & keyof T], MethodArgs extends MethodFunc extends (...args: any) => any ? Parameters<MethodFunc> : never>(this: T, pattern: string, methodName: MethodName, ...args: MethodArgs): Promise<void>;
|
54
|
+
/**
|
55
|
+
* types composed by psychic must be provided, since psychic-workers leverages
|
56
|
+
* the sync command in psychic to read your backgroundable services and extract
|
57
|
+
* metadata, which can be used to help provide types for the underlying methods
|
58
|
+
* in psychic-workers.
|
59
|
+
*/
|
9
60
|
get psychicTypes(): any;
|
10
61
|
}
|
11
62
|
export type PsychicScheduledServiceStaticMethods<T extends typeof BaseScheduledService> = Exclude<FunctionPropertyNames<Required<T>>, FunctionPropertyNames<typeof BaseScheduledService>>;
|
@@ -1,69 +1,167 @@
|
|
1
|
-
import { Dream
|
1
|
+
import { Dream } from '@rvoh/dream';
|
2
2
|
import { Job, Queue, Worker } from 'bullmq';
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
export interface BackgroundJobData {
|
9
|
-
id?: IdType;
|
10
|
-
method?: string;
|
11
|
-
args: any;
|
12
|
-
filepath?: string;
|
13
|
-
importKey?: string;
|
14
|
-
globalName?: string;
|
15
|
-
}
|
3
|
+
import { BackgroundJobConfig, BackgroundJobData, BackgroundQueuePriority, JobTypes } from '../types/background.js';
|
4
|
+
/**
|
5
|
+
* the underlying class driving the `background` singleton,
|
6
|
+
* available as an import from `psychic-workers`.
|
7
|
+
*/
|
16
8
|
export declare class Background {
|
9
|
+
/**
|
10
|
+
* returns the default queue name for your app
|
11
|
+
*/
|
17
12
|
static get defaultQueueName(): string;
|
13
|
+
/**
|
14
|
+
* @internal
|
15
|
+
*
|
16
|
+
* returns the provided Worker class, or the Worker class from BullMQ
|
17
|
+
* if no override was provided. This is providable because BullMQ also
|
18
|
+
* offers a pro version, which requires you to provide custom classes.
|
19
|
+
*/
|
18
20
|
static get Worker(): typeof Worker;
|
21
|
+
/**
|
22
|
+
* @internal
|
23
|
+
*
|
24
|
+
* returns the provided Queue class, or the Queue class from BullMQ
|
25
|
+
* if no override was provided. This is providable because BullMQ also
|
26
|
+
* offers a pro version, which requires you to provide custom classes.
|
27
|
+
*/
|
19
28
|
static get Queue(): typeof Queue;
|
20
29
|
/**
|
30
|
+
* @internal
|
31
|
+
*
|
21
32
|
* Used when adding jobs to the default queue
|
22
33
|
*/
|
23
34
|
private defaultQueue;
|
24
35
|
/**
|
36
|
+
* @internal
|
37
|
+
*
|
25
38
|
* Used when adding jobs to the default transitional queue
|
26
39
|
*/
|
27
40
|
private defaultTransitionalQueue;
|
28
41
|
/**
|
42
|
+
* @internal
|
43
|
+
*
|
29
44
|
* Used when adding jobs to a named queue
|
30
45
|
*/
|
31
46
|
private namedQueues;
|
47
|
+
/**
|
48
|
+
* @internal
|
49
|
+
*
|
50
|
+
* Used when adding grouped jobs
|
51
|
+
*/
|
32
52
|
private groupNames;
|
53
|
+
/**
|
54
|
+
* @internal
|
55
|
+
*
|
56
|
+
* Used when adding workstreams
|
57
|
+
*/
|
33
58
|
private workstreamNames;
|
34
59
|
/**
|
60
|
+
* @internal
|
61
|
+
*
|
35
62
|
* Used when adding jobs to a named transitioanl queue
|
36
63
|
*/
|
37
64
|
private namedTransitionalQueues;
|
65
|
+
/**
|
66
|
+
* @internal
|
67
|
+
*
|
68
|
+
* All of the workers that are currently registered
|
69
|
+
*/
|
38
70
|
private _workers;
|
71
|
+
/**
|
72
|
+
* @internal
|
73
|
+
*
|
74
|
+
* All of the redis connections that are currently registered
|
75
|
+
*/
|
39
76
|
private redisConnections;
|
77
|
+
/**
|
78
|
+
* Establishes connection to BullMQ via redis
|
79
|
+
*/
|
40
80
|
connect({ activateWorkers, }?: {
|
41
81
|
activateWorkers?: boolean;
|
42
82
|
}): void;
|
83
|
+
/**
|
84
|
+
* Returns all the queues in your application
|
85
|
+
*/
|
43
86
|
get queues(): Queue[];
|
87
|
+
/**
|
88
|
+
* Returns all the workers in your application
|
89
|
+
*/
|
44
90
|
get workers(): Worker<any, any, string>[];
|
45
91
|
private shutdownAndExit;
|
92
|
+
/**
|
93
|
+
* Shuts down workers, closes all redis connections
|
94
|
+
*/
|
46
95
|
shutdown(): Promise<void>;
|
96
|
+
/**
|
97
|
+
* closes all redis connections for workers and queues
|
98
|
+
*/
|
47
99
|
closeAllRedisConnections(): Promise<void>;
|
100
|
+
/**
|
101
|
+
* @internal
|
102
|
+
*
|
103
|
+
* connects to BullMQ using workstream-based arguments
|
104
|
+
*/
|
48
105
|
private simpleConnect;
|
106
|
+
/**
|
107
|
+
* @internal
|
108
|
+
*
|
109
|
+
* connects to BullMQ using native BullMQ arguments
|
110
|
+
*/
|
49
111
|
private nativeBullMQConnect;
|
112
|
+
/**
|
113
|
+
* starts background workers
|
114
|
+
*/
|
50
115
|
work(): void;
|
51
|
-
|
116
|
+
/**
|
117
|
+
* adds the static method of a provided class to BullMQ
|
118
|
+
*
|
119
|
+
* @param ObjectClass - the class you wish to background
|
120
|
+
* @param method - the method you wish to background
|
121
|
+
* @param globalName - the globalName of the class you are processing
|
122
|
+
* @param args - (optional) a list of arguments to provide to your method when it is called
|
123
|
+
* @param delaySeconds - (optional) the number of seconds you wish to wait before allowing this job to process
|
124
|
+
* @param importKey - (optional) the import key for the class
|
125
|
+
* @param jobConfig - (optional) the background job config to use when backgrounding this method
|
126
|
+
*/
|
127
|
+
staticMethod(ObjectClass: Record<'name', string>, method: string, { globalName, delaySeconds, args, jobConfig, }: {
|
52
128
|
globalName: string;
|
129
|
+
args?: any[];
|
53
130
|
filepath?: string;
|
54
131
|
delaySeconds?: number;
|
55
132
|
importKey?: string;
|
56
|
-
args?: any[];
|
57
133
|
jobConfig?: BackgroundJobConfig<any>;
|
58
134
|
}): Promise<void>;
|
135
|
+
/**
|
136
|
+
* adds the static method of a provided class to BullMQ,
|
137
|
+
* to be scheduled to run at a specified cron pattern
|
138
|
+
*
|
139
|
+
* @param ObjectClass - the class you wish to background
|
140
|
+
* @param pattern - the cron string you wish to use to govern the scheduling for this job
|
141
|
+
* @param method - the method you wish to background
|
142
|
+
* @param globalName - the globalName of the class you are processing
|
143
|
+
* @param args - (optional) a list of arguments to provide to your method when it is called
|
144
|
+
* @param importKey - (optional) the import key for the class
|
145
|
+
* @param jobConfig - (optional) the background job config to use when backgrounding this method
|
146
|
+
*/
|
59
147
|
scheduledMethod(ObjectClass: Record<'name', string>, pattern: string, method: string, { globalName, args, jobConfig, }: {
|
60
148
|
globalName: string;
|
149
|
+
args?: any[];
|
61
150
|
filepath?: string;
|
62
151
|
importKey?: string;
|
63
|
-
args?: any[];
|
64
152
|
jobConfig?: BackgroundJobConfig<any>;
|
65
153
|
}): Promise<void>;
|
66
154
|
private queueInstance;
|
155
|
+
/**
|
156
|
+
* adds the instance method of a provided dream model to BullMQ
|
157
|
+
*
|
158
|
+
* @param modelInstance - the dream model instance you wish to background
|
159
|
+
* @param method - the method you wish to background
|
160
|
+
* @param globalName - the globalName of the class you are processing
|
161
|
+
* @param args - (optional) a list of arguments to provide to your method when it is called
|
162
|
+
* @param importKey - (optional) the import key for the class
|
163
|
+
* @param jobConfig - (optional) the background job config to use when backgrounding this method
|
164
|
+
*/
|
67
165
|
modelInstanceMethod(modelInstance: Dream, method: string, { delaySeconds, args, jobConfig, }: {
|
68
166
|
delaySeconds?: number;
|
69
167
|
importKey?: string;
|
@@ -86,16 +184,3 @@ export declare class Background {
|
|
86
184
|
declare const background: Background;
|
87
185
|
export default background;
|
88
186
|
export declare function stopBackgroundWorkers(): Promise<void>;
|
89
|
-
export type BackgroundQueuePriority = 'default' | 'urgent' | 'not_urgent' | 'last';
|
90
|
-
interface BaseBackgroundJobConfig {
|
91
|
-
priority?: BackgroundQueuePriority;
|
92
|
-
}
|
93
|
-
export interface WorkstreamBackgroundJobConfig<T extends BaseScheduledService | BaseBackgroundedService> extends BaseBackgroundJobConfig {
|
94
|
-
workstream?: T['psychicTypes']['workstreamNames'][number];
|
95
|
-
}
|
96
|
-
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 {
|
97
|
-
groupId?: GroupId;
|
98
|
-
queue?: Queue;
|
99
|
-
}
|
100
|
-
export type BackgroundJobConfig<T extends BaseScheduledService | BaseBackgroundedService> = Either<WorkstreamBackgroundJobConfig<T>, QueueBackgroundJobConfig<T>>;
|
101
|
-
export type PsychicBackgroundOptions = (PsychicBackgroundSimpleOptions & Partial<Record<Exclude<keyof PsychicBackgroundNativeBullMQOptions, keyof PsychicBackgroundSimpleOptions>, never>>) | (PsychicBackgroundNativeBullMQOptions & Partial<Record<Exclude<keyof PsychicBackgroundSimpleOptions, keyof PsychicBackgroundNativeBullMQOptions>, never>>);
|
@@ -1,7 +1,8 @@
|
|
1
|
-
export { default as background, Background,
|
1
|
+
export { default as background, Background, stopBackgroundWorkers } from './background/index.js';
|
2
|
+
export { type BackgroundJobConfig, type BackgroundQueuePriority } from './types/background.js';
|
2
3
|
export { default as BaseBackgroundedModel } from './background/BaseBackgroundedModel.js';
|
3
4
|
export { default as BaseBackgroundedService } from './background/BaseBackgroundedService.js';
|
4
5
|
export { default as BaseScheduledService } from './background/BaseScheduledService.js';
|
5
|
-
export {
|
6
|
+
export { default as PsychicAppWorkers, type BullMQNativeWorkerOptions, type PsychicBackgroundNativeBullMQOptions, type PsychicBackgroundSimpleOptions, type PsychicBackgroundWorkstreamOptions, type QueueOptionsWithConnectionInstance, type RedisOrRedisClusterConnection, type TransitionalPsychicBackgroundSimpleOptions, } from './psychic-app-workers/index.js';
|
6
7
|
export { default as NoQueueForSpecifiedQueueName } from './error/background/NoQueueForSpecifiedQueueName.js';
|
7
8
|
export { default as NoQueueForSpecifiedWorkstream } from './error/background/NoQueueForSpecifiedWorkstream.js';
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import PsychicAppWorkers from './index.js';
|
2
|
+
export declare function cachePsychicWorkersApp(psychicWorkersApp: PsychicAppWorkers): void;
|
3
|
+
export declare function getCachedPsychicWorkersApp(): PsychicAppWorkers | undefined;
|
4
|
+
export declare function getCachedPsychicWorkersAppOrFail(): PsychicAppWorkers;
|