keep awake cloud functions

This commit is contained in:
mantikoros 2021-12-17 20:12:58 -06:00
parent 55479ef4ab
commit 86c2ff4605
5 changed files with 41 additions and 1 deletions

View File

@ -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"

View 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
View File

@ -0,0 +1,9 @@
let fetchRequest: typeof fetch
try {
fetchRequest = fetch
} catch {
fetchRequest = require('node-fetch')
}
export default fetchRequest

View File

@ -2,5 +2,6 @@ import * as admin from 'firebase-admin'
admin.initializeApp()
export * from './keep-awake'
export * from './place-bet'
export * from './resolve-market'
export * from './resolve-market'

View 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'),
])
})