From a76b3214282020ffd4c98aec3352e18ca75092f5 Mon Sep 17 00:00:00 2001 From: Eri Irawan Date: Wed, 15 Mar 2017 14:01:37 +0700 Subject: [PATCH 1/5] on progress --- alarm.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 alarm.js diff --git a/alarm.js b/alarm.js new file mode 100644 index 0000000..ecb2cd9 --- /dev/null +++ b/alarm.js @@ -0,0 +1,7 @@ +'use strict' + +var cron = require('node-cron') + +cron.schedule('* * * * *', function () { + console.log('running a task every minute') +}) From 448a3affefbbd7368c314c435b39bd681b737f0d Mon Sep 17 00:00:00 2001 From: Eri Irawan Date: Wed, 15 Mar 2017 14:02:34 +0700 Subject: [PATCH 2/5] watch email on sender --- mail-listen.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 mail-listen.js diff --git a/mail-listen.js b/mail-listen.js new file mode 100644 index 0000000..9ce9713 --- /dev/null +++ b/mail-listen.js @@ -0,0 +1,42 @@ +'use strict' + +const kue = require('kue') +const queue = kue.createQueue() +require('dotenv').config() + +const sg = require('sendgrid')(process.env.SENDGRID_API_KEY) + +queue.process('email', (job, done) => { + const request = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: { + personalizations: [{ + to: [{ + email: job.data.to + }], + subject: job.data.content + }], + from: { + email: 'send_grid@example.com' + }, + content: [{ + type: 'text/plain', + value: job.data.content + }] + } + }) + + sg.API(request) + .then(response => { + console.log(response.statusCode) + console.log(response.body) + console.log(response.headers) + done() + }) + .catch(error => { + // error is an instance of SendGridError + // The full response is attached to error.response + console.log(error.response.statusCode) + }) +}) From 427350b73c35ab899cd9a442ca41a4cc5d9241a3 Mon Sep 17 00:00:00 2001 From: Eri Irawan Date: Wed, 15 Mar 2017 14:03:14 +0700 Subject: [PATCH 3/5] mail sender with sendgrid and kuejs --- mail-sender.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 mail-sender.js diff --git a/mail-sender.js b/mail-sender.js new file mode 100644 index 0000000..de7eb1a --- /dev/null +++ b/mail-sender.js @@ -0,0 +1,12 @@ +'use strict' + +const kue = require('kue') +const queue = kue.createQueue() + +let jobEmail = queue.create('email', { + to: 'eryirawan91@gmail.com', + subject: 'test', + content: 'Hello sendGrid' +}).save(function (err, title) { + if (err) console.log(err) +}) From c8dff20deff01538f01d6755a1be55e2abc64ac7 Mon Sep 17 00:00:00 2001 From: Eri Irawan Date: Wed, 15 Mar 2017 14:03:54 +0700 Subject: [PATCH 4/5] install sendgrid, dotenv, kue, --- package.json | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..c5e90cf --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "its-background-job", + "version": "1.0.0", + "description": "bakcground jobs", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MrEi91/its-background-job.git" + }, + "keywords": [ + "redis", + "crons" + ], + "author": "Eri Irawan", + "license": "ISC", + "bugs": { + "url": "https://github.com/MrEi91/its-background-job/issues" + }, + "homepage": "https://github.com/MrEi91/its-background-job#readme", + "dependencies": { + "dotenv": "^4.0.0", + "kue": "^0.11.5", + "node-cron": "^1.1.3", + "sendgrid": "^4.8.2" + } +} From 63c2cfceeddbe9cb95f0c33554375a7168b0fbf9 Mon Sep 17 00:00:00 2001 From: Eri Irawan Date: Wed, 15 Mar 2017 14:04:23 +0700 Subject: [PATCH 5/5] add dot env --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 5148e52..24fda24 100644 --- a/.gitignore +++ b/.gitignore @@ -29,9 +29,12 @@ build/Release # Dependency directories node_modules jspm_packages +dump.rdb # Optional npm cache directory .npm # Optional REPL history .node_repl_history + +.env