bamzi / jobrunner

Framework for performing work asynchronously, outside of the request flow
MIT License
1.04k stars 99 forks source link

option to force job execution? #5

Closed cytec closed 7 years ago

cytec commented 8 years ago

Is there any option to force the job execution manually?

currently im doing something like this

jobrunner.Start() // optional: jobrunner.Start(pool int, concurrent int) (10, 1)
jobrunner.Schedule("@every 30s", myJob{})

routes.GET("/jobrunner/force", forceJob)

routes.Run(":8080")

func forceJob(c *gin.Context) {

    instances := jobrunner.StatusPage()

    for k, _ := range instances {
         job := instances[k].JobRunner

         if job.Name == "myJob" {
             job.Run()
         }
    }
}

which kinda does what i want: run the job... but it doesn't update the prev and next time when the job should be run... so it's more like a 20% success...

what i'm looking for is:

Force the execution of the Job right now... but update the jobrunner to know that the job is running now and it should therefore be run again in (in my example) 30s from the end of the current run.

what i'm doing at the moment can result in the job getting executed by the jobrunner again while it is still running

bamzi commented 8 years ago

If understand your case properly, I think something like this would be better:

routes.GET("/jobrunner/force", forceJob)
func forceJob(c *gin.Context) {
     // optionally remove the old scheduled job
     // ...
     // id :=  instances[k].Id  // --> cron.EntryID
     // jobrunner.Remove(id)  

     jobrunner.Now(myJob{}) // do myJob as soon as possible (one time)

    // add a new scheduled job
     jobrunner.Schedule("@every 30s", myJob{})
}

However, if you're looking to skip a wait schedule and trigger the job action immediately as your sample code indicates then the issue of 20% success is because of jobrunner.Run() -> sync.Mutex which gets locked when running a job and once done it gets unlocked ... so when a new job is forced mid sync.Mutex lock, then pool of jobs never get updated properly.

cytec commented 8 years ago

thanks for your reply :)

haven't thought about just removing, running it manually and then readding the job :D i think thats an ok'is solution for me... "real" forcing would be cool but for my case the solution you posted should be good too, so thanks again.

Only one question left: the status page gives me RUNNING status for every job which has already run at least once... no matter if it is currently running or not... new jobs start without any status and then switch to Running state after/mid first execution. Any more info on when a job is considered IDLE and RUNNING? my thought was that RUNNING is only present as long as the job is actually doing some work, once it finishes the state should switch to IDLE or something like that