diff --git a/errand-routes.go b/errand-routes.go index 48dd58f..67a52c0 100644 --- a/errand-routes.go +++ b/errand-routes.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "net/http" gin "github.com/gin-gonic/gin" @@ -88,7 +89,7 @@ func (s *ErrandsServer) failedErrand(c *gin.Context) { return err } errand.Failed = utils.GetTimestamp() - errand.Status = "failed" + errand.Status = schemas.StatusFailed errand.Progress = 0 if errand.Options.Retries > 0 { // If we should retry this errand: @@ -148,7 +149,7 @@ func (s *ErrandsServer) completeErrand(c *gin.Context) { return err } errand.Completed = utils.GetTimestamp() - errand.Status = "completed" + errand.Status = schemas.StatusCompleted errand.Progress = 100 // errand.Results = compReq.Results // If we should delete this errand upon completion: @@ -263,7 +264,7 @@ func (s *ErrandsServer) UpdateErrandByID(id string, fn func(*schemas.Errand) err errand := errandObj.(schemas.Errand) if err := fn(&errand); err != nil { - return nil, errors.New("error in given update function (fn)") + return nil, fmt.Errorf("error in given update function (fn): %w", err) } s.updateErrandInPipeline(&errand) diff --git a/errands-routes.go b/errands-routes.go index 7c890dd..e3039b3 100644 --- a/errands-routes.go +++ b/errands-routes.go @@ -223,18 +223,21 @@ func (s *ErrandsServer) processErrand(c *gin.Context) { procErrand = errands[0] - // We are processing this errand: - procErrand.Started = utils.GetTimestamp() - procErrand.Attempts += 1 - procErrand.Status = "active" - procErrand.Progress = 0.0 - _ = procErrand.AddToLogs("INFO", "Started!") - _ = s.saveErrand(&procErrand) - - s.AddNotification("processing", &procErrand) + // We are processing this errand. This won't ever return error + updatedErrand, _ := s.UpdateErrandByID(procErrand.ID, func(errand *schemas.Errand) error { + errand.Started = utils.GetTimestamp() + errand.Attempts++ + errand.Status = schemas.StatusActive + errand.Progress = 0.0 + + _ = errand.AddToLogs("INFO", "Started!") + return nil + }) + + s.AddNotification("processing", updatedErrand) c.JSON(http.StatusOK, gin.H{ "status": "OK", - "results": procErrand, + "results": updatedErrand, }) } diff --git a/errands-server.go b/errands-server.go index c6acea8..9c48f17 100644 --- a/errands-server.go +++ b/errands-server.go @@ -62,12 +62,14 @@ func NewErrandsServer(cfg *Config) *ErrandsServer { go obj.broadcastLoop() if err := obj.ErrandStore.LoadFile(path.Join(cfg.Storage, errandsDBPathSuffix)); err != nil { + log.WithError(err).Error("unable to load errands db") log.Warning("Could not load errand data from previous DB file.") log.Warning("If this is your first time running, this is normal.") log.Warning("If not please check the contents of your file: ", cfg.Storage) } if err := obj.ErrandStore.LoadFile(path.Join(cfg.Storage, pipelinesDBPathSuffix)); err != nil { + log.WithError(err).Error("unable to load pipelines db") log.Warning("Could not load pipeline data from previous DB file.") log.Warning("If this is your first time running, this is normal.") log.Warning("If not please check the contents of your file: ", cfg.Storage) diff --git a/pipeline-routes.go b/pipeline-routes.go index 75703e9..4518053 100644 --- a/pipeline-routes.go +++ b/pipeline-routes.go @@ -53,6 +53,7 @@ func (s *ErrandsServer) createPipeline(c *gin.Context) { // Initialize all the errands in the pipeline for _, errand := range pipeline.Errands { errand.SetDefaults() + errand.PipelineID = pipeline.ID errand.Status = schemas.StatusBlocked if err := s.saveErrand(errand); err != nil { diff --git a/schemas/pipeline.go b/schemas/pipeline.go index 15445ab..404f2c7 100644 --- a/schemas/pipeline.go +++ b/schemas/pipeline.go @@ -105,7 +105,7 @@ func (p *Pipeline) RecalculateStatus() { p.Status = StatusActive } - if p.Status == StatusCompleted { + if errand.Status == StatusCompleted { numCompleted++ } }