Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow maximum number of workers to be configured per job type. #7

Merged
merged 1 commit into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ it.

If you don't want more than one job running at a time then set this to 1.

Beyond this global scope you can adjust the total number of workers on each
individual Resque Job type by overriding the `max_workers` class method for the job.
If you change this, the value returned by that method takes precedence over the
global value.

```ruby
class ResourceIntensiveJob
extend Resque::Kubernetes::Job

def perform
# ...
end

def job_manifest
# ...
end

def max_workers
# Simply return an integer value, or do something more complicated if needed.
105
end
end
```

## To Do

- We probably need better namespace support, particularly for reaping
Expand Down
28 changes: 27 additions & 1 deletion lib/resque/kubernetes/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ def before_enqueue_kubernetes_job(*_)
apply_kubernetes_job
end

protected

# Return the maximum number of workers to autoscale the job to.
#
# While the number of active Kubernetes Jobs is less than this number,
# the gem will add new Jobs to auto-scale the workers.
#
# By default, this returns `Resque::Kubernetes.max_workers` from the gem
# configuration. You may override this method to return any other value,
# either as a simple integer or with some complex logic.
#
# Example:
# def max_workers
# # A simple integer
# 105
# end
#
# Example:
# def max_workers
# # Scale based on time of day
# Time.now.hour < 8 ? 15 : 5
# end
def max_workers
Resque::Kubernetes.max_workers
end

private

def jobs_client
Expand Down Expand Up @@ -98,7 +124,7 @@ def jobs_maxed?(name, namespace)
namespace: namespace
)
running = resque_jobs.reject { |job| job.spec.completions == job.status.succeeded }
running.size == Resque::Kubernetes.max_workers
running.size == max_workers
end

def adjust_manifest(manifest)
Expand Down
Loading