Skip to content

Commit

Permalink
fix disk write scheduling to only run when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtwallace committed Jan 10, 2024
1 parent 8858ed0 commit 7fb901d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 15 additions & 4 deletions pkg/main/https_server_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,21 @@ func (app *app) postKeyAndCert(w http.ResponseWriter, r *http.Request) {
return
}

// schedule file update (which will cancel any other pending job, since it isn't needed when
// new payload is received from server via this handler) -- if files on disc don't actually
// need an update, logic will deal with it later in the job (and just not write files)
app.scheduleJobWriteCertsMemoryToDisk()
// run go routine to update files; first run update immediately to check for missing files
// which also returns if the disk needs an update. Then schedule job if the disk needs an
// update. If no disk update is needed, ensure cancel any old pending job.
go func() {
// write files to disk now if file(s) are missing
diskNeedsUpdate := app.updateCertFilesAndRestartContainers(true)

// schedule job if disk still needs an update
if diskNeedsUpdate {
app.scheduleJobWriteCertsMemoryToDisk()
} else if app.pendingJobCancel != nil {
// cancel any old pending job if no update needed and there is a job to cancel
app.pendingJobCancel()
}
}()

w.WriteHeader(http.StatusOK)
}
15 changes: 7 additions & 8 deletions pkg/main/update_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ func (app *app) nextFileUpdateWindowStart() time.Time {
return nextWindow.Add(time.Duration(addDays) * 24 * time.Hour)
}

// scheduleJobWriteCertsMemoryToDisk schedules a job to write the new cert files
// using data already loaded into the client cert. This is used when the client
// launched with valid key/cert files but newer ones were found on the lego
// server. If the current time is within a permitted write time, the job runs
// immediately
// scheduleJobWriteCertsMemoryToDisk schedules a job to write the lego client's
// key/cert pem from memory to disk (and generate any additional files on disk that
// are configured)
func (app *app) scheduleJobWriteCertsMemoryToDisk() {
go func() {
// cancel any old job
Expand Down Expand Up @@ -147,9 +145,10 @@ func (app *app) scheduleJobWriteCertsMemoryToDisk() {
}()
}

// scheduleJobFetchCertsAndWriteToDisk fetches the latest key/cert from LeGo
// updates the client's key/cert. It repeats this task every 15 minutes until
// it succeeds. Then it schedules a job to write the new files to disk.
// scheduleJobFetchCertsAndWriteToDisk fetches the latest key/cert from LeGo server
// and updates the client's key/cert. It repeats this task every 15 minutes until
// it succeeds. Then it schedules a job to write lego client's key/cert pem from
// memory to disk (along with any other files that are configured).
func (app *app) scheduleJobFetchCertsAndWriteToDisk() {
go func() {
// cancel any old job
Expand Down

0 comments on commit 7fb901d

Please sign in to comment.