wildduck/worker.js

120 lines
5 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 lmtp = require('./lmtp');
const api = require('./api');
2021-06-15 15:47:18 +08:00
const acme = require('./acme');
2018-10-10 21:19:20 +08:00
const tasks = require('./tasks');
2020-10-09 16:08:33 +08:00
const webhooks = require('./webhooks');
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);
}
2018-10-10 21:19:20 +08:00
tasks.start(err => {
if (err) {
2018-10-10 21:19:20 +08:00
log.error('App', 'Failed to start task runner. %s', err.message);
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2018-10-10 21:19:20 +08:00
2020-10-09 16:08:33 +08:00
webhooks.start(err => {
if (err) {
2020-10-09 16:08:33 +08:00
log.error('App', 'Failed to start webhook runner. %s', err.message);
2017-08-29 15:36:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2020-10-09 16:08:33 +08:00
// Start IMAP server
imap(err => {
if (err) {
2020-10-09 16:08:33 +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);
}
2020-10-09 16:08:33 +08:00
// Start POP3 server
pop3(err => {
2017-04-08 02:29:14 +08:00
if (err) {
2020-10-09 16:08:33 +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);
}
2020-10-09 16:08:33 +08:00
// Start LMTP maildrop server
lmtp(err => {
2017-11-06 23:32:45 +08:00
if (err) {
2020-10-09 16:08:33 +08:00
log.error('App', 'Failed to start LMTP server');
2017-11-06 23:32:45 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2017-10-26 19:57:19 +08:00
2020-10-09 16:08:33 +08:00
// Start HTTP API server
api(err => {
2018-10-10 21:19:20 +08:00
if (err) {
2020-10-09 16:08:33 +08:00
log.error('App', 'Failed to start API server');
2018-10-10 21:19:20 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2021-06-15 15:47:18 +08:00
// Start HTTP ACME server
acme(err => {
2020-10-09 16:08:33 +08:00
if (err) {
2021-06-15 15:47:18 +08:00
log.error('App', 'Failed to start ACME server');
2020-10-09 16:08:33 +08:00
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
2021-06-15 15:47:18 +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);
}
}
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);
}
}
plugins.init(err => {
if (err) {
log.error('App', 'Failed to start plugins');
errors.notify(err);
return setTimeout(() => process.exit(1), 3000);
}
plugins.runHooks('init', () => {
log.info('App', 'All servers started, ready to process some mail');
});
2020-10-09 16:08:33 +08:00
});
2018-10-10 21:19:20 +08:00
});
2017-10-26 15:21:32 +08:00
});
});
2017-04-08 02:29:14 +08:00
});
});
});
});
});