Commit d74634b5cf37965c8d4963fa63abce5ea1be97ff
1 parent
7a2b3320
Exists in
master
and in
1 other branch
adiciona cron e unirest pra poder lidar com o update da saude em services.log
Showing
5 changed files
with
55 additions
and
4 deletions
Show diff stats
logsystem/main.coffee
| ... | ... | @@ -50,3 +50,15 @@ exports.incrementService = (serviceType, type, inc=1) -> |
| 50 | 50 | |
| 51 | 51 | |
| 52 | 52 | writeLog(services, services_log_path) |
| 53 | + | |
| 54 | +exports.updateHealth = (serviceType="outros", value=0) -> | |
| 55 | + services_log_path = "./logsystem/services.log" | |
| 56 | + services = JSON.parse(fs.readFileSync(services_log_path, 'utf8')) | |
| 57 | + | |
| 58 | + # if e else if pra excluir valores diferente desses dois | |
| 59 | + if serviceType == "videos" | |
| 60 | + services["tipo"]["videos"]["saude"] = value | |
| 61 | + else if serviceType == "outros" | |
| 62 | + services["tipo"]["outros"]["saude"] = value | |
| 63 | + | |
| 64 | + writeLog(services, services_log_path) | ... | ... |
logsystem/main.js
| ... | ... | @@ -50,4 +50,22 @@ |
| 50 | 50 | return writeLog(services, services_log_path); |
| 51 | 51 | }; |
| 52 | 52 | |
| 53 | + exports.updateHealth = function(serviceType, value) { | |
| 54 | + var services, services_log_path; | |
| 55 | + if (serviceType == null) { | |
| 56 | + serviceType = "outros"; | |
| 57 | + } | |
| 58 | + if (value == null) { | |
| 59 | + value = 0; | |
| 60 | + } | |
| 61 | + services_log_path = "./logsystem/services.log"; | |
| 62 | + services = JSON.parse(fs.readFileSync(services_log_path, 'utf8')); | |
| 63 | + if (serviceType === "videos") { | |
| 64 | + services["tipo"]["videos"]["saude"] = value; | |
| 65 | + } else if (serviceType === "outros") { | |
| 66 | + services["tipo"]["outros"]["saude"] = value; | |
| 67 | + } | |
| 68 | + return writeLog(services, services_log_path); | |
| 69 | + }; | |
| 70 | + | |
| 53 | 71 | }).call(this); | ... | ... |
logsystem/services.log
package.json
server.js
| ... | ... | @@ -19,6 +19,7 @@ var config = require('./config/main.js'); |
| 19 | 19 | var logger = require('./logsystem/main.js'); |
| 20 | 20 | var kue = require('kue'); |
| 21 | 21 | var queue = kue.createQueue(); |
| 22 | +var unirest = require('unirest'); | |
| 22 | 23 | |
| 23 | 24 | app.use(express.static(path.join(__dirname, '/videos'))); |
| 24 | 25 | app.use(express.bodyParser({ keepExtensions: true, uploadDir: path.join(__dirname, '/uploads') })); |
| ... | ... | @@ -170,3 +171,21 @@ app.get('/*', function(req, res){ |
| 170 | 171 | app.listen(properties.port, properties.host, function(){ |
| 171 | 172 | console.log('Server running on ' + properties.host + ':' + properties.port); |
| 172 | 173 | }); |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | +var CronJob = require('cron').CronJob; | |
| 178 | +// '* * * * * *' == a cada 1 segundo | |
| 179 | +new CronJob('* * * * * *', function() { | |
| 180 | + unirest.post('http://localhost:5000/api') | |
| 181 | + .header('Accept', 'application/json') | |
| 182 | + .send({ "servico": "texto", "transparencia": "opaco", "texto": "texto teste" }) | |
| 183 | + .end(function (response) { | |
| 184 | + console.log(response.status); | |
| 185 | + if(response.status === 200){ | |
| 186 | + logger.updateHealth("outros", 1); | |
| 187 | + } else { | |
| 188 | + logger.updateHealth(); | |
| 189 | + } | |
| 190 | + }); | |
| 191 | +}, null, true); // no lugar do null pode ser uma funcao pra executar quando parar | ... | ... |