Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@durable-effect/core",
"version": "0.0.1-next.14",
"version": "0.0.1-next.15",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ export const InternalJobExecutedEventSchema = Schema.Struct({
durationMs: Schema.Number,
/** Current retry attempt (1 = first attempt) */
attempt: Schema.Number,
/** State snapshot before execution (for tracking/debugging) */
preExecutionState: Schema.optional(Schema.Unknown),
});
export type InternalJobExecutedEvent = Schema.Schema.Type<typeof InternalJobExecutedEventSchema>;

Expand All @@ -604,6 +606,8 @@ export const InternalJobFailedEventSchema = Schema.Struct({
attempt: Schema.Number,
/** Whether a retry will be attempted */
willRetry: Schema.Boolean,
/** State snapshot before execution (for tracking/debugging) */
preExecutionState: Schema.optional(Schema.Unknown),
});
export type InternalJobFailedEvent = Schema.Schema.Type<typeof InternalJobFailedEventSchema>;

Expand Down Expand Up @@ -728,6 +732,7 @@ export const JobExecutedEventSchema = Schema.Struct({
runCount: Schema.Number,
durationMs: Schema.Number,
attempt: Schema.Number,
preExecutionState: Schema.optional(Schema.Unknown),
});
export type JobExecutedEvent = Schema.Schema.Type<typeof JobExecutedEventSchema>;

Expand All @@ -738,6 +743,7 @@ export const JobFailedEventSchema = Schema.Struct({
runCount: Schema.Number,
attempt: Schema.Number,
willRetry: Schema.Boolean,
preExecutionState: Schema.optional(Schema.Unknown),
});
export type JobFailedEvent = Schema.Schema.Type<typeof JobFailedEventSchema>;

Expand Down
3 changes: 2 additions & 1 deletion packages/jobs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@durable-effect/jobs",
"version": "0.0.1-next.6",
"version": "0.0.1-next.7",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -17,6 +17,7 @@
"build": "tsc",
"clean": "rm -rf dist",
"typecheck": "tsc --noEmit",
"typecheck:test": "tsc --noEmit --project tsconfig.test.json",
"test": "vitest run",
"test:watch": "vitest"
},
Expand Down
12 changes: 12 additions & 0 deletions packages/jobs/src/services/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface ExecuteOptions<S, E, R, Ctx> {
readonly allowNullState?: boolean;
/** User-provided ID for business logic correlation (included in events) */
readonly id?: string;
/** Include pre-execution state in tracking events (default: true) */
readonly includeStateInEvents?: boolean;

readonly run: (ctx: Ctx) => Effect.Effect<void, E, R>;
readonly createContext: (base: ExecutionContextBase<S>) => Ctx;
Expand Down Expand Up @@ -104,6 +106,7 @@ export const JobExecutionServiceLayer = Layer.effect(
run,
createContext,
id,
includeStateInEvents = true,
} = options;

// Track execution start time for duration calculation
Expand Down Expand Up @@ -137,6 +140,12 @@ export const JobExecutionServiceLayer = Layer.effect(
};
}

// Capture pre-execution state snapshot for tracking events
// This is the state BEFORE user code runs
const preExecutionState = includeStateInEvents
? loadedState
: undefined;

const stateHolder = {
current: loadedState as S | null,
dirty: false,
Expand Down Expand Up @@ -220,6 +229,7 @@ export const JobExecutionServiceLayer = Layer.effect(
runCount: options.runCount ?? 0,
attempt: failedAttempt,
willRetry: true,
...(preExecutionState !== undefined && { preExecutionState }),
} satisfies InternalJobFailedEvent);
}

Expand Down Expand Up @@ -281,6 +291,7 @@ export const JobExecutionServiceLayer = Layer.effect(
runCount: options.runCount ?? 0,
attempt,
willRetry: false,
...(preExecutionState !== undefined && { preExecutionState }),
} satisfies InternalJobFailedEvent).pipe(
Effect.zipRight(Effect.fail(wrapError(error))),
);
Expand Down Expand Up @@ -308,6 +319,7 @@ export const JobExecutionServiceLayer = Layer.effect(
runCount: options.runCount ?? 0,
durationMs,
attempt,
...(preExecutionState !== undefined && { preExecutionState }),
} satisfies InternalJobExecutedEvent);
}

Expand Down
Loading