Skip to content

Instantly share code, notes, and snippets.

@avivl
Created November 1, 2017 16:15
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 avivl/7352dda65e25f04b959b8e29147b37e5 to your computer and use it in GitHub Desktop.
Save avivl/7352dda65e25f04b959b8e29147b37e5 to your computer and use it in GitHub Desktop.
'use strict';
// [START import]
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.database();
var refRunning = db.ref('/running-jobs');
var refFinished = db.ref('/finished-jobs');
// [END import]
// [START RunningJobsPubSub]
exports.RunningJobsPubSub = functions.pubsub.topic('bqtop-running-jobs').onPublish(event => {
// [END trigger]
const pubSubMessage = event.data;
return refRunning.push(pubSubMessage.json);
})
// [END RunningJobsPubSub]
// [START FinishedJobsPubSub]
exports.FinishedJobsPubSub = functions.pubsub.topic('bqtop-finished-jobs').onPublish(event => {
const pubSubMessage = event.data;
return refFinished.push(pubSubMessage.json);
})
// [END FinishedJobsPubSub]
// [START DeleteRunningJobFromFinished]
exports.DeleteRunningJobFromFinished = functions.database.ref('finished-jobs/{key}').onCreate(event =>
{
const runningRef = event.data.adminRef.root.child('running-jobs')
const jobId = event.data.val()['protoPayload']['serviceData']['jobCompletedEvent']['job']['jobName']['jobId']
runningRef.orderByChild('protoPayload/serviceData/jobInsertResponse/resource/jobName/jobId')
.equalTo(jobId).once("value", function (snapshot) {
snapshot.forEach(function (data) {
runningRef.child(data.key).remove()
})
});
})
// [END DeleteRunningJobFromFinished]
// [START DeleteRunningJobFromRunning]
exports.DeleteRunningJobFromRunning = functions.database.ref('running-jobs/{key}').onCreate(event => {
const runningRef = event.data.adminRef.root.child('running-jobs');
const finishedRef = event.data.adminRef.root.child('finished-jobs');
const jobId = event.data.val()['protoPayload']['serviceData']['jobInsertResponse']['resource']['jobName']['jobId']
finishedRef.orderByChild('protoPayload/serviceData/jobCompletedEvent/job/jobName/jobId').
equalTo(jobId).once("value", function (snapshot) {
snapshot.forEach(function (data) {
runningRef.orderByChild('protoPayload/serviceData/jobInsertResponse/resource/jobName/jobId').equalTo(jobId)
.once("value", function (snapshot) {
snapshot.forEach(function (data) {
runningRef.child(data.key).remove()
})
});
})
});
})
// [END DeleteRunningJobFromRunning]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment