@shivaedev/effect-pg-boss 0.5.0 → 0.6.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/CHANGELOG.md +20 -0
- package/README.md +61 -12
- package/dist/server/client.d.ts +7 -1
- package/dist/server/client.d.ts.map +1 -1
- package/dist/server/client.js.map +1 -1
- package/dist/server/definition.d.ts +5 -5
- package/dist/server/definition.d.ts.map +1 -1
- package/dist/server/error.d.ts +10 -1
- package/dist/server/error.d.ts.map +1 -1
- package/dist/server/error.js +5 -0
- package/dist/server/error.js.map +1 -1
- package/dist/server/health.d.ts +6 -0
- package/dist/server/health.d.ts.map +1 -1
- package/dist/server/health.js.map +1 -1
- package/dist/server/internal/countsFor.d.ts +5 -0
- package/dist/server/internal/countsFor.d.ts.map +1 -0
- package/dist/server/internal/countsFor.js +27 -0
- package/dist/server/internal/countsFor.js.map +1 -0
- package/dist/server/internal/ensureQueue.d.ts +4 -0
- package/dist/server/internal/ensureQueue.d.ts.map +1 -0
- package/dist/server/internal/ensureQueue.js +27 -0
- package/dist/server/internal/ensureQueue.js.map +1 -0
- package/dist/server/internal/jobControl.d.ts +5 -0
- package/dist/server/internal/jobControl.d.ts.map +1 -0
- package/dist/server/internal/jobControl.js +35 -0
- package/dist/server/internal/jobControl.js.map +1 -0
- package/dist/server/internal/register.d.ts +2 -1
- package/dist/server/internal/register.d.ts.map +1 -1
- package/dist/server/internal/register.js +9 -25
- package/dist/server/internal/register.js.map +1 -1
- package/dist/server/internal/service.d.ts +1 -1
- package/dist/server/internal/service.d.ts.map +1 -1
- package/dist/server/internal/service.js +11 -5
- package/dist/server/internal/service.js.map +1 -1
- package/dist/server/internal/tracing.d.ts +6 -0
- package/dist/server/internal/tracing.d.ts.map +1 -0
- package/dist/server/internal/tracing.js +37 -0
- package/dist/server/internal/tracing.js.map +1 -0
- package/dist/server/service.d.ts +8 -5
- package/dist/server/service.d.ts.map +1 -1
- package/dist/server/service.js +1 -1
- package/dist/server/service.js.map +1 -1
- package/docs/north-star.md +1 -1
- package/docs/roadmap.md +9 -3
- package/package.json +2 -2
- package/src/server/client.ts +17 -1
- package/src/server/definition.ts +5 -5
- package/src/server/error.ts +10 -1
- package/src/server/health.ts +7 -0
- package/src/server/internal/countsFor.ts +33 -0
- package/src/server/internal/ensureQueue.ts +29 -0
- package/src/server/internal/jobControl.ts +47 -0
- package/src/server/internal/register.ts +10 -26
- package/src/server/internal/service.ts +17 -6
- package/src/server/internal/tracing.ts +44 -0
- package/src/server/service.ts +13 -7
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Effect, Option, Schema, Tracer } from "effect";
|
|
2
|
+
import type { Job } from "pg-boss";
|
|
3
|
+
|
|
4
|
+
const TracedPayload = Schema.Struct({
|
|
5
|
+
_tag: Schema.Literal("@shivaedev/effect-pg-boss/TracedPayload"),
|
|
6
|
+
payload: Schema.Unknown,
|
|
7
|
+
span: Schema.Struct({ sampled: Schema.Boolean, spanId: Schema.String, traceId: Schema.String }),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const isTracedPayload = Schema.is(TracedPayload);
|
|
11
|
+
|
|
12
|
+
export function capturePayload(payload: object): Effect.Effect<object> {
|
|
13
|
+
return Effect.map(Effect.option(Effect.currentParentSpan), (span) =>
|
|
14
|
+
Option.isNone(span)
|
|
15
|
+
? payload
|
|
16
|
+
: ({
|
|
17
|
+
_tag: "@shivaedev/effect-pg-boss/TracedPayload",
|
|
18
|
+
payload,
|
|
19
|
+
span: { sampled: span.value.sampled, spanId: span.value.spanId, traceId: span.value.traceId },
|
|
20
|
+
} satisfies typeof TracedPayload.Type),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function payloadOf(data: unknown): unknown {
|
|
25
|
+
return isTracedPayload(data) ? data.payload : data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function traceJob<A, E, R>(effect: Effect.Effect<A, E, R>, job: Job<unknown>, queue: string) {
|
|
29
|
+
return Effect.withSpan(effect, queue, {
|
|
30
|
+
attributes: {
|
|
31
|
+
"job.attempt": job.retryCount + 1,
|
|
32
|
+
"job.id": job.id,
|
|
33
|
+
"job.name": job.name,
|
|
34
|
+
"job.queue": queue,
|
|
35
|
+
"job.retry_count": job.retryCount,
|
|
36
|
+
"messaging.destination.name": queue,
|
|
37
|
+
"messaging.message.id": job.id,
|
|
38
|
+
"messaging.system": "pg-boss",
|
|
39
|
+
},
|
|
40
|
+
kind: "consumer",
|
|
41
|
+
links: isTracedPayload(job.data) ? [{ attributes: {}, span: Tracer.externalSpan(job.data.span) }] : [],
|
|
42
|
+
root: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
package/src/server/service.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import { Context, Effect, Layer, type Option, type Schema } from "effect";
|
|
2
|
-
import type { ConstructorOptions, SendOptions, StopOptions } from "pg-boss";
|
|
2
|
+
import type { ConstructorOptions, JobWithMetadata, SendOptions, StopOptions } from "pg-boss";
|
|
3
3
|
import type { PgBossClientFactory } from "#client.ts";
|
|
4
|
+
import type { JobPayloadSchema, JobRegistration, QueueDefinition, RegistrationRequirements } from "#definition.ts";
|
|
5
|
+
import type { PgBossError, PgBossPayloadError, PgBossQueueOptionsError } from "#error.ts";
|
|
6
|
+
import type { JobsHealth, QueueCounts } from "#health.ts";
|
|
4
7
|
import { acquireClient, releaseClient } from "#internal/lifecycle.ts";
|
|
5
8
|
import { registerJobs, registrationName } from "#internal/register.ts";
|
|
6
9
|
import { makeService } from "#internal/service.ts";
|
|
7
|
-
import type { JobPayloadSchema, JobRegistration, QueueDefinition, RegistrationRequirements } from "./definition.ts";
|
|
8
|
-
import type { PgBossError, PgBossPayloadError } from "./error.ts";
|
|
9
|
-
import type { JobsHealth } from "./health.ts";
|
|
10
10
|
|
|
11
11
|
export interface PgBossService {
|
|
12
|
+
readonly counts: (queue: QueueDefinition) => Effect.Effect<QueueCounts, PgBossError>;
|
|
12
13
|
readonly enqueue: <const TName extends string, const TPayload extends JobPayloadSchema>(
|
|
13
14
|
queue: QueueDefinition<TName, TPayload>,
|
|
14
15
|
payload: Schema.Schema.Type<TPayload>,
|
|
15
16
|
options?: SendOptions,
|
|
16
17
|
) => Effect.Effect<Option.Option<string>, PgBossError | PgBossPayloadError, TPayload["EncodingServices"]>;
|
|
18
|
+
readonly getJob: <const TName extends string, const TPayload extends JobPayloadSchema>(
|
|
19
|
+
queue: QueueDefinition<TName, TPayload>,
|
|
20
|
+
id: string,
|
|
21
|
+
) => Effect.Effect<Option.Option<JobWithMetadata<TPayload["Type"]>>, PgBossError | PgBossPayloadError, TPayload["DecodingServices"]>;
|
|
17
22
|
readonly health: Effect.Effect<JobsHealth, PgBossError>;
|
|
23
|
+
readonly retry: (queue: QueueDefinition, id: string) => Effect.Effect<void, PgBossError>;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
export type PgBossLayerOptions<TRegistrations extends readonly JobRegistration[], TErrorRequirements> = ConstructorOptions & {
|
|
@@ -29,7 +35,7 @@ export type PgBossLayerOptions<TRegistrations extends readonly JobRegistration[]
|
|
|
29
35
|
export interface PgBossDefinition<TIdentifier extends string = string> extends Context.Service<TIdentifier, PgBossService> {
|
|
30
36
|
readonly layer: <const TRegistrations extends readonly JobRegistration[], TErrorRequirements = never>(
|
|
31
37
|
options: PgBossLayerOptions<TRegistrations, TErrorRequirements>,
|
|
32
|
-
) => Layer.Layer<TIdentifier, PgBossError, RegistrationRequirements<TRegistrations> | TErrorRequirements>;
|
|
38
|
+
) => Layer.Layer<TIdentifier, PgBossError | PgBossQueueOptionsError, RegistrationRequirements<TRegistrations> | TErrorRequirements>;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
function logClientError(error: Error): Effect.Effect<void> {
|
|
@@ -41,7 +47,7 @@ export const makePgBoss = <const TIdentifier extends string>(identifier: TIdenti
|
|
|
41
47
|
|
|
42
48
|
const layer = <const TRegistrations extends readonly JobRegistration[], TErrorRequirements = never>(
|
|
43
49
|
options: PgBossLayerOptions<TRegistrations, TErrorRequirements>,
|
|
44
|
-
): Layer.Layer<TIdentifier, PgBossError, RegistrationRequirements<TRegistrations> | TErrorRequirements> => {
|
|
50
|
+
): Layer.Layer<TIdentifier, PgBossError | PgBossQueueOptionsError, RegistrationRequirements<TRegistrations> | TErrorRequirements> => {
|
|
45
51
|
type Requirements = RegistrationRequirements<TRegistrations> | TErrorRequirements;
|
|
46
52
|
const names = options.jobs.map(registrationName);
|
|
47
53
|
const { clientFactory, clientCacheKey, jobs, onError, stop, ...constructorOptions } = options;
|
|
@@ -76,7 +82,7 @@ export const makePgBoss = <const TIdentifier extends string>(identifier: TIdenti
|
|
|
76
82
|
|
|
77
83
|
return Layer.effect(
|
|
78
84
|
Service,
|
|
79
|
-
Effect.map(acquire, ({ acquired }) => makeService(acquired.client, names)),
|
|
85
|
+
Effect.map(acquire, ({ acquired }) => makeService(acquired.client, names, constructorOptions.schema)),
|
|
80
86
|
);
|
|
81
87
|
};
|
|
82
88
|
|