bamzi / jobrunner

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

Is it possible to specify 'maxInstances' for the job? #4

Closed ghost closed 8 years ago

ghost commented 8 years ago

For example I have "@every 1s" job. But sometimes this job can take time longer than 1 sec. But I don't want more then N running instance of this job one time. Is it possible to do with this library?

bamzi commented 8 years ago

@serge-nikitin if you only have a few functions creating @every 1s instances then it's fairly manageable. However, if you have too many of them then it's best to batch process them. @every 1s is too frequent and can easily become the bottleneck of your system.

Having said that, JobRunner instances join a pool so it's easy to get a count of current active jobs, outstanding jobs and etc.

The following methods are exported by the package so you can reference them inside your own program.

instances := jobrunner.StatusPage() //returns array of current active jobs

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

     // Get the name of the job and count the instances of it
     if job.Name == "JobX" {
         count++
     }
}
if count >= 100 {
    // .... either sleep for 10sec or wait until "job instances" are reduced to 0
    // you logic goes here
}