Skip to content

Instantly share code, notes, and snippets.

@dinigo
Last active November 30, 2017 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinigo/d72d01b5ebfd3d39a936c246ea3bfe6c to your computer and use it in GitHub Desktop.
Save dinigo/d72d01b5ebfd3d39a936c246ea3bfe6c to your computer and use it in GitHub Desktop.
Basic scheduler for google cloud functions

PURPOSE

It's not trivial to deploy a cron scheduler on Google Cloud. Here I purpose a simple way to prototype it. Since most part of tasks will only need a basic frequency trigger the hourly / daily / weekly / monthly percision will suffice.

1. Create the Appscript on Google Drive

Just drop the appscript.gs code insdie a freshly created appscript project. Name the project with an easily recognizable name since Google Drive doesn't let you filter by appscript file type.

2. Add the triggers

Make the appscript project trigger each function with the right frequency (i.e. trigger hourly() each our). Extra: if you don't want to receive emails on failed calls disable them on every trigger you add.

3. Create the Cloud Function on Google Cloud Platform

Choose an http trigger. Paste the index.js file and the package.json in the corresponding files. Choose cron as the function to trigger. Name the function the same way you choose in the appscript (so that the url matches)

4. Create the corresponding PubSub topics

Create the for each named frequency named cron-<frequency>, where frequency can be hourly / daily / weekly / monthly.

NEXT STEPS

Now that you have topics that get hit with certain frequency you can deploy other cloud functions to be triggered

var PROJECT_ID = 'XXXXXXXXXX'; // your project id
var ENDPOINT = 'cron'; // your cloud function name, for example, cron
// your function url
var URL = 'https://us-central1-' + PROJECT_ID + '.cloudfunctions.net/' + ENDPOINT;
/**
* Makes a GET request to the URL defined above with the frequency as a url parammeter
*
* @param {string} frequency - the trigger you wish to activate
*/
function run(frequency){
console.log('run("' + frequency + '")');
var result = UrlFetchApp.fetch(URL + '?frequency=' + frequency);
console.log(result);
}
// triggered functions
function hourly(){run("hourly")}
function daily(){run("daily")}
function weekly(){run("weekly")}
function monthly(){run("monthly")}
function testCon(){run("test")}
const PubSub = require('@google-cloud/pubsub');
const pubsub = PubSub();
const publishers = {
hourly: pubsub.topic('cron-hourly').publisher(),
daily: pubsub.topic('cron-daily').publisher(),
weekly: pubsub.topic('cron-weekly').publisher(),
monthly: pubsub.topic('cron-monthly').publisher(),
test: {publish: (f,c) => {console.log('test publish',f);c();}}
};
/**
* Queues a message on the appropiate frequency queue.
*
* @param {Object} req - Cloud Function request context.
* @param {Object} res - Cloud Function response context.
*/
exports.cron = function(req, res) {
const frequency = req.query.frequency;
const payload = {frequency, timestamp: +new Date()};
publishers[frequency].publish({}, err => {
if(err) console.error(err);
else {
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Methods', 'GET');
res.status(200).end();
}
});
};
{
"name": "cron",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@google-cloud/pubsub": "^0.15.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment