-
Notifications
You must be signed in to change notification settings - Fork 9
schema fixes / status messages on failures #954
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
Changes from all commits
6dfe85d
d40cd8a
931e511
1ff0998
02afa86
ffb8fbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |
| UI, | ||
| } from "@commontools/builder"; | ||
|
|
||
| // NOTE(ja): this must be the same as the schema in utils/src/updaters.ts | ||
| const BGCharmEntrySchema = { | ||
| type: "object", | ||
| properties: { | ||
|
|
@@ -17,9 +18,9 @@ const BGCharmEntrySchema = { | |
| integration: { type: "string" }, | ||
| createdAt: { type: "number" }, | ||
| updatedAt: { type: "number" }, | ||
| disabledAt: { type: "number" }, | ||
| lastRun: { type: "number" }, | ||
| status: { type: "string" }, | ||
| disabledAt: { type: "number", default: 0 }, | ||
| lastRun: { type: "number", default: 0 }, | ||
| status: { type: "string", default: "" }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worthwhile leaving a comment that this schema should (must?) match Probably a broader question, how does the worker code (via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call - added the note w.r.t. the broader question - as long as the schemas are additive the new defaults are loaded. but I think we have to be careful - as I don't think anything stops us from breaking things with schema changes that aren't backwards compatible |
||
| }, | ||
| required: [ | ||
| "space", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import { Cell, getCell, storage } from "@commontools/runner"; | ||
| import { JSONSchema, Schema } from "@commontools/builder"; | ||
| import { ID, JSONSchema, Schema } from "@commontools/builder"; | ||
|
|
||
| // This is the derived space id for toolshed-system | ||
| export const SYSTEM_SPACE_ID = | ||
| export const BG_SYSTEM_SPACE_ID = | ||
| "did:key:z6Mkfuw7h6jDwqVb6wimYGys14JFcyTem4Kqvdj9DjpFhY88"; | ||
| export const CELL_CAUSE = "bgUpdater-2025-03-18"; | ||
| export const BG_CELL_CAUSE = "bgUpdater-2025-03-18"; | ||
|
|
||
| export const CharmEntrySchema = { | ||
| type: "object", | ||
|
|
@@ -13,38 +13,32 @@ export const CharmEntrySchema = { | |
| charmId: { type: "string" }, | ||
| integration: { type: "string" }, | ||
| createdAt: { type: "number" }, | ||
| lastRun: { type: "number", default: null }, | ||
| updatedAt: { type: "number" }, | ||
| disabledAt: { type: "number", default: null }, | ||
| runs: { type: "number", default: 0 }, | ||
| disabledAt: { type: "number", default: 0 }, | ||
| lastRun: { type: "number", default: 0 }, | ||
| status: { type: "string", default: "" }, | ||
| }, | ||
| required: [ | ||
| "space", | ||
| "charmId", | ||
| "integration", | ||
| "createdAt", | ||
| "updatedAt", | ||
| "enabled", | ||
| "runs", | ||
| "lastRun", | ||
| "status", | ||
| ], | ||
| } as const satisfies JSONSchema; | ||
| export type BGCharmEntry = Schema<typeof CharmEntrySchema>; | ||
|
|
||
| // Define schema for the cell with correct type literals | ||
| export const bgUpdaterCharmsSchema = { | ||
| type: "object", | ||
| properties: { | ||
| charms: { | ||
| type: "array", | ||
| items: CharmEntrySchema, | ||
| default: [], | ||
| }, | ||
| }, | ||
| required: ["charms"], | ||
| type: "array", | ||
| items: CharmEntrySchema, | ||
| default: [], | ||
| } as const satisfies JSONSchema; | ||
|
|
||
| export type BGUpdaterCharmsSchema = Schema<typeof bgUpdaterCharmsSchema>; | ||
|
|
||
| export async function addCharmToBG({ | ||
| export async function addOrUpdateBGCharm({ | ||
| space, | ||
| charmId, | ||
| integration, | ||
|
|
@@ -60,59 +54,49 @@ export async function addCharmToBG({ | |
| JSON.stringify(charmsCell.getAsCellLink(), null, 2), | ||
| ); | ||
|
|
||
| // FIXME(ja): if we use IDs might might not need to do this? | ||
| // Get current charms data | ||
| const charms = charmsCell.get() || []; | ||
|
|
||
| // Check if this charm is already in the list to avoid duplicates | ||
| const exists = charms.some( | ||
| (charm: BGCharmEntry) => charm.space === space && charm.charmId === charmId, | ||
| const existingCharmIndex = charms.findIndex( | ||
| (charm: Cell<BGCharmEntry>) => | ||
| charm.get().space === space && charm.get().charmId === charmId, | ||
| ); | ||
|
|
||
| if (!exists) { | ||
| if (existingCharmIndex === -1) { | ||
| console.log("Adding charm to BGUpdater charms cell"); | ||
| charmsCell.push({ | ||
| [ID]: `${space}/${charmId}`, | ||
| space, | ||
| charmId, | ||
| integration, | ||
| createdAt: Date.now(), | ||
| updatedAt: Date.now(), | ||
| disabledAt: undefined, | ||
| runs: 0, | ||
| }); | ||
| lastRun: 0, | ||
| status: "Initializing", | ||
| } as unknown as Cell<BGCharmEntry>); | ||
|
|
||
| // Ensure changes are synced | ||
| await storage.synced(); | ||
| return true; | ||
| } | ||
|
|
||
| console.log("Charm already exists in BGUpdater charms cell"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } else { | ||
| console.log("Charm already exists in BGUpdater charms cell, re-enabling"); | ||
| const existingCharm = charms[existingCharmIndex]; | ||
| existingCharm.update({ | ||
| disabledAt: 0, | ||
| updatedAt: Date.now(), | ||
| status: "Re-initializing", | ||
| }); | ||
|
|
||
| /** | ||
| * Get the BGUpdater charms cell | ||
| */ | ||
| export async function getBGUpdaterCharmsCell() { | ||
| if (!storage.hasSigner()) { | ||
| throw new Error("Storage has no signer"); | ||
| } | ||
| await storage.synced(); | ||
|
|
||
| if (!storage.hasRemoteStorage()) { | ||
| throw new Error("Storage has no remote storage"); | ||
| return false; | ||
| } | ||
| const schema = bgUpdaterCharmsSchema.properties.charms; | ||
|
|
||
| const charmsCell = getCell(SYSTEM_SPACE_ID, CELL_CAUSE, schema); | ||
|
|
||
| // Ensure the cell is synced | ||
| await storage.syncCell(charmsCell, true); | ||
| await storage.synced(); | ||
|
|
||
| return charmsCell; | ||
| } | ||
|
|
||
| export async function newGetFunc(): Promise<Cell<Cell<BGCharmEntry>[]>> { | ||
| export async function getBGUpdaterCharmsCell(): Promise< | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice renames here 👍 |
||
| Cell<Cell<BGCharmEntry>[]> | ||
| > { | ||
| if (!storage.hasSigner()) { | ||
| throw new Error("Storage has no signer"); | ||
| } | ||
|
|
@@ -129,7 +113,7 @@ export async function newGetFunc(): Promise<Cell<Cell<BGCharmEntry>[]>> { | |
| default: [], | ||
| } as const satisfies JSONSchema; | ||
|
|
||
| const charmsCell = getCell(SYSTEM_SPACE_ID, CELL_CAUSE, schema); | ||
| const charmsCell = getCell(BG_SYSTEM_SPACE_ID, BG_CELL_CAUSE, schema); | ||
|
|
||
| // Ensure the cell is synced | ||
| // FIXME(ja): does True do the right thing here? Does this mean: I REALLY REALLY | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
consider:
error ?? "Disabled"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.
deno typescript doesn't like it:
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.
erroris astring | undefined, not sure what that type isThere 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.
I think it might just be typescript being overly cautious?