wildduck/worker.js

101 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
2017-07-16 19:37:33 +08:00
const config = require('wild-config');
2017-06-03 14:51:58 +08:00
const log = require('npmlog');
const imap = require('./imap');
const pop3 = require('./pop3');
const irc = require('./irc');
2017-06-03 14:51:58 +08:00
const lmtp = require('./lmtp');
const api = require('./api');
2017-10-26 15:21:32 +08:00
const plugins = require('./lib/plugins');
2017-06-03 14:51:58 +08:00
const db = require('./lib/db');
2017-08-29 15:36:45 +08:00
const errors = require('./lib/errors');
2017-07-16 19:37:33 +08:00
// preload certificate files
require('./lib/certs');
// Initialize database connection
db.connect(err => {
if (err) {
log.error('Db', 'Failed to setup database connection');
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
// Start IMAP server
imap(err => {
if (err) {
2017-08-15 21:41:18 +08:00
log.error('App', 'Failed to start IMAP server. %s', err.message);
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-04-08 02:29:14 +08:00
// Start POP3 server
pop3(err => {
if (err) {
2017-04-08 02:29:14 +08:00
log.error('App', 'Failed to start POP3 server');
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-04-12 19:52:29 +08:00
// Start LMTP maildrop server
lmtp(err => {
if (err) {
2017-04-12 19:52:29 +08:00
log.error('App', 'Failed to start LMTP server');
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-04-08 02:29:14 +08:00
// Start HTTP API server
api(err => {
if (err) {
log.error('App', 'Failed to start API server');
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-04-08 02:29:14 +08:00
// Start IRC server
irc(err => {
if (err) {
log.error('App', 'Failed to start IRC server');
errors.notify(err);
2017-08-29 15:36:45 +08:00
return setTimeout(() => process.exit(1), 3000);
2017-04-08 02:29:14 +08:00
}
// downgrade user and group if needed
if (config.group) {
try {
process.setgid(config.group);
log.info('App', 'Changed group to "%s" (%s)', config.group, process.getgid());
} catch (E) {
log.error('App', 'Failed to change group to "%s" (%s)', config.group, E.message);
errors.notify(E);
return setTimeout(() => process.exit(1), 3000);
}
2017-04-08 02:29:14 +08:00
}
if (config.user) {
try {
process.setuid(config.user);
log.info('App', 'Changed user to "%s" (%s)', config.user, process.getuid());
} catch (E) {
log.error('App', 'Failed to change user to "%s" (%s)', config.user, E.message);
errors.notify(E);
return setTimeout(() => process.exit(1), 3000);
}
}
2017-10-26 15:21:32 +08:00
2017-10-26 19:57:19 +08:00
plugins.init(err => {
2017-10-26 15:21:32 +08:00
if (err) {
log.error('App', 'Failed to start plugins');
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-10-26 19:57:19 +08:00
plugins.runHooks('init', () => {
log.info('App', 'All servers started, ready to process some mail');
});
2017-10-26 15:21:32 +08:00
});
});
2017-04-08 02:29:14 +08:00
});
});
});
});
});