Repository navigation
Add Durable Object facets support for scheduled jobs #163
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
19aef9a
add scheduled job facets runtime
cursoragent 92cb853
Fix job feed checker example and remove dead exports
cursoragent e215b22
Avoid redundant job lookups
cursoragent cd4f878
Remove experimental compatibility flag
cursoragent d8dc633
fix jobs server exec runtime model
cursoragent 7f8a7bd
finish jobs server exec bridge
cursoragent 64206c4
Fix saved app facet exec and partial update semantics (#165)
kentcdodds 5000f82
align jobs exec contract with saved apps
cursoragent 44e36a5
stabilize gate and jobs followups
cursoragent e82f111
keep job delete resilient in tests
cursoragent 396ca20
Kody app backend patterns (#166)
kentcdodds 857c446
address jobs review followups
cursoragent f47f212
tighten jobs runner consistency
cursoragent 2e908ea
finish jobs consistency fixes
cursoragent f8b012f
fix jobs runner review issues
cursoragent 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
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,48 @@ | ||
| # Integration-backed app happy path | ||
|
|
||
| Use this guide after `integration_bootstrap` proves the integration already | ||
| works, or when connector and secret state are already clear enough to verify | ||
| quickly. | ||
|
|
||
| ## Recommended sequence | ||
|
|
||
| 1. Discover connector and secret state. | ||
| - Use `search` to inspect saved connectors and secret references. | ||
| - Read full connector metadata only when you need the exact names, hosts, or | ||
| API base URL. | ||
| 2. Verify the required connector exists. | ||
| - Confirm the connector name, token secret names, and API base URL match the | ||
| app you are about to build. | ||
| 3. Run one cheap authenticated smoke test in `execute`. | ||
| - Prefer a small read-only request such as `GET /me`, `GET /viewer`, or | ||
| `GET /v1/me`. | ||
| 4. If the smoke test passes, proceed directly to building the app. | ||
| - Prefer a saved app with `serverCode` backend endpoints such as `/api/state` | ||
| and `/api/action`. | ||
| - Keep `clientCode` mostly UI plus `fetch(...)` calls to | ||
| `kodyWidget.appBackend.basePath`. | ||
| 5. Open the saved app and iterate. | ||
| - Save with `ui_save_app`. | ||
| - Reopen with `open_generated_ui({ app_id })`. | ||
| - Iterate on the hosted app instead of repeatedly pasting large inline HTML | ||
| blobs back into the model context. | ||
|
|
||
| ## Default saved app shape | ||
|
|
||
| For non-trivial or integration-backed apps, prefer this split: | ||
|
|
||
| - `serverCode`: connector lookups, provider API calls, persistence, validation, | ||
| and mutations | ||
| - `clientCode`: rendering, form handling, button clicks, and fetches to the app | ||
| backend | ||
| - `executeCode(...)` embedded directly in client HTML: acceptable for quick | ||
| prototypes or one-off experiments, not the default saved-app pattern | ||
|
|
||
| ## Avoid this detour | ||
|
|
||
| If the connector state, secret names, allowed hosts, and provider contract are | ||
| already clear enough, do **not** spend extra time spelunking the local repo | ||
| before building the app. | ||
|
|
||
| Inspect local source only when you specifically need repo conventions, shared | ||
| helpers, or an existing app artifact to extend. |
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,30 @@ | ||
| # Example job: feed checker with incremental state | ||
|
|
||
| This example shows a scheduled job that remembers the latest seen item id in its | ||
| facet SQLite storage and only emits new items on later runs. | ||
|
|
||
| ## Save it | ||
|
|
||
| Create the job with `job_create`: | ||
|
|
||
| ```json | ||
| { | ||
| "name": "Feed checker", | ||
| "schedule": { | ||
| "intervalMs": 3600000 | ||
| }, | ||
| "timezone": "America/Denver", | ||
| "enabled": true, | ||
| "serverCode": "import { DurableObject } from 'cloudflare:workers'\n\nexport class Job extends DurableObject {\n\tasync run() {\n\t\tconst feedUrlValue = await KODY.valueGet('feed_url', 'user')\n\t\tif (!feedUrlValue?.value) {\n\t\t\tthrow new Error('Missing persisted value \"feed_url\".')\n\t\t}\n\n\t\tconst response = await KODY.fetchViaHostGateway({\n\t\t\turl: feedUrlValue.value,\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\tAccept: 'application/json',\n\t\t\t},\n\t\t})\n\t\tif (response.status >= 400) {\n\t\t\tthrow new Error(`Feed request failed: ${response.status} ${response.statusText}`)\n\t\t}\n\n\t\tconst payload = JSON.parse(response.body)\n\t\tconst items = Array.isArray(payload.items) ? payload.items : []\n\n\t\tthis.ctx.storage.sql.exec(\n\t\t\t'CREATE TABLE IF NOT EXISTS feed_state (singleton INTEGER PRIMARY KEY CHECK (singleton = 1), item_id TEXT)',\n\t\t)\n\n\t\tconst latestSeen = this.ctx.storage.sql\n\t\t\t.exec('SELECT item_id FROM feed_state WHERE singleton = 1')\n\t\t\t.one<{ item_id?: string }>()?.item_id ?? null\n\n\t\tconst freshItems = latestSeen\n\t\t\t? items.filter((item) => item?.id && item.id !== latestSeen)\n\t\t\t: items.slice(0, 1)\n\n\t\tif (items[0]?.id) {\n\t\t\tthis.ctx.storage.sql.exec(\n\t\t\t\t'INSERT INTO feed_state (singleton, item_id) VALUES (1, ?) ON CONFLICT(singleton) DO UPDATE SET item_id = excluded.item_id',\n\t\t\t\tString(items[0].id),\n\t\t\t)\n\t\t}\n\n\t\tif (freshItems.length === 0) {\n\t\t\treturn { newItems: 0 }\n\t\t}\n\n\t\treturn {\n\t\t\tnewItems: freshItems.length,\n\t\t\tlatestItemId: String(items[0]?.id ?? ''),\n\t\t\tnotifications: freshItems.map((item) => ({\n\t\t\t\tid: String(item.id),\n\t\t\t\ttitle: String(item.title ?? item.id),\n\t\t\t})),\n\t\t}\n\t}\n}\n" | ||
| } | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - The job uses `KODY.valueGet('feed_url', 'user')` so the feed URL can be | ||
| changed without editing code. | ||
| - `KODY.fetchViaHostGateway()` uses the same secret-aware host approval path as | ||
| execute-time fetch. | ||
| - The `feed_state` table is local to the job facet, so every job keeps its own | ||
| checkpoint independently. | ||
| - Inspect the latest outcomes with `job_history({ job_id })`. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.