|
1 | 1 | import { Charm } from "@commontools/charm"; |
2 | | -import { Cell, getEntityId } from "@commontools/runner"; |
| 2 | +import { |
| 3 | + type Cell, |
| 4 | + getCell, |
| 5 | + getEntityId, |
| 6 | + type Storage, |
| 7 | +} from "@commontools/runner"; |
3 | 8 | import { Identity, type IdentityCreateConfig } from "@commontools/identity"; |
| 9 | +import { ID, type JSONSchema } from "@commontools/builder"; |
| 10 | +import { |
| 11 | + BG_CELL_CAUSE, |
| 12 | + BG_SYSTEM_SPACE_ID, |
| 13 | + type BGCharmEntry, |
| 14 | + BGCharmEntrySchema, |
| 15 | +} from "./schema.ts"; |
4 | 16 |
|
5 | 17 | /** |
6 | 18 | * Custom logger that includes timestamp and optionally charm ID |
@@ -87,3 +99,108 @@ export async function getIdentity( |
87 | 99 | } |
88 | 100 | throw new Error("No IDENTITY or OPERATOR_PASS environemnt set."); |
89 | 101 | } |
| 102 | + |
| 103 | +export async function setBGCharm({ |
| 104 | + space, |
| 105 | + charmId, |
| 106 | + integration, |
| 107 | + storage, |
| 108 | + bgSpace, |
| 109 | + bgCause, |
| 110 | +}: { |
| 111 | + space: string; |
| 112 | + charmId: string; |
| 113 | + integration: string; |
| 114 | + storage: Storage; |
| 115 | + bgSpace?: string; |
| 116 | + bgCause?: string; |
| 117 | +}): Promise<boolean> { |
| 118 | + const charmsCell = await getBGCharms({ |
| 119 | + bgSpace, |
| 120 | + bgCause, |
| 121 | + storage, |
| 122 | + }); |
| 123 | + |
| 124 | + console.log( |
| 125 | + "charmsCell", |
| 126 | + JSON.stringify(charmsCell.getAsCellLink(), null, 2), |
| 127 | + ); |
| 128 | + |
| 129 | + const charms = charmsCell.get() || []; |
| 130 | + |
| 131 | + const existingCharmIndex = charms.findIndex( |
| 132 | + (charm: Cell<BGCharmEntry>) => |
| 133 | + charm.get().space === space && charm.get().charmId === charmId, |
| 134 | + ); |
| 135 | + |
| 136 | + if (existingCharmIndex === -1) { |
| 137 | + console.log("Adding charm to BGUpdater charms cell"); |
| 138 | + charmsCell.push({ |
| 139 | + [ID]: `${space}/${charmId}`, |
| 140 | + space, |
| 141 | + charmId, |
| 142 | + integration, |
| 143 | + createdAt: Date.now(), |
| 144 | + updatedAt: Date.now(), |
| 145 | + disabledAt: undefined, |
| 146 | + lastRun: 0, |
| 147 | + status: "Initializing", |
| 148 | + } as unknown as Cell<BGCharmEntry>); |
| 149 | + |
| 150 | + // Ensure changes are synced |
| 151 | + await storage.synced(); |
| 152 | + |
| 153 | + return true; |
| 154 | + } else { |
| 155 | + console.log("Charm already exists in BGUpdater charms cell, re-enabling"); |
| 156 | + const existingCharm = charms[existingCharmIndex]; |
| 157 | + existingCharm.update({ |
| 158 | + disabledAt: 0, |
| 159 | + updatedAt: Date.now(), |
| 160 | + status: "Re-initializing", |
| 161 | + }); |
| 162 | + |
| 163 | + await storage.synced(); |
| 164 | + |
| 165 | + return false; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export async function getBGCharms( |
| 170 | + { bgSpace, bgCause, storage }: { |
| 171 | + bgSpace?: string; |
| 172 | + bgCause?: string; |
| 173 | + storage: Storage; |
| 174 | + }, |
| 175 | +): Promise< |
| 176 | + Cell<Cell<BGCharmEntry>[]> |
| 177 | +> { |
| 178 | + bgSpace = bgSpace ?? BG_SYSTEM_SPACE_ID; |
| 179 | + bgCause = bgCause ?? BG_CELL_CAUSE; |
| 180 | + |
| 181 | + if (!storage.hasSigner()) { |
| 182 | + throw new Error("Storage has no signer"); |
| 183 | + } |
| 184 | + |
| 185 | + if (!storage.hasRemoteStorage()) { |
| 186 | + throw new Error("Storage has no remote storage"); |
| 187 | + } |
| 188 | + const schema = { |
| 189 | + type: "array", |
| 190 | + items: { |
| 191 | + ...BGCharmEntrySchema, |
| 192 | + asCell: true, |
| 193 | + }, |
| 194 | + default: [], |
| 195 | + } as const satisfies JSONSchema; |
| 196 | + |
| 197 | + const charmsCell = getCell(bgSpace, bgCause, schema); |
| 198 | + |
| 199 | + // Ensure the cell is synced |
| 200 | + // FIXME(ja): does True do the right thing here? Does this mean: I REALLY REALLY |
| 201 | + // INSIST THAT YOU HAVE THIS CELL ON THE SERVER! |
| 202 | + await storage.syncCell(charmsCell, true); |
| 203 | + await storage.synced(); |
| 204 | + |
| 205 | + return charmsCell; |
| 206 | +} |
0 commit comments