Repository navigation
feat(agents): synchronous Lifecycle job push and cancel #2420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": patch | ||
| --- | ||
|
|
||
| Lifecycle capabilities can push and cancel jobs synchronously with `jobs.pushSync()` and `jobs.cancelSync()`, so a job commits or rolls back with the capability's own writes in one `storage.transactionSync`. Call `jobs.rearm()` once the transaction commits. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| LifecycleCapability, | ||
| type LifecycleJobPushOptions | ||
| } from "../../lifecycle"; | ||
| import { withCapabilityHarness } from "../shared/capability-harness"; | ||
|
|
||
| class SyncJobCapability extends LifecycleCapability { | ||
| constructor() { | ||
| super("sync-job-test"); | ||
| } | ||
|
|
||
| push(options: LifecycleJobPushOptions) { | ||
| return this.lifecycle.jobs.pushSync(options); | ||
| } | ||
|
|
||
| cancel(id: string) { | ||
| return this.lifecycle.jobs.cancelSync(id); | ||
| } | ||
|
|
||
| list() { | ||
| return this.lifecycle.jobs.list(); | ||
| } | ||
|
|
||
| rearm() { | ||
| return this.lifecycle.jobs.rearm(); | ||
| } | ||
| } | ||
|
|
||
| describe("Lifecycle synchronous jobs", () => { | ||
| it("rolls a synchronous job back with its surrounding transaction", async () => { | ||
| await withCapabilityHarness(async ({ storage, install }) => { | ||
| const capability = new SyncJobCapability(); | ||
| const { lifecycle } = install(capability); | ||
| await lifecycle.start(); | ||
|
|
||
| expect(() => | ||
| storage.transactionSync(() => { | ||
| capability.push({ id: "job", fn: "run", time: Date.now() }); | ||
| throw new Error("rollback"); | ||
| }) | ||
| ).toThrow("rollback"); | ||
| expect(capability.list()).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| it("rearms after a committed synchronous mutation", async () => { | ||
| await withCapabilityHarness(async ({ storage, install }) => { | ||
| const capability = new SyncJobCapability(); | ||
| const { lifecycle } = install(capability); | ||
| await lifecycle.start(); | ||
|
|
||
| const time = Date.now() + 60_000; | ||
| storage.transactionSync(() => { | ||
| capability.push({ id: "job", fn: "run", time }); | ||
| }); | ||
| expect(await storage.getAlarm()).toBeNull(); | ||
|
|
||
| await capability.rearm(); | ||
| expect(await storage.getAlarm()).toBe(time); | ||
|
|
||
| storage.transactionSync(() => { | ||
| expect(capability.cancel("job")).toBe(true); | ||
| }); | ||
| await capability.rearm(); | ||
| expect(await storage.getAlarm()).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| it("recreates the job table when the transaction that created it rolled back", async () => { | ||
| await withCapabilityHarness(async ({ storage, install }) => { | ||
| const capability = new SyncJobCapability(); | ||
| const { lifecycle } = install(capability); | ||
| await lifecycle.start(); | ||
| storage.sql.exec("DROP TABLE IF EXISTS cf_agents_jobs"); | ||
|
|
||
| expect(() => | ||
| storage.transactionSync(() => { | ||
| capability.push({ id: "first", fn: "run", time: Date.now() }); | ||
| throw new Error("rollback"); | ||
| }) | ||
| ).toThrow("rollback"); | ||
|
|
||
| const time = Date.now() + 60_000; | ||
| storage.transactionSync(() => { | ||
| capability.push({ id: "second", fn: "run", time }); | ||
| }); | ||
| expect(capability.list().map((job) => job.id)).toEqual(["second"]); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 Check upstream Lifecycle documentation sync
The documentation rules require a matching update in cloudflare-docs. That repository is outside this PR, so confirm the new synchronous job guidance is ported.
Was this helpful? React with 👍 or 👎 to provide feedback.