Skip to content

Commit

Permalink
Correctly handled promises (end with a catch block) and some minor op…
Browse files Browse the repository at this point in the history
…timizations
  • Loading branch information
Javinator9889 committed Jun 9, 2020
1 parent 251d65f commit f10a6b9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
6 changes: 5 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
],
"rewrites": [
{
"source": "/api/v1/**",
"source": "/{,**}",
"destination": "/api/v1"
},
{
"source": "/api/v1**",
"function": "www-webApi"
}
]
Expand Down
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"daemon": "cross-env-shell RUN_DAEMON=true \"npm run build && firebase emulators:start --only functions\""
},
"engines": {
"node": "10"
"node": "8"
},
"main": "lib/bin/index.js",
"dependencies": {
Expand Down
10 changes: 8 additions & 2 deletions functions/src/bin/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import functions = require('firebase-functions');


updater.initialize()
.then(_ => updater.scheduleUpdates());
.then(_ => updater.scheduleUpdates()
.catch(err => console.warn(`Error while scheduling updates - ${err}`)))
.catch(err => {
console.error(`Error while initializing the updater - ${err}`);
process.exit(1);
});

process.on('SIGINT', () => {
updater.stopScheduling()
.then(process.exit(0));
.finally(process.exit(0))
.catch(err => console.warn(`Error while finishing the schedules - ${err}`));
});

exports.updater = functions.https.onRequest((req, resp) => resp.sendStatus(200));
4 changes: 1 addition & 3 deletions functions/src/models/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export async function initialize() {
initCalled = true;
const projectProperties = properties.projectProperties(firebaseApp);
for (const language of properties.languages) {
console.debug(`Creating updater for language ${language}`);
const terms = await remoteConfig.getSearchTermsForLanguage(language);
console.debug(`Updater terms: ${terms}`);
updaters[language] = new Updater(
projectProperties.database,
`${projectProperties.collection}_${language}`,
Expand All @@ -42,7 +40,7 @@ export async function initialize() {
export async function scheduleUpdates() {
if (!initCalled)
throw new Error('`initialize` not called');
console.info('Updater is scheduling updates')
console.info('Updaters are scheduling updates')
for (const language of properties.languages) {
timers.add(updaters[language].schedule());
}
Expand Down
4 changes: 3 additions & 1 deletion functions/src/rcdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class RemoteConfigData {
listenToRCChanges() {
functions.remoteConfig.onUpdate(_ => {
return admin.credential.applicationDefault().getAccessToken()
// tslint:disable-next-line:no-shadowed-variable
.then(_ => {
this.remoteConfig.getTemplate()
.then(template => {
Expand All @@ -51,7 +52,8 @@ export class RemoteConfigData {
console.warn(`Updaters are not set yet - ${e}`);
}
}
});
})
.catch(err => console.warn(`Error while obtaining the template - ${err}`));
})
.catch(err => console.error(`Error while obtaining data from RC: ${err}`));
});
Expand Down
21 changes: 11 additions & 10 deletions functions/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ export class Updater {

async updateData(content: Array<NewsriverData>) {
try {
content.forEach(element => {
firebaseHelper.firestore.checkDocumentExists(this.db, this.collectionName, element.id)
.then(exists => {
if (!exists)
firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element);
else
firebaseHelper.firestore.updateDocument(this.db, this.collectionName, element.id, element);
})
console.log(`Created element with ID: ${element.id}`);
});
for (const element of content) {
try {
const exists = await firebaseHelper.firestore.checkDocumentExists(this.db, this.collectionName, element.id);
if (!exists)
await firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element);
else
await firebaseHelper.firestore.updateDocument(this.db, this.collectionName, element.id, element);
} catch (err) {
console.warn(`Error while creating/updating document - ${err}`);
}
}
} catch (error) {
console.error(`Unhandled error ${error}`);
}
Expand Down
3 changes: 2 additions & 1 deletion functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"outDir": "lib",
"sourceMap": true,
"strict": false,
"target": "es2017"
"target": "es2017",
"lib": ["es2015", "es2016", "dom", "es2018.promise"]
},
"compileOnSave": true,
"include": [
Expand Down

0 comments on commit f10a6b9

Please sign in to comment.