Skip to content

Commit

Permalink
Cloud function: showJobCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariya Hidayat committed Apr 8, 2020
1 parent 2bb67e7 commit c7dfb0d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
24 changes: 20 additions & 4 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@
"**/.*",
"**/node_modules/**"
],
"rewrites": [{
"source" : "/ping",
"function": "ping"
}]
"rewrites": [
{
"source": "/ping",
"function": "ping"
},
{
"source": "/showJobCounter",
"function": "showJobCounter"
}
]
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"hosting": {
"port": 5000
},
"pubsub": {
"port": 8085
}
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
8 changes: 8 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
16 changes: 16 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const admin = require('firebase-admin');
const functions = require('firebase-functions');

exports.ping = functions.https.onRequest((request, response) => {
response.send(`OK ${Date.now()}`);
});

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.showJobCounter = functions.https.onRequest(async (request, response) => {
try {
var doc = await db.collection('jobConfig').doc('counter').get();
var counter = doc.data().value;
console.log(`Counter is ${counter}`);
response.send(`Counter is ${counter}`);
} catch (err) {
console.error(`Failed to obtain the counter: ${err.toString()}`);
response.send(`EXCEPTION: ${err.toString()}`);
}
});
8 changes: 8 additions & 0 deletions tests/integration_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ describe('firebase-cronjobs', function () {
assert(/[0-9]+/.test(timestamp));
});
});

describe('firebase-cronjobs', function () {
it('should have a working showJobCounter function', async function () {
const res = await axios.get('http://localhost:5000/showJobCounter');
// fresh run: jobConfig/counter does not exist yet
assert.equal(res.data, "EXCEPTION: TypeError: Cannot read property 'value' of undefined");
});
});

0 comments on commit c7dfb0d

Please sign in to comment.