fleiss 0.2.1 → 0.3.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
  SHA256:
3
- metadata.gz: 94c1519f479dcb78d6e96d73cdf56b750371d64d2129e49ff20a82535c69ece1
4
- data.tar.gz: 3a3d640790d631ed73b2594801ffe3769105d3ecded703e2213d6474622dc5fb
3
+ metadata.gz: 5f5d2237b3a3bb01c8998c273a9fbb4f65cbf75b737df03187db2315c1efa93d
4
+ data.tar.gz: c80f8f0423dea4b516e824ccb669ec2e34abb6c2365adbe5cea937101526c429
5
5
  SHA512:
6
- metadata.gz: 06faa820062f68549d50122e79dc8848b62e29097321dd6ea891dfceaca97d3501f5559e1ca6cb2bc2164960ed9b219a3ea1703916f4e27f84fa54ca7378e11a
7
- data.tar.gz: a01c87c032fd5f6e3b3d36538022ec06042a94e41c4a3e043347281d1fa1edcf0c2781f6545ede2282c1a0d82ad02967a4b1f229966fdf8196a1ce4804317b4d
6
+ metadata.gz: e497a57b2aec4333ce4cde801a7bcf631546158c1b4c1d2b2a663ca997066438f01bb14b5103637cbb661c168e403c4b21014dbe92ee2fb927806a377b788f21
7
+ data.tar.gz: 7d18e086ebe3ca8f6d8c0c025ca302abdfa487487548c38664d18904cd5c3a8d7e1997132054af334c5cbb1e8863b01fcdf176546f7d9d300e30443a2958bbe0
@@ -1,5 +1,8 @@
1
1
  inherit_from:
2
- - https://bitbucket.org/bsm/misc/raw/master/rubocop/default.yml
2
+ - https://gitlab.com/bsm/misc/raw/master/rubocop/default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: "2.4"
3
6
 
4
7
  Security/Open:
5
8
  Exclude:
@@ -1,8 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.6
3
4
  - 2.5
4
5
  - 2.4
5
6
  before_install:
7
+ - gem install bundler
6
8
  - mysql -e 'CREATE DATABASE fleiss_test;'
7
9
  - psql -c 'CREATE DATABASE fleiss_test;' -U postgres
8
10
  services:
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fleiss (0.2.1)
4
+ fleiss (0.3.0)
5
5
  activejob (>= 5.0)
6
6
  activerecord (>= 5.0)
7
7
  concurrent-ruby
@@ -9,36 +9,35 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activejob (5.2.2)
13
- activesupport (= 5.2.2)
12
+ activejob (5.2.3)
13
+ activesupport (= 5.2.3)
14
14
  globalid (>= 0.3.6)
15
- activemodel (5.2.2)
16
- activesupport (= 5.2.2)
17
- activerecord (5.2.2)
18
- activemodel (= 5.2.2)
19
- activesupport (= 5.2.2)
15
+ activemodel (5.2.3)
16
+ activesupport (= 5.2.3)
17
+ activerecord (5.2.3)
18
+ activemodel (= 5.2.3)
19
+ activesupport (= 5.2.3)
20
20
  arel (>= 9.0)
21
- activesupport (5.2.2)
21
+ activesupport (5.2.3)
22
22
  concurrent-ruby (~> 1.0, >= 1.0.2)
23
23
  i18n (>= 0.7, < 2)
24
24
  minitest (~> 5.1)
25
25
  tzinfo (~> 1.1)
26
26
  arel (9.0.0)
27
27
  ast (2.4.0)
28
- concurrent-ruby (1.1.4)
28
+ concurrent-ruby (1.1.5)
29
29
  diff-lcs (1.3)
30
- globalid (0.4.1)
30
+ globalid (0.4.2)
31
31
  activesupport (>= 4.2.0)
32
- i18n (1.4.0)
32
+ i18n (1.6.0)
33
33
  concurrent-ruby (~> 1.0)
34
- jaro_winkler (1.5.1)
34
+ jaro_winkler (1.5.2)
35
35
  minitest (5.11.3)
36
36
  mysql2 (0.5.2)
37
- parallel (1.12.1)
38
- parser (2.5.3.0)
37
+ parallel (1.17.0)
38
+ parser (2.6.3.0)
39
39
  ast (~> 2.4.0)
40
- pg (1.1.3)
41
- powerpack (0.1.2)
40
+ pg (1.1.4)
42
41
  rainbow (3.0.0)
43
42
  rake (12.3.2)
44
43
  rspec (3.8.0)
@@ -47,27 +46,26 @@ GEM
47
46
  rspec-mocks (~> 3.8.0)
48
47
  rspec-core (3.8.0)
49
48
  rspec-support (~> 3.8.0)
50
- rspec-expectations (3.8.2)
49
+ rspec-expectations (3.8.3)
51
50
  diff-lcs (>= 1.2.0, < 2.0)
