@rvoh/psychic-workers 0.3.0 → 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 (39) 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 +134 -65
  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/psychic-app-workers/index.js +1 -1
  11. package/dist/cjs/src/types/utils.js +2 -0
  12. package/dist/esm/src/background/BaseBackgroundedModel.js +105 -0
  13. package/dist/esm/src/background/BaseBackgroundedService.js +71 -0
  14. package/dist/esm/src/background/BaseScheduledService.js +51 -0
  15. package/dist/esm/src/background/helpers/nameToRedisQueueName.js +7 -0
  16. package/dist/esm/src/background/index.js +124 -55
  17. package/dist/esm/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +8 -0
  18. package/dist/esm/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +14 -0
  19. package/dist/esm/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +8 -0
  20. package/dist/esm/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +13 -0
  21. package/dist/esm/src/index.js +1 -1
  22. package/dist/esm/src/psychic-app-workers/index.js +1 -1
  23. package/dist/esm/src/types/utils.js +1 -0
  24. package/dist/types/src/background/BaseBackgroundedModel.d.ts +108 -3
  25. package/dist/types/src/background/BaseBackgroundedService.d.ts +73 -2
  26. package/dist/types/src/background/BaseScheduledService.d.ts +53 -2
  27. package/dist/types/src/background/helpers/nameToRedisQueueName.d.ts +2 -0
  28. package/dist/types/src/background/index.d.ts +115 -30
  29. package/dist/types/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.d.ts +3 -0
  30. package/dist/types/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.d.ts +5 -0
  31. package/dist/types/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +3 -0
  32. package/dist/types/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +5 -0
  33. package/dist/types/src/index.d.ts +2 -1
  34. package/dist/types/src/psychic-app-workers/index.d.ts +2 -2
  35. package/dist/types/src/types/background.d.ts +47 -0
  36. package/package.json +2 -2
  37. /package/dist/cjs/src/{background/types.js → types/background.js} +0 -0
  38. /package/dist/esm/src/{background/types.js → types/background.js} +0 -0
  39. /package/dist/types/src/{background/types.d.ts → types/utils.d.ts} +0 -0
@@ -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
- get backgroundJobConfig(): BackgroundJobConfig<BaseBackgroundedModel>;
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 './index.js';
3
- import { FunctionPropertyNames } from './types.js';
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 './index.js';
2
- import { FunctionPropertyNames } from './types.js';
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>>;
@@ -0,0 +1,2 @@
1
+ import { Redis, Cluster } from 'ioredis';
2
+ export default function nameToRedisQueueName(queueName: string, redis: Redis | Cluster): string;
@@ -1,69 +1,167 @@
1
- import { Dream, IdType } from '@rvoh/dream';
1
+ import { Dream } from '@rvoh/dream';
2
2
  import { Job, Queue, Worker } from 'bullmq';
3
- import { PsychicBackgroundNativeBullMQOptions, PsychicBackgroundSimpleOptions } from '../psychic-app-workers/index.js';
4
- import BaseBackgroundedService from './BaseBackgroundedService.js';
5
- import BaseScheduledService from './BaseScheduledService.js';
6
- import { Either } from './types.js';
7
- type JobTypes = 'BackgroundJobQueueFunctionJob' | 'BackgroundJobQueueStaticJob' | 'BackgroundJobQueueModelInstanceJob';
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
- staticMethod(ObjectClass: Record<'name', string>, method: string, { delaySeconds, globalName, args, jobConfig, }: {
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>>);
@@ -0,0 +1,3 @@
1
+ export default class ActivatingBackgroundWorkersWithoutDefaultWorkerConnection extends Error {
2
+ get message(): string;
3
+ }
@@ -0,0 +1,5 @@
1
+ export default class ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection extends Error {
2
+ private queueName;
3
+ constructor(queueName: string);
4
+ get message(): string;
5
+ }
@@ -0,0 +1,3 @@
1
+ export default class DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection extends Error {
2
+ get message(): string;
3
+ }
@@ -0,0 +1,5 @@
1
+ export default class NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection extends Error {
2
+ private queueName;
3
+ constructor(queueName: string);
4
+ get message(): string;
5
+ }
@@ -1,4 +1,5 @@
1
- export { default as background, Background, stopBackgroundWorkers, type BackgroundJobConfig, type BackgroundQueuePriority, } from './background/index.js';
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';
@@ -1,7 +1,7 @@
1
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';
4
+ import { PsychicBackgroundOptions } from '../types/background.js';
5
5
  export default class PsychicAppWorkers {
6
6
  static init(psychicApp: PsychicApp, cb: (app: PsychicAppWorkers) => void | Promise<void>): Promise<PsychicAppWorkers>;
7
7
  /**
@@ -21,7 +21,7 @@ export default class PsychicAppWorkers {
21
21
  private _hooks;
22
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 PsychicWorkersAppOption>(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[];
@@ -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.3.0",
5
+ "version": "0.3.1",
6
6
  "author": "RVO Health",
7
7
  "repository": {
8
8
  "type": "git",
@@ -73,4 +73,4 @@
73
73
  "vitest": "^3.1.1"
74
74
  },
75
75
  "packageManager": "yarn@4.7.0"
76
- }
76
+ }