@devmoods/express-extras 0.35.0 → 0.36.0
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/faktory.d.ts +9 -12
- package/dist/faktory.js +3 -2
- package/dist/listen.d.ts +1 -1
- package/dist/listen.js +12 -4
- package/dist/redis.js +5 -3
- package/package.json +1 -1
package/dist/faktory.d.ts
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
1
|
-
import { create } from 'faktory-worker';
|
|
1
|
+
import { create, type WorkerOptions } from 'faktory-worker';
|
|
2
2
|
export interface JobsOptions {
|
|
3
3
|
queueName: string;
|
|
4
4
|
faktory?: typeof create;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare function createJobs({ queueName, faktory: createFactory, }: JobsOptions): {
|
|
10
|
-
job: <K extends Record<string, any>, T extends (input: K, ctx: JobContext) => undefined | Promise<any>>(fn: T) => {
|
|
6
|
+
export type JobContext = Record<string, any>;
|
|
7
|
+
export declare function createJobs<GlobalContext extends JobContext>({ queueName, faktory: createFactory, }: JobsOptions): {
|
|
8
|
+
job: <K extends Record<string, any>, T extends (input: K, ctx: GlobalContext) => undefined | Promise<any>>(fn: T) => {
|
|
11
9
|
$job: boolean;
|
|
12
10
|
jobName: string;
|
|
13
11
|
readonly executionCount: number;
|
|
14
12
|
delay(args?: Parameters<T>[0]): Promise<`${string}-${string}-${string}-${string}-${string}`>;
|
|
15
|
-
execute(args: Parameters<T>[0], context:
|
|
13
|
+
execute(args: Parameters<T>[0], context: GlobalContext): Promise<any>;
|
|
16
14
|
};
|
|
17
15
|
queueName: string;
|
|
18
16
|
};
|
|
19
17
|
export type Job = ReturnType<ReturnType<typeof createJobs>['job']>;
|
|
20
|
-
export interface FaktoryWorkerOptions {
|
|
18
|
+
export interface FaktoryWorkerOptions<GlobalContext> {
|
|
21
19
|
jobs: unknown[];
|
|
22
20
|
queueName: string;
|
|
23
21
|
faktory?: typeof create;
|
|
24
|
-
jobContext?:
|
|
22
|
+
jobContext?: GlobalContext;
|
|
25
23
|
}
|
|
26
24
|
export declare function useJobId(): string | undefined;
|
|
27
|
-
export declare function createFaktoryWorker({ queueName, jobs, faktory: createFactory, jobContext, }: FaktoryWorkerOptions): Promise<{
|
|
28
|
-
start(): Promise<void>;
|
|
25
|
+
export declare function createFaktoryWorker<GlobalContext>({ queueName, jobs, faktory: createFactory, jobContext, }: FaktoryWorkerOptions<GlobalContext>): Promise<{
|
|
26
|
+
start(workerOptions?: WorkerOptions): Promise<void>;
|
|
29
27
|
stop(): Promise<void>;
|
|
30
28
|
}>;
|
|
31
|
-
export {};
|
package/dist/faktory.js
CHANGED
|
@@ -61,7 +61,7 @@ export function useJobId() {
|
|
|
61
61
|
return store.get(JOB_ID);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
export async function createFaktoryWorker({ queueName, jobs, faktory: createFactory = create, jobContext
|
|
64
|
+
export async function createFaktoryWorker({ queueName, jobs, faktory: createFactory = create, jobContext, }) {
|
|
65
65
|
const faktory = createFactory();
|
|
66
66
|
const filter = (j) => !!Object.prototype.hasOwnProperty.call(j, '$job');
|
|
67
67
|
const availableJobs = jobs.filter(filter);
|
|
@@ -71,7 +71,7 @@ export async function createFaktoryWorker({ queueName, jobs, faktory: createFact
|
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
return {
|
|
74
|
-
async start() {
|
|
74
|
+
async start(workerOptions = {}) {
|
|
75
75
|
try {
|
|
76
76
|
logger.info('faktory started', {
|
|
77
77
|
queueName,
|
|
@@ -96,6 +96,7 @@ export async function createFaktoryWorker({ queueName, jobs, faktory: createFact
|
|
|
96
96
|
});
|
|
97
97
|
await faktory.work({
|
|
98
98
|
queues: [queueName],
|
|
99
|
+
...workerOptions,
|
|
99
100
|
});
|
|
100
101
|
}
|
|
101
102
|
catch (error) {
|
package/dist/listen.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ interface StoppableServer extends Server {
|
|
|
7
7
|
export declare function stoppable(server: Server, grace?: number): StoppableServer;
|
|
8
8
|
interface ExpressServerOptions {
|
|
9
9
|
onShutdown?(): Promise<void> | void;
|
|
10
|
-
healthCheck?(): Promise<unknown>;
|
|
10
|
+
healthCheck?(type?: 'live' | 'ready'): Promise<unknown>;
|
|
11
11
|
}
|
|
12
12
|
export declare function createExpressServer(app: Application, options?: ExpressServerOptions): {
|
|
13
13
|
start(): Promise<void>;
|
package/dist/listen.js
CHANGED
|
@@ -6,6 +6,7 @@ import {} from 'express';
|
|
|
6
6
|
import { getLogger } from './logger.js';
|
|
7
7
|
import { before, route } from './middleware.js';
|
|
8
8
|
import { envPort, readEnv } from './utils.js';
|
|
9
|
+
import { validateQuery } from './validate.js';
|
|
9
10
|
const logger = getLogger('http');
|
|
10
11
|
const READINESS_PERIOD = readEnv.number('READINESS_PERIOD_MS', 0);
|
|
11
12
|
const GRACE_PERIOD = readEnv.number('GRACE_PERIOD_MS', 20000);
|
|
@@ -96,16 +97,23 @@ export function createExpressServer(app, options = {}) {
|
|
|
96
97
|
process.on(signal, onSignal);
|
|
97
98
|
}
|
|
98
99
|
if (!app.get('__healthAttached')) {
|
|
99
|
-
app.get('/health', before(),
|
|
100
|
+
app.get('/health', before(), validateQuery({
|
|
101
|
+
type: 'object',
|
|
102
|
+
properties: {
|
|
103
|
+
type: { type: 'string', enum: ['live', 'ready'] },
|
|
104
|
+
},
|
|
105
|
+
}), route(async (req) => {
|
|
100
106
|
const timestamp = new Date().toISOString();
|
|
101
107
|
try {
|
|
102
108
|
if (stopping) {
|
|
103
109
|
throw new Error();
|
|
104
110
|
}
|
|
105
|
-
const
|
|
106
|
-
|
|
111
|
+
const type = req.query.type;
|
|
112
|
+
const meta = await options.healthCheck?.(type);
|
|
113
|
+
return { status: 'ok', type, meta, timestamp };
|
|
107
114
|
}
|
|
108
|
-
catch {
|
|
115
|
+
catch (error) {
|
|
116
|
+
logger.error(error, 'Failed health check');
|
|
109
117
|
return [503, { status: 'error', timestamp }];
|
|
110
118
|
}
|
|
111
119
|
}));
|
package/dist/redis.js
CHANGED
|
@@ -2,8 +2,10 @@ import { createClient } from 'redis';
|
|
|
2
2
|
import { readEnv } from './utils.js';
|
|
3
3
|
export function createRedis({ url = readEnv('REDIS_URL', ''), onError, } = {}) {
|
|
4
4
|
const client = createClient({ url });
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
// Always attach a listener even if
|
|
6
|
+
// onError is not provided.
|
|
7
|
+
client.on('error', (error) => {
|
|
8
|
+
onError?.(error);
|
|
9
|
+
});
|
|
8
10
|
return client;
|
|
9
11
|
}
|