keep awake cloud functions
This commit is contained in:
parent
55479ef4ab
commit
86c2ff4605
|
@ -13,6 +13,7 @@
|
|||
},
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"fetch": "1.1.0",
|
||||
"firebase-admin": "10.0.0",
|
||||
"firebase-functions": "3.16.0",
|
||||
"lodash": "4.17.21"
|
||||
|
|
17
functions/src/call-cloud-function.ts
Normal file
17
functions/src/call-cloud-function.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import * as admin from 'firebase-admin'
|
||||
|
||||
import fetch from './fetch'
|
||||
|
||||
export const callCloudFunction = (functionName: string, data: {} = {}) => {
|
||||
const projectId = admin.instanceId().app.options.projectId
|
||||
|
||||
let url = `https://us-central1-${projectId}.cloudfunctions.net/${functionName}`
|
||||
|
||||
return fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ data }),
|
||||
}).then((response) => response.json())
|
||||
}
|
9
functions/src/fetch.ts
Normal file
9
functions/src/fetch.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
let fetchRequest: typeof fetch
|
||||
|
||||
try {
|
||||
fetchRequest = fetch
|
||||
} catch {
|
||||
fetchRequest = require('node-fetch')
|
||||
}
|
||||
|
||||
export default fetchRequest
|
|
@ -2,5 +2,6 @@ import * as admin from 'firebase-admin'
|
|||
|
||||
admin.initializeApp()
|
||||
|
||||
export * from './keep-awake'
|
||||
export * from './place-bet'
|
||||
export * from './resolve-market'
|
12
functions/src/keep-awake.ts
Normal file
12
functions/src/keep-awake.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import * as functions from 'firebase-functions'
|
||||
|
||||
import { callCloudFunction } from './call-cloud-function'
|
||||
|
||||
export const keepAwake = functions.pubsub
|
||||
.schedule('every 1 minutes')
|
||||
.onRun(async () => {
|
||||
await Promise.all([
|
||||
callCloudFunction('placeBet'),
|
||||
callCloudFunction('resolveMarket'),
|
||||
])
|
||||
})
|
Loading…
Reference in New Issue
Block a user