Skip to content

Instantly share code, notes, and snippets.

@AdamCanady
Created May 3, 2018 05:22
Show Gist options
  • Save AdamCanady/19a8f57c3f9afa86d931638ff11c1635 to your computer and use it in GitHub Desktop.
Save AdamCanady/19a8f57c3f9afa86d931638ff11c1635 to your computer and use it in GitHub Desktop.
report catcher
FROM node:8-alpine
COPY . /var/app
WORKDIR /var/app
VOLUME /var/app/report
CMD ["node", "index.js"]
'use strict';
const fs = require('fs');
const {
GCLOUD_APPLICATION_CREDENTIALS,
GCLOUD_BUCKET_NAME,
GCLOUD_PROJECT_NAME,
} = process.env;
const storage = require('@google-cloud/storage')({
projectId: GCLOUD_PROJECT_NAME,
keyFilename: GCLOUD_APPLICATION_CREDENTIALS,
});
const VOLUME_PATH = process.env.VOLUME_PATH || '/var/app/reports';
const bucket = storage.bucket(GCLOUD_BUCKET_NAME);
const watcher = fs.watch(VOLUME_PATH, { recursive: true }, (evt, filename) => {
console.log(filename, evt);
bucket.upload(`${VOLUME_PATH}/${filename}`, (err) => {
if (err) {
return console.error(`An error occured: ${err}`);
}
return resolve(`The file ${VOLUME_PATH}/${filename} was correctly uploaded`);
});
});
// handle properly the errors for
// restarting the container if required
watcher.on('error', (err) => {
console.error(err);
process.exit(1);
})
process.on('SIGTERM', () => {
watcher.close();
process.exit(0);
});
{
"name": "report-catcher",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@google-cloud/storage": "^1.5.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment