Skip to content

Commit

Permalink
feat: record refresh timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Apr 12, 2023
1 parent 1ff38de commit e23c193
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Env } from './types';

import { makePrisma } from './prisma';
import { handleScheduled } from './scheduled';
import { getRefreshTimestamp } from './state';

const router = Router();

Expand Down Expand Up @@ -56,7 +57,7 @@ router.get('/resources', async (request, env: Env) => {
}
}));

return makeResponse({ resources });
return makeResponse({ resources, timestamp: await getRefreshTimestamp(env) });

function resolveParams():
| {
Expand Down
3 changes: 3 additions & 0 deletions packages/worker/src/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fetchResourcePage } from 'animegarden';
import type { Env } from './types';

import { makePrisma } from './prisma';
import { updateRefreshTimestamp } from './state';

const teams = new Set<number>();
const users = new Set<number>();
Expand Down Expand Up @@ -77,5 +78,7 @@ export async function handleScheduled(env: Env) {
console.log(`There are ${count} resources inserted`);
}

await updateRefreshTimestamp(env);

return { count: sum };
}
9 changes: 9 additions & 0 deletions packages/worker/src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Env } from './types';

export async function updateRefreshTimestamp(env: Env) {
await env.animegarden.put('state/refresh-timestamp', new Date().toISOString());
}

export async function getRefreshTimestamp(env: Env) {
return new Date((await env.animegarden.get('state/refresh-timestamp')) ?? 0);
}
2 changes: 2 additions & 0 deletions packages/worker/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface Env {
DATABASE_URL: string;

animegarden: KVNamespace;

database: D1Database;
}
4 changes: 4 additions & 0 deletions packages/worker/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ name = "animegarden"
main = "src/index.ts"
compatibility_date = "2023-03-22"

kv_namespaces = [
{ binding = "animegarden", id = "d0bb91899b22468db08020b251d3cef4", preview_id = "0bcc680779d340dfb80a3512a89cb533" }
]

[triggers]
crons = ["*/5 * * * *"]

0 comments on commit e23c193

Please sign in to comment.