Command line args, moved logger into new module, removed taskmanager logger param, cleanups

pull/1/head
Piero Toffanin 2016-07-29 11:59:58 -05:00
rodzic 647441104e
commit 2b1d7ff058
5 zmienionych plików z 102 dodań i 60 usunięć

Wyświetl plik

@ -1,37 +1,52 @@
/*
Node-OpenDroneMap Node.js App and REST API to access OpenDroneMap.
Copyright (C) 2016 Node-OpenDroneMap Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
// config.js - Configuration for cognicity-server
let argv = require('minimist')(process.argv.slice(2));
if (argv.help){
console.log(`
Usage: node index.js [options]
/**
* Cognicity server configuration object.
* @namespace {object} config
* @property {string} instance The name of this instance of the server
* @property {object} logger Configuration options for logging
* @property {string} logger.level Log level - info, verbose or debug are most useful. Levels are (npm defaults): silly, debug, verbose, info, warn, error.
* @property {number} logger.maxFileSize Maximum size of each log file in bytes
* @property {number} logger.maxFiles Maximum number of log files to keep
* @property {?number} logger.logDirectory Full path to directory to store log files in, if not set logs will be written to the application directory
* @property {number} port Port to launch server on
*/
Options:
-p, --port <number> Port to bind the server to (default: 3000)
--odm_path <path> Path to OpenDroneMap's code (default: /code)
--log_level <logLevel> Set log level verbosity (default: debug)
-d, --deamonize Set process to run as a deamon
Log Levels:
error | debug | info | verbose | debug | silly
`);
process.exit(0);
}
let config = {};
// Instance name - default name for this configuration (will be server process name)
// Instance name - default name for this configuration
config.instance = 'node-OpenDroneMap';
config.odm_path = '/code';
config.odm_path = argv.odm_path || '/code';
// Logging configuration
config.logger = {};
config.logger.level = "debug"; // What level to log at; info, verbose or debug are most useful. Levels are (npm defaults): silly, debug, verbose, info, warn, error.
config.logger.level = argv.log_level || 'debug'; // What level to log at; info, verbose or debug are most useful. Levels are (npm defaults): silly, debug, verbose, info, warn, error.
config.logger.maxFileSize = 1024 * 1024 * 100; // Max file size in bytes of each log file; default 100MB
config.logger.maxFiles = 10; // Max number of log files kept
config.logger.logDirectory = ''; // Set this to a full path to a directory - if not set logs will be written to the application directory.
// Server port
config.port = process.env.PORT || 3000;
// process.env.PORT is what AWS Elastic Beanstalk defines
// on IBM bluemix use config.port = process.env.VCAP_APP_PORT || 8081;
config.port = parseInt(argv.port || argv.p || process.env.PORT || 3000);
config.deamon = argv.deamonize || argv.d;
module.exports = config;

Wyświetl plik

@ -19,9 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
let config = require('./config.js')
let logger = require('winston');
let logger = require('./libs/logger');
let fs = require('fs');
let path = require('path');
let async = require('async');
let express = require('express');
@ -32,40 +31,15 @@ let multer = require('multer');
let bodyParser = require('body-parser');
let morgan = require('morgan');
// Set up logging
// Configure custom File transport to write plain text messages
let logPath = ( config.logger.logDirectory ? config.logger.logDirectory : __dirname );
// Check that log file directory can be written to
try {
fs.accessSync(logPath, fs.W_OK);
} catch (e) {
console.log( "Log directory '" + logPath + "' cannot be written to" );
throw e;
}
logPath += path.sep;
logPath += config.instance + ".log";
logger
.add(logger.transports.File, {
filename: logPath, // Write to projectname.log
json: false, // Write in plain text, not JSON
maxsize: config.logger.maxFileSize, // Max size of each file
maxFiles: config.logger.maxFiles, // Max number of files
level: config.logger.level // Level of log messages
})
// Console transport is no use to us when running as a daemon
.remove(logger.transports.Console);
let TaskManager = require('./libs/TaskManager');
let Task = require('./libs/Task');
let odmOptions = require('./libs/odmOptions');
let winstonStream = {
write: function(message, encoding){
logger.info(message.slice(0, -1));
}
};
let TaskManager = require('./libs/TaskManager');
let Task = require('./libs/Task');
let odmOptions = require('./libs/odmOptions');
app.use(morgan('combined', { stream : winstonStream }));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
@ -215,7 +189,7 @@ let taskManager;
let server;
async.series([
cb => { taskManager = new TaskManager(cb,logger); },
cb => { taskManager = new TaskManager(cb); },
cb => { server = app.listen(config.port, err => {
if (!err) logger.info('Server has started on port ' + String(config.port));
cb(err);

Wyświetl plik

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"use strict";
let assert = require('assert');
let fs = require('fs');
let logger = require('./logger');
let Task = require('./Task');
let statusCodes = require('./statusCodes');
let async = require('async');
@ -28,8 +29,7 @@ const TASKS_DUMP_FILE = "data/tasks.json";
const CLEANUP_TASKS_IF_OLDER_THAN = 1000 * 60 * 60 * 24 * 3; // 3 days
module.exports = class TaskManager{
constructor(done,logger){
this.logger = logger;
constructor(done){
this.tasks = {};
this.runningQueue = [];
@ -57,7 +57,7 @@ module.exports = class TaskManager{
removeOldTasks(done){
let list = [];
let now = new Date().getTime();
this.logger.info("Checking for old tasks to be removed...");
logger.info("Checking for old tasks to be removed...");
for (let uuid in this.tasks){
let task = this.tasks[uuid];
@ -71,7 +71,7 @@ module.exports = class TaskManager{
}
async.eachSeries(list, (uuid, cb) => {
this.logger.info(`Cleaning up old task ${uuid}`)
logger.info(`Cleaning up old task ${uuid}`)
this.remove(uuid, cb);
}, done);
}
@ -91,11 +91,11 @@ module.exports = class TaskManager{
}
});
}, err => {
this.logger.info(`Initialized ${tasks.length} tasks`);
logger.info(`Initialized ${tasks.length} tasks`);
if (done !== undefined) done();
});
}else{
this.logger.info("No tasks dump found");
logger.info("No tasks dump found");
if (done !== undefined) done();
}
});
@ -211,8 +211,8 @@ module.exports = class TaskManager{
}
fs.writeFile(TASKS_DUMP_FILE, JSON.stringify(output), err => {
if (err) this.logger.error(`Could not dump tasks: ${err.message}`);
else this.logger.debug("Dumped tasks list.");
if (err) logger.error(`Could not dump tasks: ${err.message}`);
else logger.debug("Dumped tasks list.");
if (done !== undefined) done();
})
}

52
libs/logger.js 100644
Wyświetl plik

@ -0,0 +1,52 @@
/*
Node-OpenDroneMap Node.js App and REST API to access OpenDroneMap.
Copyright (C) 2016 Node-OpenDroneMap Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let config = require('../config');
let winston = require('winston');
let fs = require('fs');
let path = require('path');
// Set up logging
// Configure custom File transport to write plain text messages
let logPath = ( config.logger.logDirectory ? config.logger.logDirectory : __dirname );
// Check that log file directory can be written to
try {
fs.accessSync(logPath, fs.W_OK);
} catch (e) {
console.log( "Log directory '" + logPath + "' cannot be written to" );
throw e;
}
logPath += path.sep;
logPath += config.instance + ".log";
winston.add(winston.transports.File, {
filename: logPath, // Write to projectname.log
json: false, // Write in plain text, not JSON
maxsize: config.logger.maxFileSize, // Max size of each file
maxFiles: config.logger.maxFiles, // Max number of files
level: config.logger.level // Level of log messages
})
if (config.deamon){
// Console transport is no use to us when running as a daemon
winston.remove(winston.transports.Console);
}
module.exports = winston;

Wyświetl plik

@ -24,6 +24,7 @@
"async": "^2.0.0-rc.6",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"minimist": "^1.2.0",
"morgan": "^1.7.0",
"multer": "^1.1.0",
"node-schedule": "^1.1.1",