delayed-threaded 0.16.0 → 0.17.1

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: 32e63adc06acd90d954baed1e872c113239941d9
4
- data.tar.gz: bb4d43d6376e85f84efe0945c14149143ca59140
3
+ metadata.gz: ce6a3948e6a0ceea302b0645c284c392dae15b19
4
+ data.tar.gz: 6fe497718a6c6cc06b010e13e8a1c477ded4b228
5
5
  SHA512:
6
- metadata.gz: 46418908010e3463ca50d1c6a17d069670869b40cb220f2c3729b6924b89b99006ddf07a1e0259f7b2429b5d01e690be9bf8bd9692494c6c39cb3848f9a92403
7
- data.tar.gz: f50279911f6e6024570079a3f6715a72136d9853e53949ebad1f62591422656881b0eee64fd1db29ab505d2bdfb0d68d46c55f77e9393b3ddeac4bfc9a769db2
6
+ metadata.gz: 1dd596551baf39bb987209f5b136c5d34a55a6deec73206c441175a2d8034e9c5b44a80bb44b1d1d3649a808223596a3e460c0f4c3b0a64e92026223aaa5ae8b
7
+ data.tar.gz: 18dc0ba741a33056f789f03b79c7b972647ae4945bbf00c94e671ddcece7196aa56f1ade89b3c0341b24a8b425b50fbb55b66b26c72ac415c958750e114a5ed6
data/.travis.yml CHANGED
@@ -9,7 +9,7 @@ before_install: gem install bundler -v 1.16.1
9
9
  env:
10
10
  - delayed_job="~>4.1.5" activerecord="~>4.2"
11
11
  - delayed_job="~>4.0.6" activerecord="~>4.2"
12
- - delayed_job="~>3.0" # activerecord="~>3.2"
12
+ - delayed_job="~>3.0" activerecord="~>3.2.22"
13
13
  - delayed_job="~>4.1" activerecord="~>4.2.6"
14
14
  matrix:
15
15
  allow_failures:
data/Gemfile CHANGED
@@ -23,7 +23,7 @@ end
23
23
 
24
24
  if ENV['activerecord']
25
25
  gem 'activerecord', version = ENV['activerecord'], :require => nil
26
- if version =~ /~?\s?4\.[12]/
26
+ if version =~ /~?\s?[34]\.[012]/
27
27
  gem 'activerecord-jdbc-adapter', '~> 1.3.20', :require => nil, :platform => :jruby
28
28
  else
29
29
  gem 'activerecord-jdbc-adapter', :require => nil, :platform => :jruby
data/README.md CHANGED
@@ -20,10 +20,6 @@ and `bundle` or install it yourself as `gem install delayed-threaded`.
20
20
  ```ruby
21
21
  def start_worker
22
22
  options = { :quiet => true }
23
- options[:queues] = (ENV['QUEUES'] || ENV['QUEUE'] || '').split(',')
24
- options[:min_priority] = ENV['MIN_PRIORITY']
25
- options[:max_priority] = ENV['MAX_PRIORITY']
26
- # beyond `rake delayed:work` compatibility :
27
23
  if read_ahead = ENV['READ_AHEAD'] # DEFAULT_READ_AHEAD = 5
28
24
  options[:read_ahead] = read_ahead.to_i
29
25
  end
@@ -31,6 +27,7 @@ def start_worker
31
27
  options[:sleep_delay] = sleep_delay.to_f
32
28
  end
33
29
 
30
+ # some options are set to work per-thread (as a thread-local).
34
31
  worker = Delayed::Threaded::Worker.new(options)
35
32
  worker.start
36
33
  rescue Exception => e
@@ -41,9 +38,23 @@ rescue Exception => e
41
38
  STDERR.puts(msg)
42
39
  end
43
40
 
41
+ # while other options are global and do not make sense to be set per-thread
42
+ Delayed::Worker.queues = (ENV['QUEUES'] || ENV['QUEUE'] || '').split(',')
43
+ Delayed::Worker.min_priority = ENV['MIN_PRIORITY'] if ENV['MIN_PRIORITY']
44
+ Delayed::Worker.max_priority = ENV['MAX_PRIORITY'] if ENV['MAX_PRIORITY']
45
+
44
46
  Thread.new { start_worker }
45
47
  ```
