kopia lustrzana https://github.com/OpenDroneMap/NodeODM
Command line args, moved logger into new module, removed taskmanager logger param, cleanups
rodzic
647441104e
commit
2b1d7ff058
57
config.js
57
config.js
|
@ -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';
|
'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]
|
||||||
|
|
||||||
/**
|
Options:
|
||||||
* Cognicity server configuration object.
|
-p, --port <number> Port to bind the server to (default: 3000)
|
||||||
* @namespace {object} config
|
--odm_path <path> Path to OpenDroneMap's code (default: /code)
|
||||||
* @property {string} instance The name of this instance of the server
|
--log_level <logLevel> Set log level verbosity (default: debug)
|
||||||
* @property {object} logger Configuration options for logging
|
-d, --deamonize Set process to run as a deamon
|
||||||
* @property {string} logger.level Log level - info, verbose or debug are most useful. Levels are (npm defaults): silly, debug, verbose, info, warn, error.
|
Log Levels:
|
||||||
* @property {number} logger.maxFileSize Maximum size of each log file in bytes
|
error | debug | info | verbose | debug | silly
|
||||||
* @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
|
process.exit(0);
|
||||||
* @property {number} port Port to launch server on
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
let config = {};
|
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.instance = 'node-OpenDroneMap';
|
||||||
|
config.odm_path = argv.odm_path || '/code';
|
||||||
config.odm_path = '/code';
|
|
||||||
|
|
||||||
|
|
||||||
// Logging configuration
|
// Logging configuration
|
||||||
config.logger = {};
|
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.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.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.
|
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 = parseInt(argv.port || argv.p || process.env.PORT || 3000);
|
||||||
config.port = process.env.PORT || 3000;
|
config.deamon = argv.deamonize || argv.d;
|
||||||
// process.env.PORT is what AWS Elastic Beanstalk defines
|
|
||||||
// on IBM bluemix use config.port = process.env.VCAP_APP_PORT || 8081;
|
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|
36
index.js
36
index.js
|
@ -19,9 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
let config = require('./config.js')
|
let config = require('./config.js')
|
||||||
|
|
||||||
let logger = require('winston');
|
let logger = require('./libs/logger');
|
||||||
let fs = require('fs');
|
let fs = require('fs');
|
||||||
let path = require('path');
|
|
||||||
let async = require('async');
|
let async = require('async');
|
||||||
|
|
||||||
let express = require('express');
|
let express = require('express');
|
||||||
|
@ -32,40 +31,15 @@ let multer = require('multer');
|
||||||
let bodyParser = require('body-parser');
|
let bodyParser = require('body-parser');
|
||||||
let morgan = require('morgan');
|
let morgan = require('morgan');
|
||||||
|
|
||||||
// Set up logging
|
let TaskManager = require('./libs/TaskManager');
|
||||||
// Configure custom File transport to write plain text messages
|
let Task = require('./libs/Task');
|
||||||
let logPath = ( config.logger.logDirectory ? config.logger.logDirectory : __dirname );
|
let odmOptions = require('./libs/odmOptions');
|
||||||
// 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 winstonStream = {
|
let winstonStream = {
|
||||||
write: function(message, encoding){
|
write: function(message, encoding){
|
||||||
logger.info(message.slice(0, -1));
|
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(morgan('combined', { stream : winstonStream }));
|
||||||
app.use(bodyParser.urlencoded({extended: true}));
|
app.use(bodyParser.urlencoded({extended: true}));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
@ -215,7 +189,7 @@ let taskManager;
|
||||||
let server;
|
let server;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
cb => { taskManager = new TaskManager(cb,logger); },
|
cb => { taskManager = new TaskManager(cb); },
|
||||||
cb => { server = app.listen(config.port, err => {
|
cb => { server = app.listen(config.port, err => {
|
||||||
if (!err) logger.info('Server has started on port ' + String(config.port));
|
if (!err) logger.info('Server has started on port ' + String(config.port));
|
||||||
cb(err);
|
cb(err);
|
||||||
|
|
|
@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"use strict";
|
"use strict";
|
||||||
let assert = require('assert');
|
let assert = require('assert');
|
||||||
let fs = require('fs');
|
let fs = require('fs');
|
||||||
|
let logger = require('./logger');
|
||||||
let Task = require('./Task');
|
let Task = require('./Task');
|
||||||
let statusCodes = require('./statusCodes');
|
let statusCodes = require('./statusCodes');
|
||||||
let async = require('async');
|
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
|
const CLEANUP_TASKS_IF_OLDER_THAN = 1000 * 60 * 60 * 24 * 3; // 3 days
|
||||||
|
|
||||||
module.exports = class TaskManager{
|
module.exports = class TaskManager{
|
||||||
constructor(done,logger){
|
constructor(done){
|
||||||
this.logger = logger;
|
|
||||||
this.tasks = {};
|
this.tasks = {};
|
||||||
this.runningQueue = [];
|
this.runningQueue = [];
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ module.exports = class TaskManager{
|
||||||
removeOldTasks(done){
|
removeOldTasks(done){
|
||||||
let list = [];
|
let list = [];
|
||||||
let now = new Date().getTime();
|
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){
|
for (let uuid in this.tasks){
|
||||||
let task = this.tasks[uuid];
|
let task = this.tasks[uuid];
|
||||||
|
@ -71,7 +71,7 @@ module.exports = class TaskManager{
|
||||||
}
|
}
|
||||||
|
|
||||||
async.eachSeries(list, (uuid, cb) => {
|
async.eachSeries(list, (uuid, cb) => {
|
||||||
this.logger.info(`Cleaning up old task ${uuid}`)
|
logger.info(`Cleaning up old task ${uuid}`)
|
||||||
this.remove(uuid, cb);
|
this.remove(uuid, cb);
|
||||||
}, done);
|
}, done);
|
||||||
}
|
}
|
||||||
|
@ -91,11 +91,11 @@ module.exports = class TaskManager{
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, err => {
|
}, err => {
|
||||||
this.logger.info(`Initialized ${tasks.length} tasks`);
|
logger.info(`Initialized ${tasks.length} tasks`);
|
||||||
if (done !== undefined) done();
|
if (done !== undefined) done();
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
this.logger.info("No tasks dump found");
|
logger.info("No tasks dump found");
|
||||||
if (done !== undefined) done();
|
if (done !== undefined) done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -211,8 +211,8 @@ module.exports = class TaskManager{
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(TASKS_DUMP_FILE, JSON.stringify(output), err => {
|
fs.writeFile(TASKS_DUMP_FILE, JSON.stringify(output), err => {
|
||||||
if (err) this.logger.error(`Could not dump tasks: ${err.message}`);
|
if (err) logger.error(`Could not dump tasks: ${err.message}`);
|
||||||
else this.logger.debug("Dumped tasks list.");
|
else logger.debug("Dumped tasks list.");
|
||||||
if (done !== undefined) done();
|
if (done !== undefined) done();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
@ -24,6 +24,7 @@
|
||||||
"async": "^2.0.0-rc.6",
|
"async": "^2.0.0-rc.6",
|
||||||
"body-parser": "^1.15.2",
|
"body-parser": "^1.15.2",
|
||||||
"express": "^4.14.0",
|
"express": "^4.14.0",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
"multer": "^1.1.0",
|
"multer": "^1.1.0",
|
||||||
"node-schedule": "^1.1.1",
|
"node-schedule": "^1.1.1",
|
||||||
|
|
Ładowanie…
Reference in New Issue