Skip to content
32 changes: 16 additions & 16 deletions packages/background-charm-service/src/space-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ export class SpaceManager {
this.failureTracking.delete(charmId);
}

const tx = entry.runtime.edit();
entry.withTx(tx).update({
lastRun: Date.now(),
status: "Success",
entry.runtime.editWithRetry((tx) => {
entry.withTx(tx).update({
lastRun: Date.now(),
status: "Success",
});
});
tx.commit(); // TODO(seefeld): We don't retry writing this. Should we?

if (this.enabledCharms.has(charmId)) {
this.pushTask(charmId, entry);
Expand All @@ -224,12 +224,12 @@ export class SpaceManager {
this.disableCharm(charmId, entry, error);
} else {
this.failureTracking.set(charmId, failureCount);
const tx = entry.runtime.edit();
entry.withTx(tx).update({
lastRun: Date.now(),
status: error,
entry.runtime.editWithRetry((tx) => {
entry.withTx(tx).update({
lastRun: Date.now(),
status: error,
});
});
tx.commit(); // TODO(seefeld): We don't retry writing this. Should we?

if (this.enabledCharms.has(charmId)) {
// Apply a linear backoff for the next attempts
Expand All @@ -247,13 +247,13 @@ export class SpaceManager {
entry: Cell<BGCharmEntry>,
error: string,
) {
const tx = entry.runtime.edit();
entry.withTx(tx).update({
disabledAt: Date.now(),
lastRun: Date.now(),
status: `Disabled: ${error}`,
entry.runtime.editWithRetry((tx) => {
entry.withTx(tx).update({
disabledAt: Date.now(),
lastRun: Date.now(),
status: `Disabled: ${error}`,
});
});
tx.commit(); // TODO(seefeld): We don't retry writing this. Should we?

this.enabledCharms.delete(charmId);
this.pendingTasks = this.pendingTasks.filter((r) => r.charmId !== charmId);
Expand Down
38 changes: 19 additions & 19 deletions packages/background-charm-service/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ export async function setBGCharm({

if (existingCharmIndex === -1) {
console.log("Adding charm to BGUpdater charms cell");
const tx = runtime.edit();
charmsCell.withTx(tx).push({
[ID]: `${space}/${charmId}`,
space,
charmId,
integration,
createdAt: Date.now(),
updatedAt: Date.now(),
disabledAt: undefined,
lastRun: 0,
status: "Initializing",
} as unknown as Cell<BGCharmEntry>);
await tx.commit();
runtime.editWithRetry((tx) => {
charmsCell.withTx(tx).push({
[ID]: `${space}/${charmId}`,
space,
charmId,
integration,
createdAt: Date.now(),
updatedAt: Date.now(),
disabledAt: undefined,
lastRun: 0,
status: "Initializing",
} as unknown as Cell<BGCharmEntry>);
});

// Ensure changes are synced
await runtime.storageManager.synced();
Expand All @@ -108,13 +108,13 @@ export async function setBGCharm({
} else {
console.log("Charm already exists in BGUpdater charms cell, re-enabling");
const existingCharm = charms[existingCharmIndex];
const tx = runtime.edit();
existingCharm.withTx(tx).update({
disabledAt: 0,
updatedAt: Date.now(),
status: "Re-initializing",
runtime.editWithRetry((tx) => {
existingCharm.withTx(tx).update({
disabledAt: 0,
updatedAt: Date.now(),
status: "Re-initializing",
});
});
tx.commit(); // TODO(seefeld): We don't retry writing this. Should we?
await runtime.storageManager.synced();

return false;
Expand Down
19 changes: 10 additions & 9 deletions packages/charm/src/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,16 @@ export const generateNewRecipeVersion = async (
llmRequestId,
);

const tx = newCharm.runtime.edit();
newCharm.withTx(tx).getSourceCell(charmSourceCellSchema)?.key("lineage").push(
{
charm: parent,
relation: "iterate",
timestamp: Date.now(),
},
);
await tx.commit(); // TODO(seefeld): We don't retry writing this. Should we?
await newCharm.runtime.editWithRetry((tx) => {
newCharm.withTx(tx).getSourceCell(charmSourceCellSchema)?.key("lineage")
.push(
{
charm: parent,
relation: "iterate",
timestamp: Date.now(),
},
);
});

return newCharm;
};
Expand Down
Loading