Skip to content

Commit bc02d1d

Browse files
Merge pull request #35 from backpine/job-tracking
Job tracking
2 parents 1d44dd7 + 59bf645 commit bc02d1d

22 files changed

Lines changed: 1694 additions & 184 deletions

File tree

examples/effect-worker-v2/src/jobs/basic-debounce.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export const debounceExample = Debounce.make({
5252
Effect.gen(function* () {
5353
const state = yield* ctx.state;
5454
const eventCount = yield* ctx.eventCount;
55-
55+
yield* Effect.tryPromise(() => fetch("http://localhost:3000/api/health"));
56+
// yield* Effect.fail("Debounce job failed");
5657
yield* Effect.log(
5758
`Debounce flushed! Events: ${eventCount}, Last action: ${state?.actionId}, Reason: ${ctx.flushReason}`,
5859
);

examples/effect-worker-v2/src/jobs/basic-task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const basicTask = Task.make({
2727
currentRun: 0,
2828
});
2929

30-
yield* ctx.schedule(Date.now() + 5000);
30+
yield* ctx.schedule(Date.now() + 200);
3131
}
3232
}),
3333

examples/effect-worker-v2/src/jobs/heartbeat.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Continuous } from "@durable-effect/jobs";
2-
import { Effect, Schema } from "effect";
2+
import { Duration, Effect, Schema } from "effect";
33

