Skip to content

Commit

Permalink
fix(refresh-tokens): add scheduled cloud function to run update
Browse files Browse the repository at this point in the history
github disables workflow on repo inactivity
  • Loading branch information
leomp12 committed Nov 5, 2020
1 parent e79b789 commit d338924
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/call-refresh-tokens.yml

This file was deleted.

24 changes: 18 additions & 6 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ router.get('/', (req, res) => {
require(`${routes}/`)(req, res)
})

const prepareAppSdk = () => {
// debug ecomAuth processes and ensure enable token updates by default
process.env.ECOM_AUTH_DEBUG = 'true'
process.env.ECOM_AUTH_UPDATE = 'enabled'
// setup ecomAuth client with Firestore instance
return setup(null, true, admin.firestore())
}

// base routes for E-Com Plus Store API
const routesDir = path.join(__dirname, routes)
recursiveReadDir(routesDir).filter(filepath => filepath.endsWith('.js')).forEach(filepath => {
Expand Down Expand Up @@ -103,12 +111,7 @@ recursiveReadDir(routesDir).filter(filepath => filepath.endsWith('.js')).forEach
if (middleware) {
router[method](filename, (req, res) => {
console.log(`${method} ${filename}`)

// debug ecomAuth processes and ensure enable token updates by default
process.env.ECOM_AUTH_DEBUG = 'true'
process.env.ECOM_AUTH_UPDATE = 'enabled'
// setup ecomAuth client with Firestore instance
setup(null, true, admin.firestore()).then(appSdk => {
prepareAppSdk().then(appSdk => {
middleware({ appSdk, admin }, req, res)
}).catch(err => {
console.error(err)
Expand All @@ -128,3 +131,12 @@ server.use(express.static('public'))

exports[functionName] = functions.https.onRequest(server)
console.log(`-- Starting '${app.title}' E-Com Plus app with Function '${functionName}'`)

// schedule update tokens job
const cron = '25 1,9,17 * * *'
exports.updateTokens = functions.pubsub.schedule(cron).onRun(() => {
return prepareAppSdk().then(appSdk => {
return appSdk.updateTokens()
})
})
console.log(`-- Sheduled update E-Com Plus tokens '${cron}'`)

0 comments on commit d338924

Please sign in to comment.