| 165 | }, |
| 166 | |
| 167 | async onFinish(ctx) { |
| 168 | const state = runState.get(ctx) |
| 169 | if (!state) return |
| 170 | const { handle, ensureCtx } = state |
| 171 | |
| 172 | await state.watcher?.stop() |
| 173 | |
| 174 | const lifecycle = definition.lifecycle |
| 175 | |
| 176 | if ( |
| 177 | lifecycle?.snapshot === 'after-run' && |
| 178 | handle.capabilities.snapshots && |
| 179 | handle.snapshot |
| 180 | ) { |
| 181 | const snapshot = await handle.snapshot(`after-run-${ctx.runId}`) |
| 182 | const store = ensureCtx.store |
| 183 | if (store) { |
| 184 | const key = definition.key(ensureCtx) |
| 185 | const existing = await store.get(key) |
| 186 | if (existing) { |
| 187 | await store.upsert({ |
| 188 | ...existing, |
| 189 | latestSnapshotId: snapshot.id, |
| 190 | updatedAt: Date.now(), |
| 191 | }) |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (lifecycle?.destroyOnComplete) { |
| 197 | await definition.destroy(ensureCtx) |
| 198 | await definition.hooks?.onDestroy?.() |
| 199 | } |
| 200 | }, |
| 201 | |
| 202 | async onAbort(ctx, _info: AbortInfo) { |
| 203 | const state = runState.get(ctx) |