Skip to content

Commit

Permalink
Customize logging for script to fetch Nimbledroid data
Browse files Browse the repository at this point in the history
This adds the [debug](package) to customize the logging for
Nimbledroid's data fetching script.

The Heroku run will execute without debugging information
since it clutters the output.

We also add a note to the documentation to know how to include the debug
messages.
  • Loading branch information
armenzg committed Mar 12, 2019
1 parent e96c0f8 commit 942c680
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -53,7 +53,8 @@ Once you have it you can start the backend like this:
export NIMBLEDROID_API_KEY=<API key>
export NIMBLEDROID_EMAIL=<your email address>
export REDIS_URL=redis://localhost:6379
yarn fetchNimbledroidData
// Change DEBUG to script:* if you also want debug output
DEBUG=script:info,script:error yarn fetchNimbledroidData
yarn start
```

Expand Down
2 changes: 1 addition & 1 deletion app.json
@@ -1,7 +1,7 @@
{
"name": "firefox-health-backend",
"scripts": {
"postdeploy": "yarn fetchNimbledroidData"
"postdeploy": "DEBUG=script:info,script:error yarn fetchNimbledroidData"
},
"env": {
"GOOGLE_API_KEY": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -26,6 +26,7 @@
"license": "MPL-2.0",
"dependencies": {
"async-redis": "^1.1.4",
"debug": "^4.1.1",
"googleapis": "^27.0.0",
"isomorphic-fetch": "^2.2.1",
"koa": "^2.3.0",
Expand Down
20 changes: 13 additions & 7 deletions src/scripts/fetchNimbledroidData.js
@@ -1,6 +1,11 @@
import debug from 'debug';
import asyncRedis from 'async-redis';
import NimbledroidClient from '../utils/NimbledroidClient';

const debugLog = debug('script:debug');
const infoLog = debug('script:info');
const errorLog = debug('script:error');

if (
!process.env.REDIS_URL ||
!process.env.NIMBLEDROID_API_KEY ||
Expand All @@ -16,7 +21,7 @@ const nimbledroidClient = new NimbledroidClient(
const redisClient = asyncRedis.createClient(process.env.REDIS_URL);

redisClient.on('error', (err) => {
console.error(err);
errorLog(err);
});

// eslint-disable-next-line consistent-return
Expand All @@ -33,10 +38,10 @@ const storeProfilingRunIfMissing = async (profilingRunData) => {
if (status === 'Failed') {
const cached = await redisClient.get(key);
if (!cached) {
console.log(`Storing ${key}`);
debugLog(`Storing ${key}`);
await redisClient.set(key, JSON.stringify(profilingRunData));
} else {
console.log(`The key is already in the cache (${key})`);
debugLog(`The key is already in the cache (${key})`);
}
}
};
Expand All @@ -51,21 +56,22 @@ const fetchData = async productName =>

const main = async () => {
let errorCode = -1;
console.log('Fetching each product can take between 20-40 seconds.');
debugLog('DEBUG output will be shown.');
infoLog('Fetching each product can take between 20-40 seconds.');
try {
await Promise.all([
'org.mozilla.klar',
'org.mozilla.geckoview_example',
'com.chrome.beta',
].map(async (productName) => {
console.log(`Fetching ${productName}`);
infoLog(`Fetching ${productName}`);
const productData = await fetchData(productName);
console.log(`Storing ${productName}`);
infoLog(`Storing ${productName}`);
await storeDataInRedis(productData);
}));
errorCode = 0;
} catch (e) {
console.error(e);
errorLog(e);
} finally {
process.exit(errorCode);
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -2025,6 +2025,13 @@ debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.6, debug@^2.6.
dependencies:
ms "2.0.0"

debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
dependencies:
ms "^2.1.1"

decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down Expand Up @@ -4975,6 +4982,11 @@ ms@2.0.0:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=

ms@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==

multicast-dns-service-types@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/multicast-dns-service-types/-/multicast-dns-service-types-1.1.0.tgz#899f11d9686e5e05cb91b35d5f0e63b773cfc901"
Expand Down

0 comments on commit 942c680

Please sign in to comment.