46
48
 
49
+ ### ActiveRecord
50
+
51
+ There's an optional integration with the [ActiveRecord][2] backend, to clear
52
+ the connections after work (as the worker sleeps), setup as a plugin using :
53
+
54
+ ```ruby
55
+ require 'delayed/active_record/release_connection_plugin.rb'
56
+ ````
57
+
47
58
  ## Development
48
59
 
49
60
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -59,3 +70,4 @@ See LICENSE (http://en.wikipedia.org/wiki/MIT_License) for details.
59
70
 
60
71
  [0]: https://github.com/collectiveidea/delayed_job
61
72
  [1]: https://github.com/kares/jruby-rack-worker
73
+ [2]: https://github.com/collectiveidea/delayed_job_active_record
@@ -0,0 +1,4 @@
1
+ require 'delayed/worker'
2
+
3
+ require 'delayed/backend/active_record/release_connection'
4
+ Delayed::Worker.plugins << Delayed::Backend::ActiveRecord::ReleaseConnection
@@ -0,0 +1,30 @@
1
+ module Delayed
2
+ module Backend
3
+ module ActiveRecord
4
+
5
+ # Plugin responsible for releasing ActiveRecord connections.
6
+ # Connection (mapped to the current worker thread) gets cleared
7
+ # as the worker finishes a 'work' iteration and goes to sleep.
8
+ # Expected to be compatibile with AR versions 4.x/5.x.
9
+ #
10
+ # `Delayed::Worker.plugins << Delayed::Backend::ActiveRecord::ReleaseConnection`
11
+ #
12
+ # @note Should be the last one in the plugin list.
13
+ # @note `require 'delayed/active_record/release_connection_plugin.rb'`
14
+ #
15
+ class ReleaseConnection < Delayed::Plugin
16
+
17
+ def self.call(_)
18
+ ::ActiveRecord::Base.clear_active_connections!
19
+ # ~ connection_pool_list.each(&:release_connection)
20
+ end
21
+
22
+ callbacks do |lifecycle|
23
+ lifecycle.after(:loop, &method(:call)) # (worker) work loop
24
+ lifecycle.after(:execute, &method(:call)) # once as (worker) stops
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -24,7 +24,7 @@ module Delayed
24
24
  # (now - last) * 0.001 < opt_pause ? ( time + time / 11.1 ) : time
25
25
  end
26
26
 
27
- def thread_count
27
+ def thread_count # TODO gross jruby-rack-worker heritage
28
28
  $worker_manager ? $worker_manager.thread_count : false
29
29
  end
30
30
 
@@ -1,5 +1,5 @@
1
1
  module Delayed
2
2
  module Threaded
3
- VERSION = '0.16.0'
3
+ VERSION = '0.17.1'
4
4
  end
5
5
  end
@@ -35,18 +35,24 @@ module Delayed::Threaded
35
35
  # @patch make sure concurrent worker threads do not cause multiple initializations
36
36
  Delayed::Worker.extend SyncLifecycle if Delayed.const_defined? :Lifecycle
37
37
 
38
- THREAD_LOCAL_ACCESSORS = [
39
- :min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete
40
- ]
38
+ THREAD_LOCAL_ACCESSORS = [ :sleep_delay, :read_ahead, :exit_on_complete,
39
+ :delay_jobs, :max_attempts, :default_log_level ]
41
40
  private_constant :THREAD_LOCAL_ACCESSORS if respond_to?(:private_constant)
41
+ #
42
42
  # due Delayed::Worker#initialize(options = {}) :
43
43
  #
44
44
  # [:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete].each do |option|
45
45
  # self.class.send("#{option}=", options[option]) if options.key?(option)
46
46
  # end
47
+ #
48
+ # :min_priority, :max_priority, :queues, :max_run_time, :destroy_failed_jobs
49
+ # ... are "always" global (accessed in backend)
50
+
51
+ # NOTE: should we warn when 'global' options are being passed in, so that users prefer (realize)
52
+ # they need to be setting the cattr instead e.g. `Delayed::Worker.queues = [ 'my_queue' ]` ... ?
47
53
 
48
54
  class Config
49
- attr_accessor *THREAD_LOCAL_ACCESSORS
55
+ attr_accessor(*THREAD_LOCAL_ACCESSORS)
50
56
  def key?(name); ! instance_variable_get(:"@#{name}").nil? end
51
57
  end
52
58
 
@@ -62,19 +68,25 @@ module Delayed::Threaded
62
68
  superclass.#{name}
63
69
  end
64
70
  end
71
+ # and re-def instance accessors setup by cattr_accessor (from superclass) :
72
+ def #{name}=(val); self.class.#{name} = val end
65
73
  EOS
74
+ unless [:max_attempts, :max_run_time].include?(name) # DJ::Worker#max_run_time(job)
75
+ # re-def instance accessors setup by cattr_accessor (from superclass)
76
+ class_eval("def #{name}; self.class.#{name} end", __FILE__, __LINE__)
77
+ end
66
78
  end
67
79
  # e.g. :
68
80
  #
69
- # def self.min_priority=(value)
70
- # (Thread.current[:delayed_jruby_worker_config] ||= Config.new).min_priority = value
81
+ # def self.sleep_delay=(value)
82
+ # (Thread.current[:delayed_threaded_worker_config] ||= Config.new).sleep_delay = value
71
83
  # end
72
84
  #
73
85
  # def self.min_priority
74
- # if (config = Thread.current[:delayed_jruby_worker_config]) && config.key?(:min_priority)
75
- # config.min_priority
86
+ # if (config = Thread.current[:delayed_threaded_worker_config]) && config.key?(:sleep_delay)
87
+ # config.sleep_delay
76
88
  # else
77
- # Worker.min_priority
89
+ # superclass.sleep_delay # Delayed::Worker.sleep_delay
78
90
  # end
79
91
  # end
80
92
 
@@ -92,14 +104,26 @@ module Delayed::Threaded
92
104
 
93
105
  def to_s; name; end
94
106
 
95
- def thread_id
96
- # NOTE: JRuby might set a bit long name for Thread.new { ... } code e.g.
97
- # RubyThread-1: /home/[...]/src/test/ruby/delayed/jruby_worker_test.rb:163
98
- if name = java.lang.Thread.currentThread.getName
99
- if name.size > 100 && match = name.match(/(.*?)\:\s.*?[\/\\]+/)
100
- match[1]
101
- else
102
- name
107
+ if defined? Thread.current.name
108
+ def thread_id
109
+ if ( name = Thread.current.name || '' ).empty?
110
+ name = java.lang.Thread.currentThread.getName
111
+ if name.size > 100 && match = name.match(/(.*?)\:\s.*?[\/\\]+/)
112
+ name = match[1]
113
+ end
114
+ end
115
+ name
116
+ end
117
+ else
118
+ def thread_id
119
+ # NOTE: JRuby might set a bit long name for Thread.new { ... } code e.g.
120
+ # RubyThread-1: /home/[...]/src/test/ruby/delayed/jruby_worker_test.rb:163
121
+ if name = java.lang.Thread.currentThread.getName
122
+ if name.size > 100 && match = name.match(/(.*?)\:\s.*?[\/\\]+/)
123
+ match[1]
124
+ else
125
+ name
126
+ end
103
127
  end
104
128
  end
105
129
  end
@@ -1,5 +1,3 @@
1
1
  require 'delayed/threaded/version'
2
2
 
3
3
  require 'delayed/threaded/worker'
4
-
5
- require 'delayed/threaded/compat'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed-threaded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Bucek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-02 00:00:00.000000000 Z
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -116,8 +116,9 @@ files:
116
116
  - bin/console
117
117
  - bin/setup
118
118
  - delayed-threaded.gemspec
119
+ - lib/delayed/active_record/release_connection_plugin.rb
120
+ - lib/delayed/backend/active_record/release_connection.rb
119
121
  - lib/delayed/threaded.rb
120
- - lib/delayed/threaded/compat.rb
121
122
  - lib/delayed/threaded/sleep_calculator.rb
122
123
  - lib/delayed/threaded/sync_lifecycle.rb
123
124
  - lib/delayed/threaded/version.rb
@@ -1,4 +0,0 @@
1
- module Delayed
2
- # @deprecated
3
- JRubyWorker = Threaded::Worker
4
- end