workless_revived 1.2.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98e47054154aee4abcc5275e895a0ce1695370b4
4
- data.tar.gz: 3d1aacf639ccafff4f39b05932cdd2a57fd3121e
3
+ metadata.gz: ef58f3d586079df9d5adf482bae320be8dd2ac8b
4
+ data.tar.gz: 719f686eb694190dd6f7020ec325ecbccb2a979d
5
5
  SHA512:
6
- metadata.gz: 23bf9f4b9e0d9cdebf6f731d87f19d574ebdb99f59dc5130875b7a6719a853958dd5336ad3fe09ad480e44717da504fb38b224d13025917ac3b87d4dd72a67b4
7
- data.tar.gz: c27916acaa6088249f0d264be8f3ea78c38100c90eedcccfd61191be47eaead40147d32dee2bf2b2c593bce91bc0464f87aef894aefde780616d63b57665fb3b
6
+ metadata.gz: 1a260a84ed1ced0d76d84bd97a818af27a8ae225933d5f79ae96637291ca7da31c58227caedba4d0fb1edfbeefcb121006e6e9ec82d6183e7dc0766aa4442ed7
7
+ data.tar.gz: b023d243fc50803b11a879afe83cfc75e30856ee705620b984fb1773ce73f343171dd5672a298b98fd6976c1971fbcbf9a918fa8f6b6590999fe8bef28a5accb
data/README.md CHANGED
@@ -11,8 +11,13 @@ It is designed to be used when you're using Heroku as a host and have the need t
11
11
 
12
12
  By adding the gem to your project and configuring our Heroku app with some config variables workless should do the rest.
13
13
 
14
+ :warning: **[The Legacy API will be sunset on April 15th, 2017](https://devcenter.heroku.com/changelog-items/862)** :warning:
15
+ Please upgrade to version 2.0.0 as soon as you can. Version 2.0.0 is released on March 1st, 2017.
16
+
14
17
  ## Updates
15
18
 
19
+ * Version 2.0.0 Updated to use latest version of the Heroku API. Drops support for old style Heroku
20
+ * Version 1.3.0 DROPS SUPPORT FOR OLDER RUBY AND RAILS VERSIONS!
16
21
  * Version 1.2.5 Added middleware to check on delayed jobs, fixed Rails 5 support
17
22
  * Version 1.2.4 drops support for older versions!
18
23
  * Version 1.2.3 replaces multiple commit callback with two callbacks for compatibility by @lostboy
@@ -31,21 +36,13 @@ Workless should work correctly with Rubies 2.0.0 and up. It is compatible with D
31
36
 
32
37
  Ruby | Rails | Delayed Job
33
38
  ---------- | ------ | -----
34
- 2.0.0 | 3.2.22 | 2.0.7
35
- 2.2.4 | 4.2 | 2.1.4
36
- 2.3.0 | 5.0 | 4.1.2
39
+ 2.2.5 | 4.2 | 2.1.4
40
+ 2.3.1 | 5.0 | 4.1.2
37
41
 
38
42
  ## Installation
39
43
 
40
44
  Add the workless gem and the delayed_job gem to your project Gemfile and update your bundle. Its is recommended to specify the gem version for delayed_job
41
45
 
42
- ### For rails 3.x with latest delayed_job 3.x using active record
43
-
44
- <pre>
45
- gem "delayed_job_active_record"
46
- gem "workless", "~> 1.1.3"
47
- </pre>
48
-
49
46
  ### For rails 4.x with latest delayed_job 3.x using active record
50
47
 
51
48
  <pre>
@@ -87,7 +84,7 @@ There are three other scalers included. Note that if you are running on the Aspe
87
84
 
88
85
  <pre>
89
86
  Delayed::Job.scaler = :heroku
90
- Delayed::Job.scaler = :heroku_cedar
87
+ Delayed::Job.scaler = :heroku
91
88
  Delayed::Job.scaler = :local
92
89
  </pre>
93
90
 
@@ -1,4 +1,4 @@
1
- Delayed::Worker.max_attempts = 3
1
+ Delayed::Worker.max_attempts ||= 3
2
2
  Delayed::Backend::ActiveRecord::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::ActiveRecord::Job)
3
3
  Delayed::Backend::Mongoid::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::Mongoid::Job)
4
4
  Delayed::Backend::MongoMapper::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::MongoMapper::Job)
@@ -7,14 +7,9 @@ class WorklessChecker
7
7
  status, headers, response = @app.call(env)
8
8
  return [status, headers, response] if file?(headers) || empty?(response)
9
9
 
