Run renews sequentially

This commit is contained in:
Stephen Crosby 2024-01-02 14:04:16 -08:00
parent fe4bd9fed6
commit f7d1c490b3

View File

@ -30,7 +30,7 @@ const internalCertificate = {
intervalTimeout: 1000 * 60 * 60, // 1 hour intervalTimeout: 1000 * 60 * 60, // 1 hour
interval: null, interval: null,
intervalProcessing: false, intervalProcessing: false,
renewBeforeExpirationBy: [7, 'days'], renewBeforeExpirationBy: [30, 'days'],
initTimer: () => { initTimer: () => {
logger.info('Let\'s Encrypt Renewal Timer initialized'); logger.info('Let\'s Encrypt Renewal Timer initialized');
@ -49,7 +49,7 @@ const internalCertificate = {
const expirationThreshold = moment().add(internalCertificate.renewBeforeExpirationBy[0], internalCertificate.renewBeforeExpirationBy[1]).format('YYYY-MM-DD HH:mm:ss'); const expirationThreshold = moment().add(internalCertificate.renewBeforeExpirationBy[0], internalCertificate.renewBeforeExpirationBy[1]).format('YYYY-MM-DD HH:mm:ss');
// Fetch all the letsencrypt certs from the db that will expire within 7 days // Fetch all the letsencrypt certs from the db that will expire within N days
certificateModel certificateModel
.query() .query()
.where('is_deleted', 0) .where('is_deleted', 0)
@ -60,28 +60,32 @@ const internalCertificate = {
return null; return null;
} }
let promises = []; /**
* Renews must be run sequentially or we'll get an error 'Another
* instance of Certbot is already running.'
*/
let sequence = Promise.resolve();
certificates.forEach(function (certificate) { certificates.forEach(function (certificate) {
const promise = internalCertificate sequence = sequence.then(() =>
.renew( internalCertificate
{ .renew(
can: () => {
Promise.resolve({ can: () =>
permission_visibility: 'all', Promise.resolve({
}), permission_visibility: 'all',
}, }),
{ id: certificate.id }, },
) { id: certificate.id },
.catch((err) => { )
// Don't want to stop the train here, just log the error .catch((err) => {
logger.error(err.message); // Don't want to stop the train here, just log the error
}); logger.error(err.message);
}),
promises.push(promise); );
}); });
return Promise.all(promises); return sequence;
}) })
.then(() => { .then(() => {
internalCertificate.intervalProcessing = false; internalCertificate.intervalProcessing = false;