Skip to content

Commit d2149af

Browse files
committed
Run renews sequentially
1 parent c17273a commit d2149af

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

backend/internal/certificate.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const internalCertificate = {
3030
intervalTimeout: 1000 * 60 * 60, // 1 hour
3131
interval: null,
3232
intervalProcessing: false,
33-
renewBeforeExpirationBy: [7, 'days'],
33+
renewBeforeExpirationBy: [30, 'days'],
3434

3535
initTimer: () => {
3636
logger.info('Let\'s Encrypt Renewal Timer initialized');
@@ -49,7 +49,7 @@ const internalCertificate = {
4949

5050
const expirationThreshold = moment().add(internalCertificate.renewBeforeExpirationBy[0], internalCertificate.renewBeforeExpirationBy[1]).format('YYYY-MM-DD HH:mm:ss');
5151

52-
// Fetch all the letsencrypt certs from the db that will expire within 7 days
52+
// Fetch all the letsencrypt certs from the db that will expire within N days
5353
certificateModel
5454
.query()
5555
.where('is_deleted', 0)
@@ -60,28 +60,32 @@ const internalCertificate = {
6060
return null;
6161
}
6262

63-
let promises = [];
63+
/**
64+
* Renews must be run sequentially or we'll get an error 'Another
65+
* instance of Certbot is already running.'
66+
*/
67+
let sequence = Promise.resolve();
6468

6569
certificates.forEach(function (certificate) {
66-
const promise = internalCertificate
67-
.renew(
68-
{
69-
can: () =>
70-
Promise.resolve({
71-
permission_visibility: 'all',
72-
}),
73-
},
74-
{ id: certificate.id },
75-
)
76-
.catch((err) => {
77-
// Don't want to stop the train here, just log the error
78-
logger.error(err.message);
79-
});
80-
81-
promises.push(promise);
70+
sequence = sequence.then(() =>
71+
internalCertificate
72+
.renew(
73+
{
74+
can: () =>
75+
Promise.resolve({
76+
permission_visibility: 'all',
77+
}),
78+
},
79+
{ id: certificate.id },
80+
)
81+
.catch((err) => {
82+
// Don't want to stop the train here, just log the error
83+
logger.error(err.message);
84+
}),
85+
);
8286
});
8387

84-
return Promise.all(promises);
88+
return sequence;
8589
})
8690
.then(() => {
8791
internalCertificate.intervalProcessing = false;

0 commit comments

Comments
 (0)