52
51
  rspec-support (~> 3.8.0)
53
52
  rspec-mocks (3.8.0)
54
53
  diff-lcs (>= 1.2.0, < 2.0)
55
54
  rspec-support (~> 3.8.0)
56
55
  rspec-support (3.8.0)
57
- rubocop (0.62.0)
56
+ rubocop (0.71.0)
58
57
  jaro_winkler (~> 1.5.1)
59
58
  parallel (~> 1.10)
60
- parser (>= 2.5, != 2.5.1.1)
61
- powerpack (~> 0.1)
59
+ parser (>= 2.6)
62
60
  rainbow (>= 2.2.2, < 4.0)
63
61
  ruby-progressbar (~> 1.7)
64
- unicode-display_width (~> 1.4.0)
65
- ruby-progressbar (1.10.0)
62
+ unicode-display_width (>= 1.4.0, < 1.7)
63
+ ruby-progressbar (1.10.1)
66
64
  sqlite3 (1.3.13)
67
65
  thread_safe (0.3.6)
68
66
  tzinfo (1.2.5)
69
67
  thread_safe (~> 0.1)
70
- unicode-display_width (1.4.1)
68
+ unicode-display_width (1.6.0)
71
69
 
72
70
  PLATFORMS
73
71
  ruby
@@ -80,7 +78,7 @@ DEPENDENCIES
80
78
  rake
81
79
  rspec
82
80
  rubocop
83
- sqlite3
81
+ sqlite3 (~> 1.3.6)
84
82
 
85
83
  BUNDLED WITH
86
- 1.17.2
84
+ 2.0.1
data/bin/fleiss CHANGED
@@ -13,12 +13,12 @@ begin
13
13
  cli.parse!
14
14
  cli.run!
15
15
  rescue ArgumentError => e
16
- STDERR.puts " ! #{e.message}\n"
16
+ warn " ! #{e.message}\n"
17
17
  STDERR.puts
18
- STDERR.puts cli.parser
18
+ warn cli.parser
19
19
  exit 1
20
20
  rescue StandardError => e
21
- STDERR.puts e.message
22
- STDERR.puts e.backtrace.join("\n")
21
+ warn e.message
22
+ warn e.backtrace.join("\n")
23
23
  exit 1
24
24
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'fleiss'
3
- s.version = '0.2.1'
3
+ s.version = '0.3.0'
4
4
  s.authors = ['Black Square Media Ltd']
5
5
  s.email = ['info@blacksquaremedia.com']
6
6
  s.summary = %(Minimialist background jobs backed by ActiveJob and ActiveRecord.)
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^spec/}) }
13
13
  s.test_files = `git ls-files -z -- spec/*`.split("\x0")
14
14
  s.require_paths = ['lib']
15
- s.required_ruby_version = '>= 2.2'
15
+ s.required_ruby_version = '>= 2.4'
16
16
 
17
17
  s.add_dependency 'activejob', '>= 5.0'
18
18
  s.add_dependency 'activerecord', '>= 5.0'
@@ -24,5 +24,5 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'rake'
25
25
  s.add_development_dependency 'rspec'
26
26
  s.add_development_dependency 'rubocop'
27
- s.add_development_dependency 'sqlite3'
27
+ s.add_development_dependency 'sqlite3', '~> 1.3.6'
28
28
  end
@@ -15,6 +15,10 @@ module Fleiss
15
15
  end
16
16
 
17
17
  module ClassMethods
18
+ def wrap_perform(&block)
19
+ connection_pool.with_connection(&block)
20
+ end
21
+
18
22
  # @return [ActiveRecord::Relation] pending scope
19
23
  def pending(now=Time.zone.now)
20
24
  not_finished
@@ -0,0 +1,22 @@
1
+ require 'fleiss'
2
+ require 'concurrent/executor/simple_executor_service'
3
+
4
+ class Fleiss::Executor < Concurrent::SimpleExecutorService
5
+ attr_reader :max_size
6
+
7
+ def post(&block)
8
+ super unless capacity.zero?
9
+ end
10
+
11
+ def capacity
12
+ val = @max_size - @count.value
13
+ val.positive? ? val : 0
14
+ end
15
+
16
+ private
17
+
18
+ def ns_initialize(opts={})
19
+ super()
20
+ @max_size = opts.fetch(:max_size, 1).to_i
21
+ end
22
+ end
@@ -1,6 +1,5 @@
1
1
  require 'fleiss'
2
- require 'concurrent/executor/fixed_thread_pool'
3
- require 'concurrent/atomic/atomic_fixnum'
2
+ require 'fleiss/executor'
4
3
  require 'securerandom'
5
4
 
6
5
  class Fleiss::Worker
