From a590d58dab988b262a8ff9e38a0f2a5eee45980b Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Tue, 27 Jun 2017 11:08:24 -0300
Subject: [PATCH] Better file structure to handle jobs

---
 src/libs/agenda.js        | 18 ++++++++++++
 src/libs/jobs/download.js | 35 +++++++++++++++++++++++
 src/libs/routes/region.js | 59 ++++++---------------------------------
 3 files changed, 61 insertions(+), 51 deletions(-)
 create mode 100644 src/libs/agenda.js
 create mode 100644 src/libs/jobs/download.js

diff --git a/src/libs/agenda.js b/src/libs/agenda.js
new file mode 100644
index 00000000..1c0ce568
--- /dev/null
+++ b/src/libs/agenda.js
@@ -0,0 +1,18 @@
+const libs = `${process.cwd()}/libs`;
+const config = require(`${libs}/config`);
+
+const Agenda = require('agenda');
+
+const agenda = new Agenda({
+    db: {
+        address: config.monq.uri,
+        collection: 'agendaJobs'}
+});
+
+require(`${libs}/jobs/download`)(agenda);
+
+agenda.on('ready', function() {
+  agenda.start();
+});
+
+module.exports = agenda;
diff --git a/src/libs/jobs/download.js b/src/libs/jobs/download.js
new file mode 100644
index 00000000..bf76afd2
--- /dev/null
+++ b/src/libs/jobs/download.js
@@ -0,0 +1,35 @@
+const libs = `${process.cwd()}/libs`;
+const log = require(`${libs}/log`)(module);
+const execQuery = require(`${libs}/db/query_exec`);
+const jsonexport = require('jsonexport');
+const fs = require('fs');
+
+module.exports = function(agenda) {
+    agenda.define('download database', (job, done) => {
+        let sql = job.attrs.data.sql;
+        execQuery(sql.text, sql.values).then((result) => {
+            log.debug('Got results');
+            jsonexport(result, (err, csv) => {
+                if(err) {
+                    log.error('Error converting to CSV');
+                    log.error(err);
+                    return done(err);
+                }
+                log.debug('Converted to csv');
+                fs.writeFile('out.csv', csv, (err) => {
+                    if(err) {
+                        log.error('Error writing file');
+                        log.error(err);
+                        return done(err);
+                    }
+                    log.info('File created');
+                    done();
+                });
+            });
+        }, (err) => {
+            log.error('Error getting data from database!');
+            log.error(err);
+            return done(err);
+        });
+    });
+}
diff --git a/src/libs/routes/region.js b/src/libs/routes/region.js
index e8e3f3de..5afeea77 100644
--- a/src/libs/routes/region.js
+++ b/src/libs/routes/region.js
@@ -53,56 +53,13 @@ regionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     next();
 }, query, response('region'));
 
-const config = require(`${libs}/config`);
-const Agenda = require('agenda');
-const execQuery = require(`${libs}/db/query_exec`);
-const jsonexport = require('jsonexport');
-const fs = require('fs');
-let agenda = new Agenda({
-    db: {
-        address: config.monq.uri,
-        collection: 'agendaJobs'}
-});
-
-agenda.define('download region', (job, done) => {
-    log.debug('POPEPO');
-    console.log('POPEPO');
-    console.log(job.attrs.data.sql);
-    let sql = job.attrs.data.sql;
-    execQuery(sql.text, sql.values).then((result) => {
-        log.debug(result);
-        jsonexport(result, (err, csv) => {
-            if(err) {
-                log.error('Erro na conversão do json pra csv');
-                log.error(err);
-                return done(err);
-            }
-            console.log(csv);
-            fs.writeFile('out.csv', csv, (err) => {
-                if(err) {
-                    log.error('Erro na escrita do arquivo');
-                    log.error(err);
-                    return done(err);
-                }
-                log.info('Criado arquivo');
-                done();
-            })
-        });
-    }, (error) => {
-        log.error('Error!');
-        done(error);
-    });
-});
-
-agenda.on('ready', function() {
-  agenda.start();
-});
-
-regionApp.get('/download', (req, res) => {
-    req.sql.from('regiao').field('*');
-    let params = req.sql.toParam();
-    agenda.now('download region', {sql: params});
-    res.json({msg: 'Wait for it...'});
-});
+// const agenda = require(`${libs}/agenda`);
+//
+// regionApp.get('/download', (req, res) => {
+//     req.sql.from('regiao').field('*');
+//     let params = req.sql.toParam();
+//     agenda.now('download database', {sql: params});
+//     res.json({msg: 'Wait for it...'});
+// });
 
 module.exports = regionApp;
-- 
GitLab