@devmoods/express-extras 0.35.1 → 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 +6 -6
- 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
|
@@ -4,25 +4,25 @@ export interface JobsOptions {
|
|
|
4
4
|
faktory?: typeof create;
|
|
5
5
|
}
|
|
6
6
|
export type JobContext = Record<string, any>;
|
|
7
|
-
export declare function createJobs({ queueName, faktory: createFactory, }: JobsOptions): {
|
|
8
|
-
job: <K extends Record<string, any>, T extends (input: K, ctx:
|
|
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) => {
|
|
9
9
|
$job: boolean;
|
|
10
10
|
jobName: string;
|
|
11
11
|
readonly executionCount: number;
|
|
12
12
|
delay(args?: Parameters<T>[0]): Promise<`${string}-${string}-${string}-${string}-${string}`>;
|
|
13
|
-
execute(args: Parameters<T>[0], context:
|
|
13
|
+
execute(args: Parameters<T>[0], context: GlobalContext): Promise<any>;
|
|
14
14
|
};
|
|
15
15
|
queueName: string;
|
|
16
16
|
};
|
|
17
17
|
export type Job = ReturnType<ReturnType<typeof createJobs>['job']>;
|
|
18
|
-
export interface FaktoryWorkerOptions {
|
|
18
|
+
export interface FaktoryWorkerOptions<GlobalContext> {
|
|
19
19
|
jobs: unknown[];
|
|
20
20
|
queueName: string;
|
|
21
21
|
faktory?: typeof create;
|
|
22
|
-
jobContext?:
|
|
22
|
+
jobContext?: GlobalContext;
|
|
23
23
|
}
|
|
24
24
|
export declare function useJobId(): string | undefined;
|
|
25
|
-
export declare function createFaktoryWorker({ queueName, jobs, faktory: createFactory, jobContext, }: FaktoryWorkerOptions): Promise<{
|
|
25
|
+
export declare function createFaktoryWorker<GlobalContext>({ queueName, jobs, faktory: createFactory, jobContext, }: FaktoryWorkerOptions<GlobalContext>): Promise<{
|
|
26
26
|
start(workerOptions?: WorkerOptions): Promise<void>;
|
|
27
27
|
stop(): Promise<void>;
|
|
28
28
|
}>;
|
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
|
}
|