@@ -20,13 +19,13 @@ class Fleiss::Worker
20
19
  def initialize(queues: [Fleiss::DEFAULT_QUEUE], concurrency: 10, wait_time: 1)
21
20
  @uuid = SecureRandom.uuid
22
21
  @queues = Array(queues)
23
- @pool = Concurrent::FixedThreadPool.new(concurrency, fallback_policy: :discard)
22
+ @pool = Fleiss::Executor.new(max_size: concurrency)
24
23
  @wait_time = wait_time
25
24
  end
26
25
 
27
26
  # Run starts the worker
28
27
  def run
29
- log(:info) { "Worker #{uuid} starting - queues: #{queues.inspect}, concurrency: #{@pool.max_length}" }
28
+ log(:info) { "Worker #{uuid} starting - queues: #{queues.inspect}, concurrency: #{@pool.max_size}" }
30
29
  loop do
31
30
  run_cycle
32
31
  sleep @wait_time
@@ -50,19 +49,21 @@ class Fleiss::Worker
50
49
  end
51
50
 
52
51
  def run_cycle
53
- return if @pool.shuttingdown?
52
+ return unless @pool.running?
54
53
 
55
- capacity = @pool.max_length - @pool.scheduled_task_count + @pool.completed_task_count
56
- return unless capacity.positive?
54
+ limit = @pool.capacity
55
+ return unless limit.positive?
57
56
 
58
57
  batch = Fleiss.backend
59
58
  .in_queue(queues)
60
59
  .pending
61
- .limit(capacity)
60
+ .limit(limit)
62
61
  .to_a
63
62
 
64
63
  batch.each do |job|
65
- @pool.post { perform(job) }
64
+ @pool.post do
65
+ Fleiss.backend.wrap_perform { perform(job) }
66
+ end
66
67
  end
67
68
  rescue StandardError => e
68
69
  handle_exception e, 'running cycle'
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'fleiss/executor'
3
+
4
+ RSpec.describe Fleiss::Executor do
5
+ subject { described_class.new max_size: 2 }
6
+ after { subject.kill }
7
+
8
+ it 'should check capacity' do
9
+ expect(described_class.new.capacity).to eq(1)
10
+
11
+ expect(subject.capacity).to eq(2)
12
+ subject.post { sleep(1) }
13
+ expect(subject.capacity).to eq(1)
14
+ subject.post { sleep(1) }
15
+ expect(subject.capacity).to eq(0)
16
+ end
17
+
18
+ it 'should discard execution when capacity is reached' do
19
+ n = Concurrent::AtomicFixnum.new(0)
20
+ 10.times do
21
+ 10.times { subject.post { n.increment } }
22
+ sleep(0.001)
23
+ end
24
+ subject.shutdown
25
+ subject.wait_for_termination(1)
26
+ expect(n.value).to be_within(10).of(20)
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fleiss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Black Square Media Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-02 00:00:00.000000000 Z
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -140,16 +140,16 @@ dependencies:
140
140
  name: sqlite3
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ">="
143
+ - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '0'
145
+ version: 1.3.6
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - ">="
150
+ - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '0'
152
+ version: 1.3.6
153
153
  description: Run background jobs with your favourite stack.
154
154
  email:
155
155
  - info@blacksquaremedia.com
@@ -176,8 +176,10 @@ files:
176
176
  - lib/fleiss/backend/active_record/concern.rb
177
177
  - lib/fleiss/backend/active_record/migration.rb
178
178
  - lib/fleiss/cli.rb
179
+ - lib/fleiss/executor.rb
179
180
  - lib/fleiss/worker.rb
180
181
  - spec/fleiss/backend/active_record_spec.rb
182
+ - spec/fleiss/executor_spec.rb
181
183
  - spec/fleiss/worker_spec.rb
182
184
  - spec/fleiss_spec.rb
183
185
  - spec/spec_helper.rb
@@ -193,20 +195,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
193
195
  requirements:
194
196
  - - ">="
195
197
  - !ruby/object:Gem::Version
196
- version: '2.2'
198
+ version: '2.4'
197
199
  required_rubygems_version: !ruby/object:Gem::Requirement
198
200
  requirements:
199
201
  - - ">="
200
202
  - !ruby/object:Gem::Version
201
203
  version: '0'
202
204
  requirements: []
203
- rubyforge_project:
204
- rubygems_version: 2.7.7
205
+ rubygems_version: 3.0.3
205
206
  signing_key:
206
207
  specification_version: 4
207
208
  summary: Minimialist background jobs backed by ActiveJob and ActiveRecord.
208
209
  test_files:
209
210
  - spec/fleiss/backend/active_record_spec.rb
211
+ - spec/fleiss/executor_spec.rb
210
212
  - spec/fleiss/worker_spec.rb
211
213
  - spec/fleiss_spec.rb
212
214
  - spec/spec_helper.rb