10
- Delayed::Job.scaler.up if Delayed::Job.scaler.jobs.size > 0
11
- response_body = nil
12
- if status == 200 && !response.body.frozen? && html_request?(headers, response)
13
- response_body = response.body << "\n<!-- workless jobs: #{Delayed::Job.scaler.jobs.size} -->"
14
- headers['Content-Length'] = response_body.bytesize.to_s
15
- end
10
+ Delayed::Job.scaler.up unless Delayed::Job.scaler.jobs.empty?
16
11
 
17
- [status, headers, response_body ? [response_body] : response]
12
+ [status, headers, response]
18
13
  end
19
14
 
20
15
  # fix issue if response's body is a Proc
@@ -30,8 +25,4 @@ class WorklessChecker
30
25
  def file?(headers)
31
26
  headers['Content-Transfer-Encoding'] == 'binary'
32
27
  end
33
-
34
- def html_request?(headers, response)
35
- headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response.body.include?('<html')
36
- end
37
28
  end
@@ -2,7 +2,6 @@ module Delayed
2
2
  module Workless
3
3
  module Scaler
4
4
  autoload :Heroku, 'workless/scalers/heroku'
5
- autoload :HerokuCedar, 'workless/scalers/heroku_cedar'
6
5
  autoload :Local, 'workless/scalers/local'
7
6
  autoload :Null, 'workless/scalers/null'
8
7
 
@@ -10,9 +9,15 @@ module Delayed
10
9
  base.send :extend, ClassMethods
11
10
  if base.to_s =~ /ActiveRecord/
12
11
  base.class_eval do
13
- after_commit 'self.class.scaler.down'.to_sym, on: :update, if: proc { |r| !r.failed_at.nil? }
14
- after_commit 'self.class.scaler.down'.to_sym, on: :destroy, if: proc { |r| r.destroyed? || !r.failed_at.nil? }
15
- after_commit 'self.class.scaler.up'.to_sym, on: :create
12
+ after_commit(on: :update, if: proc { |r| !r.failed_at.nil? }) do
13
+ self.class.scaler.down
14
+ end
15
+ after_commit(on: :destroy, if: proc { |r| r.destroyed? || !r.failed_at.nil? }) do
16
+ self.class.scaler.down
17
+ end
18
+ after_commit(on: :create) do
19
+ self.class.scaler.up
20
+ end
16
21
  end
17
22
  elsif base.to_s =~ /Sequel/
18
23
  base.send(:define_method, 'after_destroy') do
@@ -39,7 +44,7 @@ module Delayed
39
44
  module ClassMethods
40
45
  def scaler
41
46
  @scaler ||= if ENV.include?('HEROKU_API_KEY')
42
- Scaler::HerokuCedar
47
+ Scaler::Heroku
43
48
  else
44
49
  Scaler::Local
45
50
  end
@@ -5,17 +5,13 @@ module Delayed
5
5
  module Scaler
6
6
  class Base
7
7
  def self.jobs
8
- if Rails.version >= '3.0.0'
9
- Delayed::Job.where(failed_at: nil)
10
- else
11
- Delayed::Job.all(conditions: { failed_at: nil })
12
- end
8
+ Delayed::Job.where(failed_at: nil)
13
9
  end
14
10
  end
15
11
 
16
12
  module HerokuClient
17
13
  def client
18
- @client ||= ::Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
14
+ @client ||= ::PlatformAPI.connect(ENV['HEROKU_API_KEY'])
19
15
  end
20
16
  end
21
17
  end
@@ -1,4 +1,4 @@
1
- require 'heroku-api'
1
+ require 'platform-api'
2
2
 
3
3
  module Delayed
4
4
  module Workless
@@ -7,15 +7,45 @@ module Delayed
7
7
  extend Delayed::Workless::Scaler::HerokuClient
8
8
 
9
9
  def self.up
10
- client.put_workers(ENV['APP_NAME'], 1) if workers == 0
10
+ return unless workers_needed > min_workers && workers < workers_needed
11
+ updates = { "quantity": workers_needed }
12
+ client.formation.update(ENV['APP_NAME'], 'worker', updates)
11
13
  end
12
14
 
13
15
  def self.down
14
- client.put_workers(ENV['APP_NAME'], 0) unless jobs.count > 0 || workers == 0
16
+ return if workers == workers_needed
17
+ updates = { "quantity": workers_needed }
18
+ client.formation.update(ENV['APP_NAME'], 'worker', updates)
15
19
  end
16
20
 
17
21
  def self.workers
