Forked from nbbaier/cronLogger
Public
Script
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
45
46
47
import { inTheBackground } from "https://esm.town/v/neverstew/inTheBackground";
import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
import { sqlite } from "https://esm.town/v/std/sqlite?v=6";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=45";
import { refs } from "https://esm.town/v/stevekrouse/refs?v=11";
type CronFunction = (interval: Interval) => Promise<void>;
export const cronEvalLogger = (cron: CronFunction): CronFunction => {
let interval: Interval;
const cronProxy = new Proxy(cron, {
apply(target, thisArg, args) {
interval = args[0] as Interval;
target.apply(thisArg, args);
},
});
inTheBackground(async () => {
const cronVal = refs().at(-1);
const userHandle = cronVal.userHandle;
const valName = cronVal.valName;
const htmlUrl = `https://val.town/v/${userHandle}/${valName}`;
const headers: HeadersInit = {};
const token = Deno.env.get("valtownToken");
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const { id } = await fetchJSON(`${API_URL}/v1/alias/${userHandle}/${valName}`, { headers });
const createCronEvalsTable = `CREATE TABLE IF NOT EXISTS cron_evals (
interval_id TEXT PRIMARY KEY,
val_id TEXT NOT NULL,
timestamp INTEGER NOT NULL
);`;
const insertCronEval = {
sql: `INSERT INTO cron_evals (interval_id, val_id, timestamp) VALUES (?, ?, ?);`,
args: [interval.id, id, Date.now()],
};
await sqlite.batch([
createCronEvalsTable,
insertCronEval,
]);
});
return cronProxy;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 4, 2024