44
// =============================================================================
55
// Heartbeat Job - Simple Continuous Job Example
@@ -41,11 +41,16 @@ export const heartbeat = Continuous.make({
4141
stateSchema: HeartbeatState,
4242

4343
// Run every 10 seconds
44-
schedule: Continuous.every("4 minutes"),
44+
schedule: Continuous.every("4 seconds"),
4545

4646
// Start immediately when created (default: true)
4747
startImmediately: true,
4848

49+
retry: {
50+
maxAttempts: 3,
51+
delay: Duration.seconds(1),
52+
},
53+
4954
// The execute function runs on each scheduled tick
5055
execute: (ctx) =>
5156
Effect.gen(function* () {
@@ -55,6 +60,7 @@ export const heartbeat = Continuous.make({
5560
yield* Effect.log(
5661
`Heartbeat #${ctx.runCount}: ${currentState.name} - count=${currentState.count}`,
5762
);
63+
// yield* Effect.fail("Heartbeat job failed");
5864

5965
// Update state (Effect-based)
6066
yield* ctx.updateState((s) => ({
@@ -64,7 +70,7 @@ export const heartbeat = Continuous.make({
6470
}));
6571

6672
// Example: auto-terminate after 10 heartbeats
67-
if (currentState.count >= 9) {
73+
if (currentState.count >= 2000) {
6874
yield* Effect.log(
6975
`Heartbeat ${currentState.name} reached max count, terminating`,
7076
);

examples/effect-worker-v2/src/jobs/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,24 @@ import { debounceExample } from "./basic-debounce";
3030
* });
3131
* ```
3232
*/
33-
export const { Jobs, JobsClient, registry } = createDurableJobs({
34-
// Task jobs
35-
basicTask,
33+
export const { Jobs, JobsClient, registry } = createDurableJobs(
34+
{
35+
// Task jobs
36+
basicTask2: basicTask,
3637

37-
// Continuous jobs
38-
heartbeat,
39-
// Debounce job
40-
debounceExample,
41-
});
38+
// Continuous jobs
39+
heartbeat2: heartbeat,
40+
// Debounce job
41+
debounceExample2: debounceExample,
42+
},
43+
{
44+
tracker: {
45+
endpoint: "http://localhost:3000/sync",
46+
env: "dev",
47+
serviceKey: "my-service-key",
48+
},
49+
},
50+
);
4251

4352
// =============================================================================
4453
// Type Exports

examples/effect-worker-v2/src/routes/jobs/continuous.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const continuousRoutes = HttpRouter.empty.pipe(
4040
const body = yield* HttpServerRequest.schemaBodyJson(StartRequest);
4141

4242
const now = Date.now();
43-
const result = yield* client.continuous("heartbeat").start({
43+
const result = yield* client.continuous("heartbeat2").start({
4444
id: body.id,
4545
input: {
4646
name: body.name,
@@ -69,7 +69,7 @@ export const continuousRoutes = HttpRouter.empty.pipe(
6969
const client = JobsClient.fromBinding(env.JOBS);
7070
const body = yield* HttpServerRequest.schemaBodyJson(TerminateRequest);
7171

72-
const result = yield* client.continuous("heartbeat").terminate(body.id, {
72+
const result = yield* client.continuous("heartbeat2").terminate(body.id, {
7373
reason: body.reason,
7474
});
7575

@@ -91,7 +91,7 @@ export const continuousRoutes = HttpRouter.empty.pipe(
9191
const client = JobsClient.fromBinding(env.JOBS);
9292
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
9393

94-
const result = yield* client.continuous("heartbeat").trigger(body.id);
94+
const result = yield* client.continuous("heartbeat2").trigger(body.id);
9595

9696
return yield* HttpServerResponse.json({
9797
success: true,
@@ -112,7 +112,7 @@ export const continuousRoutes = HttpRouter.empty.pipe(
112112
const client = JobsClient.fromBinding(env.JOBS);
113113
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
114114

115-
const result = yield* client.continuous("heartbeat").status(body.id);
115+
const result = yield* client.continuous("heartbeat2").status(body.id);
116116

117117
return yield* HttpServerResponse.json({
118118
success: true,
@@ -129,7 +129,7 @@ export const continuousRoutes = HttpRouter.empty.pipe(
129129
const client = JobsClient.fromBinding(env.JOBS);
130130
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
131131

132-
const result = yield* client.continuous("heartbeat").getState(body.id);
132+
const result = yield* client.continuous("heartbeat2").getState(body.id);
133133

134134
return yield* HttpServerResponse.json({
135135
success: true,

examples/effect-worker-v2/src/routes/jobs/debounce.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
3535
const client = JobsClient.fromBinding(env.JOBS);
3636
const body = yield* HttpServerRequest.schemaBodyJson(AddEventRequest);
3737

38-
const result = yield* client.debounce("debounceExample").add({
38+
const result = yield* client.debounce("debounceExample2").add({
3939
id: body.id,
4040
event: {
4141
actionId: body.actionId,
@@ -53,7 +53,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
5353
created: result.created,
5454
},
5555
});
56-
})
56+
}),
5757
),
5858

5959
// POST /debounce/flush - Manually flush the buffer
@@ -64,7 +64,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
6464
const client = JobsClient.fromBinding(env.JOBS);
6565
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
6666

67-
const result = yield* client.debounce("debounceExample").flush(body.id);
67+
const result = yield* client.debounce("debounceExample2").flush(body.id);
6868

6969
return yield* HttpServerResponse.json({
7070
success: true,
@@ -74,7 +74,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
7474
reason: result.reason,
7575
},
7676
});
77-
})
77+
}),
7878
),
7979

8080
// POST /debounce/clear - Clear the buffer without flushing
@@ -85,7 +85,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
8585
const client = JobsClient.fromBinding(env.JOBS);
8686
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
8787

88-
const result = yield* client.debounce("debounceExample").clear(body.id);
88+
const result = yield* client.debounce("debounceExample2").clear(body.id);
8989

9090
return yield* HttpServerResponse.json({
9191
success: true,
@@ -94,7 +94,7 @@ export const debounceRoutes = HttpRouter.empty.pipe(
9494
discardedEvents: result.discardedEvents,
9595
},
9696
});
97-
})
97+
}),
9898
),
9999

100100
// POST /debounce/status - Get debounce status
@@ -105,13 +105,13 @@ export const debounceRoutes = HttpRouter.empty.pipe(
105105
const client = JobsClient.fromBinding(env.JOBS);
106106
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
107107

108-
const result = yield* client.debounce("debounceExample").status(body.id);
108+
const result = yield* client.debounce("debounceExample2").status(body.id);
109109

110110
return yield* HttpServerResponse.json({
111111
success: true,
112112
result,
113113
});
114-
})
114+
}),
115115
),
116116

117117
// POST /debounce/state - Get debounce state
@@ -122,14 +122,16 @@ export const debounceRoutes = HttpRouter.empty.pipe(
122122
const client = JobsClient.fromBinding(env.JOBS);
123123
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
124124

125-
const result = yield* client.debounce("debounceExample").getState(body.id);
125+
const result = yield* client
126+
.debounce("debounceExample2")
127+
.getState(body.id);
126128

127129
return yield* HttpServerResponse.json({
128130
success: true,
129131
result: {
130132
state: result.state,
131133
},
132134
});
133-
})
134-
)
135+
}),
136+
),
135137
);

examples/effect-worker-v2/src/routes/jobs/task.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const taskRoutes = HttpRouter.empty.pipe(
3434
const client = JobsClient.fromBinding(env.JOBS);
3535
const body = yield* HttpServerRequest.schemaBodyJson(SendEventRequest);
3636

37-
const result = yield* client.task("basicTask").send({
37+
const result = yield* client.task("basicTask2").send({
3838
id: body.id,
3939
event: { targetRuns: body.targetRuns },
4040
});
@@ -46,7 +46,7 @@ export const taskRoutes = HttpRouter.empty.pipe(
4646
scheduledAt: result.scheduledAt,
4747
},
4848
});
49-
})
49+
}),
5050
),
5151

5252
// POST /task/status - Get task status
@@ -57,13 +57,13 @@ export const taskRoutes = HttpRouter.empty.pipe(
5757
const client = JobsClient.fromBinding(env.JOBS);
5858
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
5959

60-
const result = yield* client.task("basicTask").status(body.id);
60+
const result = yield* client.task("basicTask2").status(body.id);
6161

6262
return yield* HttpServerResponse.json({
6363
success: true,
6464
result,
6565
});
66-
})
66+
}),
6767
),
6868

6969
// POST /task/state - Get task state
@@ -74,7 +74,7 @@ export const taskRoutes = HttpRouter.empty.pipe(
7474
const client = JobsClient.fromBinding(env.JOBS);
7575
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
7676

77-
const result = yield* client.task("basicTask").getState(body.id);
77+
const result = yield* client.task("basicTask2").getState(body.id);
7878

7979
return yield* HttpServerResponse.json({
8080
success: true,
@@ -84,7 +84,7 @@ export const taskRoutes = HttpRouter.empty.pipe(
8484
scheduledAt: result.scheduledAt,
8585
},
8686
});
87-
})
87+
}),
8888
),
8989

9090
// POST /task/terminate - Terminate task
@@ -95,7 +95,7 @@ export const taskRoutes = HttpRouter.empty.pipe(
9595
const client = JobsClient.fromBinding(env.JOBS);
9696
const body = yield* HttpServerRequest.schemaBodyJson(IdRequest);
9797

98-
const result = yield* client.task("basicTask").terminate(body.id);
98+
const result = yield* client.task("basicTask2").terminate(body.id);
9999

100100
return yield* HttpServerResponse.json({
101101
success: true,
@@ -104,6 +104,6 @@ export const taskRoutes = HttpRouter.empty.pipe(
104104
terminated: result.terminated,
105105
},
106106
});
107-
})
108-
)
107+
}),
108+
),
109109
);

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@durable-effect/core",
3-
"version": "0.0.1-next.13",
3+
"version": "0.0.1-next.14",
44
"type": "module",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

0 commit comments

Comments
 (0)