18
- client.get_ps(ENV['APP_NAME']).body.count { |p| p['process'] =~ /worker\.\d?/ }
22
+ client.formation.info(ENV['APP_NAME'], 'worker')['quantity']
23
+ end
24
+
25
+ # Returns the number of workers needed based on the current number of pending jobs and the settings defined by:
26
+ #
27
+ # ENV['WORKLESS_WORKERS_RATIO']
28
+ # ENV['WORKLESS_MAX_WORKERS']
29
+ # ENV['WORKLESS_MIN_WORKERS']
30
+ #
31
+ def self.workers_needed
32
+ [[(jobs.count.to_f / workers_ratio).ceil, max_workers].min, min_workers].max
33
+ end
34
+
35
+ def self.workers_ratio
36
+ if ENV['WORKLESS_WORKERS_RATIO'].present? && (ENV['WORKLESS_WORKERS_RATIO'].to_i != 0)
37
+ ENV['WORKLESS_WORKERS_RATIO'].to_i
38
+ else
39
+ 100
40
+ end
41
+ end
42
+
43
+ def self.max_workers
44
+ ENV['WORKLESS_MAX_WORKERS'].present? ? ENV['WORKLESS_MAX_WORKERS'].to_i : 1
45
+ end
46
+
47
+ def self.min_workers
48
+ ENV['WORKLESS_MIN_WORKERS'].present? ? ENV['WORKLESS_MIN_WORKERS'].to_i : 0
19
49
  end
20
50
  end
21
51
  end
@@ -1,4 +1,3 @@
1
- require 'heroku-api'
2
1
  require File.dirname(__FILE__) + '/workless/scalers/base'
3
2
  require File.dirname(__FILE__) + '/workless/scaler'
4
3
  require File.dirname(__FILE__) + '/workless/middleware/workless_checker' if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workless_revived
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.5
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - davidakachaos
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: heroku-api
28
+ name: platform-api
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -94,7 +94,6 @@ files:
94
94
  - lib/workless/scaler.rb
95
95
  - lib/workless/scalers/base.rb
96
96
  - lib/workless/scalers/heroku.rb
97
- - lib/workless/scalers/heroku_cedar.rb
98
97
  - lib/workless/scalers/local.rb
99
98
  - lib/workless/scalers/null.rb
100
99
  - lib/workless_revived.rb
@@ -110,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
109
  requirements:
111
110
  - - ">="
112
111
  - !ruby/object:Gem::Version
113
- version: 2.0.0
112
+ version: 2.2.5
114
113
  required_rubygems_version: !ruby/object:Gem::Requirement
115
114
  requirements:
116
115
  - - ">="
@@ -1,49 +0,0 @@
1
- require 'heroku-api'
2
-
3
- module Delayed
4
- module Workless
5
- module Scaler
6
- class HerokuCedar < Base
7
- extend Delayed::Workless::Scaler::HerokuClient
8
-
9
- def self.up
10
- client.post_ps_scale(ENV['APP_NAME'], 'worker', workers_needed) if workers_needed > min_workers && workers < workers_needed
11
- end
12
-
13
- def self.down
14
- client.post_ps_scale(ENV['APP_NAME'], 'worker', workers_needed) unless workers == workers_needed
15
- end
16
-
17
- def self.workers
18
- client.get_ps(ENV['APP_NAME']).body.count { |p| p['process'] =~ /worker\.\d?/ }
19
- end
20
-
21
- # Returns the number of workers needed based on the current number of pending jobs and the settings defined by:
22
- #
23
- # ENV['WORKLESS_WORKERS_RATIO']
24
- # ENV['WORKLESS_MAX_WORKERS']
25
- # ENV['WORKLESS_MIN_WORKERS']
26
- #
27
- def self.workers_needed
28
- [[(jobs.count.to_f / workers_ratio).ceil, max_workers].min, min_workers].max
29
- end
30
-
31
- def self.workers_ratio
32
- if ENV['WORKLESS_WORKERS_RATIO'].present? && (ENV['WORKLESS_WORKERS_RATIO'].to_i != 0)
33
- ENV['WORKLESS_WORKERS_RATIO'].to_i
34
- else
35
- 100
36
- end
37
- end
38
-
39
- def self.max_workers
40
- ENV['WORKLESS_MAX_WORKERS'].present? ? ENV['WORKLESS_MAX_WORKERS'].to_i : 1
41
- end
42
-
43
- def self.min_workers
44
- ENV['WORKLESS_MIN_WORKERS'].present? ? ENV['WORKLESS_MIN_WORKERS'].to_i : 0
45
- end
46
- end
47
- end
48
- end
49
- end