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 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') +}) 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) + }) +}) 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) +}) 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" + } +}