Skip to content

Commit

Permalink
Cloud function to generate new queues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariya Hidayat committed Apr 9, 2020
1 parent f810e01 commit badb1a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
{
"source": "/addJobConfig",
"function": "addJobConfig"
},
{
"source": "/showQueueLength",
"function": "showQueueLength"
}
]
},
Expand Down
45 changes: 45 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ exports.addJobConfig = functions.https.onRequest(async (request, response) => {
}
});

exports.generateQueues = functions.firestore.document('jobConfig/counter').onUpdate(async (change, context) => {
try {
const counter = (await change.after.ref.get()).data().value;
console.log(`Job counter is ${counter}`);
const docIds = [];
const allJobs = await db.collection('jobConfig').get();
allJobs.forEach((doc) => {
if (doc.id !== 'counter') docIds.push(doc.id);
});
await Promise.all(
docIds.map(async (id) => {
const doc = await db.collection('jobConfig').doc(id).get();
const job = doc.data();
const period = job.period;
if (counter % job.period === 0) {
console.log(`Queueing ${job.task} (${period} mins)`);
const queue = {
jobId: doc.id,
timestamp: Date.now(),
index: counter / job.period,
};
return db.collection('jobQueues').add(queue);
}
})
);
} catch (err) {
console.error(`Failed to generate new queue: ${err.toString()}`);
}
});

// ----- for running in the emulators only

const { PubSub } = require('@google-cloud/pubsub');
Expand Down Expand Up @@ -105,3 +135,18 @@ exports.triggerScheduledIncrementJobCounter = functions.https.onRequest(async (r
response.send('ERROR: usable only in emulators');
}
});

exports.showQueueLength = functions.https.onRequest(async (request, response) => {
if (inEmulators) {
const allQueues = await db.collection('jobQueues').get();
let totalQueues = 0;
allQueues.forEach((queue) => {
console.log(`Queue ${totalQueues}: ${queue.id}`);
++totalQueues;
});
console.log(`Queue length is ${totalQueues}`);
response.send(`Queue length is ${totalQueues}`);
} else {
response.send('ERROR: usable only in emulators');
}
});
13 changes: 13 additions & 0 deletions tests/integration_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ describe('firebase-cronjobs', function () {
assert.equal(res.data, 'added: {"period":"10","task":"DoNothing"}');
});
});

describe('firebase-cronjobs', function () {
this.timeout(6000);
it('should queue a job', async function () {
axios.get('http://localhost:5000/addJobConfig?period=2&task=DoNothing');
axios.get('http://localhost:5000/incrementJobCounter');
await new Promise((r) => setTimeout(r, 500));
axios.get('http://localhost:5000/incrementJobCounter');
await new Promise((r) => setTimeout(r, 500));
const res = await axios.get('http://localhost:5000/showQueueLength');
assert.equal(res.data, 'Queue length is 1');
});
});

0 comments on commit badb1a0

Please sign in to comment.