Spaces:
Runtime error
Runtime error
File size: 1,209 Bytes
c0dd699 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import { DurableObject } from 'cloudflare:workers';
import { TaskStatusResult } from './types';
const CACHE_EXPIRATION_TIME = 5 * 60 * 1000; // 5 minutes in milliseconds
export class TaskStatusDurableObject extends DurableObject {
private value: Record<string, TaskStatusResult>;
constructor(state: DurableObjectState, env: unknown) {
super(state, env);
this.value = {};
}
getStatus(id: string) {
return this.value[id] || {};
}
setStatus(id: string, status: TaskStatusResult) {
const newValue = Object.assign(this.getStatus(id), status) as TaskStatusResult;
this.value[id] = newValue;
this.ctx.storage.getAlarm().then((currentAlarm) => {
if (currentAlarm == null) {
return this.ctx.storage.setAlarm(Date.now() + CACHE_EXPIRATION_TIME);
}
});
return newValue;
}
// TaskStatusResult has a timestamp, so we can use it to clean up old data
cleanUpOldData() {
const now = Date.now();
const ids = Object.keys(this.value);
for (const id of ids) {
if (now - this.value[id].timestamp > CACHE_EXPIRATION_TIME) {
delete this.value[id];
}
}
}
async alarm() {
this.cleanUpOldData();
await this.ctx.storage.setAlarm(Date.now() + CACHE_EXPIRATION_TIME);
}
}
|