-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathrunNotificationFunctions.ts
More file actions
72 lines (65 loc) · 2.01 KB
/
runNotificationFunctions.ts
File metadata and controls
72 lines (65 loc) · 2.01 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import axios from "axios"
import * as admin from "firebase-admin"
import { FunctionName } from "functions/src"
import { uniq } from "lodash"
import { z } from "zod"
import { Script } from "./types"
import { performance } from "perf_hooks"
admin.initializeApp()
const db = admin.firestore()
const notificationFunctions: {
[K in FunctionName]?: K
} = {
httpsDeliverNotifications: "httpsDeliverNotifications",
httpsDeliverAdminNotifications: "httpsDeliverAdminNotifications",
httpsCleanupNotifications: "httpsCleanupNotifications"
}
const Args = z.object({
interval: z.number().default(5),
targets: z
.string()
.transform(s => {
const t = s
.split(",")
.map(s => s.trim())
.map(name => {
const s = notificationFunctions[name as FunctionName]
if (!s) throw Error(`Invalid function ${name}`)
return s
})
return uniq(t)
})
.default(Object.keys(notificationFunctions).join(",")) as z.ZodType<
Array<FunctionName>,
any,
string
>
})
export const script: Script = async ({ env, args }) => {
if (env !== "local") throw Error("only local supported")
const { interval, targets } = Args.parse(args)
const intervalMs = interval * 1e3
// Fetch all events
const events = await db.collection("events").get()
// Loop through the documents in the events collection
for (const event of events.docs) {
// Extract the id of the event
const topicEventId = event.id
console.log("Topic event id", topicEventId)
for (const target of targets) {
console.log("Running", target)
const start = performance.now()
await axios.post(
`http://localhost:5001/demo-dtp/us-central1/${target}`, // modified line
{
topicEventId: topicEventId
}
)
const remaining = Math.max(0, intervalMs - (performance.now() - start))
if (remaining) {
console.log(`pausing ${(remaining * 1e-3).toFixed(1)} s`)
await new Promise(r => setTimeout(r, remaining))
}
}
}
}