feat: initialize
This commit is contained in:
71
internal/managed_job/cycle.go
Normal file
71
internal/managed_job/cycle.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package managed_job
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitea.com/logicamp/lc"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (job *ManagedJob) startCycle() {
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
quit := make(chan struct{})
|
||||
|
||||
// load job state from db
|
||||
job.loadState()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
job.cycle()
|
||||
case <-quit:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (job *ManagedJob) cycle() {
|
||||
lc.Logger.Debug("[managed-job] [new] check cycle", zap.String("jobKey", job.def.Key))
|
||||
|
||||
// Init job
|
||||
if job.state == nil {
|
||||
err := job.upload()
|
||||
if err != nil {
|
||||
job.setError("[upload-error] " + err.Error())
|
||||
return
|
||||
}
|
||||
err = job.run()
|
||||
if err != nil {
|
||||
job.setError("[run-error] " + err.Error())
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
// Check for set running or error state
|
||||
if job.state.Status == JobStatusCreating {
|
||||
err := job.checkStatus()
|
||||
if errors.Is(err, ErrNoJobId) {
|
||||
job.state = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if job.state.Status == JobStatusRunning {
|
||||
err := job.checkStatus()
|
||||
if errors.Is(err, ErrNoJobId) {
|
||||
job.state = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//if job.state.LastSavepointDate == nil || time.Now().Add(-time.Minute*3).After(*job.state.LastSavepointDate) {
|
||||
err := job.createSavepoint()
|
||||
if errors.Is(err, ErrNoJobId) {
|
||||
job.state = nil
|
||||
}
|
||||
//}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user