2021-08-17 01:12:07 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/scheduler"
|
|
|
|
)
|
|
|
|
|
|
|
|
func StartStackSchedules(scheduler *scheduler.Scheduler, stackdeployer StackDeployer, datastore portainer.DataStore, gitService portainer.GitService) error {
|
|
|
|
stacks, err := datastore.Stack().RefreshableStacks()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to fetch refreshable stacks")
|
|
|
|
}
|
|
|
|
for _, stack := range stacks {
|
|
|
|
d, err := time.ParseDuration(stack.AutoUpdate.Interval)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Unable to parse auto update interval")
|
|
|
|
}
|
2021-09-29 23:58:10 +00:00
|
|
|
stackID := stack.ID // to be captured by the scheduled function
|
|
|
|
jobID := scheduler.StartJobEvery(d, func() error {
|
|
|
|
return RedeployWhenChanged(stackID, stackdeployer, datastore, gitService)
|
2021-08-17 01:12:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
stack.AutoUpdate.JobID = jobID
|
|
|
|
if err := datastore.Stack().UpdateStack(stack.ID, &stack); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to update stack job id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|