fleiss 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: '0786905f08fa028fa92f0d422db3d26acc173b57dbf403b5fe1b20dc2bfb8e73'
4
- data.tar.gz: f6f13c6d6c86e456a7779bc06119b2d4e7ed61daa1bbd9438014a0e356daedd0
3
+ metadata.gz: e9eb58758de53588746446aedfbd877e9b84d361de7650185265b9973aeff0b3
4
+ data.tar.gz: d5f1ac77ce8f6dbef855c6a4e0f2eecdad56a7f8a5fc30f5851d95934eb92453
5
5
  SHA512:
6
- metadata.gz: 67ae6d5b939e7c83cfa69bba4d0ecbacabbed12a0c474289a9162a82928a2aea78aaaa6d96bf66700f10e06c548ac10fbe3008b7562be1133ed09bfdc33b3713
7
- data.tar.gz: 609b536863e02bc91eb093b839f64ec518090bd722eaffa163162cccff537d3cd87f5991c0a366abd403a5a3fb701d1a581830d3d820439d0d74d80e30638812
6
+ metadata.gz: c1b97a6658b29170a51a85df91611ff562f319d03f29626bbcf3069858e19cf7cc5aed4cfec7b0508fc90a1899cf325b84fef67e9c270842609acb62cbe0fad7
7
+ data.tar.gz: 28dccc2dced0062b9355f75da37989f2c008bd3b6d3be0d8cac4c8c3ea637eb41f4b94a747e10abd19ef6f85e51d45f01db62c581f1dee0b98377f6affb6cb3f
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  inherit_from:
2
- - https://storage.googleapis.com/bsm-misc/rubocop.yml
2
+ - https://bitbucket.org/bsm/misc/raw/master/rubocop/default.yml
3
3
 
4
4
  Security/Open:
5
5
  Exclude:
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5
4
+ - 2.4
5
+ before_install:
6
+ - mysql -e 'CREATE DATABASE fleiss_test;'
7
+ - psql -c 'CREATE DATABASE fleiss_test;' -U postgres
8
+ services:
9
+ - postgresql
10
+ - mysql
11
+ env:
12
+ - DATABASE_URL=sqlite3:///tmp/fleiss_test.sqlite3
13
+ - DATABASE_URL=mysql2://travis@127.0.0.1/fleiss_test
14
+ - DATABASE_URL=postgresql://127.0.0.1/fleiss_test
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fleiss (0.1.0)
4
+ fleiss (0.1.1)
5
5
  activejob (>= 5.0)
6
6
  activerecord (>= 5.0)
7
7
  concurrent-ruby
@@ -9,16 +9,16 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activejob (5.2.1)
13
- activesupport (= 5.2.1)
12
+ activejob (5.2.2)
13
+ activesupport (= 5.2.2)
14
14
  globalid (>= 0.3.6)
15
- activemodel (5.2.1)
16
- activesupport (= 5.2.1)
17
- activerecord (5.2.1)
18
- activemodel (= 5.2.1)
19
- activesupport (= 5.2.1)
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)
20
20
  arel (>= 9.0)
21
- activesupport (5.2.1)
21
+ activesupport (5.2.2)
22
22
  concurrent-ruby (~> 1.0, >= 1.0.2)
23
23
  i18n (>= 0.7, < 2)
24
24
  minitest (~> 5.1)
@@ -33,9 +33,11 @@ GEM
33
33
  concurrent-ruby (~> 1.0)
34
34
  jaro_winkler (1.5.1)
35
35
  minitest (5.11.3)
36
+ mysql2 (0.5.2)
36
37
  parallel (1.12.1)
37
38
  parser (2.5.3.0)
38
39
  ast (~> 2.4.0)
40
+ pg (1.1.3)
39
41
  powerpack (0.1.2)
40
42
  rainbow (3.0.0)
41
43
  rake (12.3.1)
@@ -52,7 +54,7 @@ GEM
52
54
  diff-lcs (>= 1.2.0, < 2.0)
53
55
  rspec-support (~> 3.8.0)
54
56
  rspec-support (3.8.0)
55
- rubocop (0.60.0)
57
+ rubocop (0.61.1)
56
58
  jaro_winkler (~> 1.5.1)
57
59
  parallel (~> 1.10)
58
60
  parser (>= 2.5, != 2.5.1.1)
@@ -73,6 +75,8 @@ PLATFORMS
73
75
  DEPENDENCIES
74
76
  bundler
75
77
  fleiss!
78
+ mysql2
79
+ pg
76
80
  rake
77
81
  rspec
78
82
  rubocop
data/fleiss.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'fleiss'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.1'
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.)
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency 'concurrent-ruby'
20
20
 
21
21
  s.add_development_dependency 'bundler'
22
+ s.add_development_dependency 'mysql2'
23
+ s.add_development_dependency 'pg'
22
24
  s.add_development_dependency 'rake'
23
25
  s.add_development_dependency 'rspec'
24
26
  s.add_development_dependency 'rubocop'
@@ -68,19 +68,23 @@ module Fleiss
68
68
  # @param [String] owner
69
69
  # @return [Boolean] true if job was started.
70
70
  def start(owner, now: Time.zone.now)
71
- self.class.pending(now)
72
- .where(id: id)
73
- .update_all(started_at: now, owner: owner) == 1
71
+ with_lock do
72
+ self.class.pending(now)
73
+ .where(id: id)
74
+ .update_all(started_at: now, owner: owner) == 1
75
+ end
74
76
  end
75
77
 
76
78
  # Marks a job as finished.
77
79
  # @param [String] owner
78
80
  # @return [Boolean] true if successful.
79
81
  def finish(owner, now: Time.zone.now)
80
- self.class
81
- .in_progress(owner)
82
- .where(id: id)
83
- .update_all(finished_at: now) == 1
82
+ with_lock do
83
+ self.class
84
+ .in_progress(owner)
85
+ .where(id: id)
86
+ .update_all(finished_at: now) == 1
87
+ end
84
88
  end
85
89
  end
86
90
  end
data/lib/fleiss/worker.rb CHANGED
@@ -57,31 +57,24 @@ class Fleiss::Worker
57
57
  private
58
58
 
59
59
  def run_cycle
60
- jobs = next_batch
61
- while !@stopped && capacity.positive? && !jobs.empty?
62
- job = jobs.shift
60
+ capacity = @pool.max_length - @pool.scheduled_task_count + @pool.completed_task_count
61
+ return unless capacity.positive?
62
+
63
+ batch = Fleiss.backend
64
+ .in_queue(queues)
65
+ .pending
66
+ .limit(capacity)
67
+ .to_a
68
+
69
+ batch.each do |job|
70
+ break if @stopped
71
+
63
72
  @pool.post { perform(job) }
64
73
  end
65
74
  rescue StandardError => e
66
75
  handle_exception e, 'running cycle'
67
76
  end
68
77
 
69
- def capacity
70
- num = @pool.max_length - @pool.scheduled_task_count + @pool.completed_task_count
71
- num.positive? ? num : 0
72
- end
73
-
74
- def next_batch
75
- slots = capacity
76
- return [] if slots.zero?
77
-
78
- Fleiss.backend
79
- .in_queue(queues)
80
- .pending
81
- .limit(slots)
82
- .to_a
83
- end
84
-
85
78
  def perform(job)
86
79
  return unless job.start(uuid)
87
80
 
@@ -95,6 +95,18 @@ RSpec.describe Fleiss::Backend::ActiveRecord do
95
95
  expect(rec.started_at).to be_within(2.seconds).of(Time.zone.now)
96
96
  end
97
97
 
98
+ it 'should lock atomically' do
99
+ 24.times do
100
+ TestJob.perform_later
101
+ end
102
+ counts = (1..4).map do |n|
103
+ Thread.new do
104
+ described_class.pending.to_a.count {|j| j.start "owner-#{n}" }
105
+ end
106
+ end.map(&:value)
107
+ expect(counts.sum).to eq(24)
108
+ end
109
+
98
110
  it 'should finish' do
99
111
  job = TestJob.perform_later
100
112
  rec = retrieve(job)
data/spec/spec_helper.rb CHANGED
@@ -12,12 +12,11 @@ ActiveJob::Base.logger = Logger.new(nil)
12
12
 
13
13
  Time.zone_default = Time.find_zone!('UTC')
14
14
 
15
- ActiveRecord::Base.configurations['test'] = {
16
- 'adapter' => 'sqlite3',
17
- 'database' => Tempfile.new(['fleiss-test', '.sqlite3']).path,
18
- 'pool' => 20,
19
- }
15
+ database_url = ENV['DATABASE_URL'] || "sqlite3://#{Tempfile.new(['fleiss-test', '.sqlite3']).path}"
16
+ ActiveRecord::Base.configurations['test'] = { 'url' => database_url, 'pool' => 20 }
17
+
20
18
  ActiveRecord::Base.establish_connection :test
19
+ ActiveRecord::Base.connection.drop_table('fleiss_jobs', if_exists: true)
21
20
  ActiveRecord::Migration.suppress_messages do
22
21
  Fleiss::Backend::ActiveRecord::Migration.migrate(:up)
23
22
  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.1.0
4
+ version: 0.1.1
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: 2018-11-15 00:00:00.000000000 Z
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: rake
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +161,7 @@ files:
133
161
  - ".editorconfig"
134
162
  - ".gitignore"
135
163
  - ".rubocop.yml"
164
+ - ".travis.yml"
136
165
  - Gemfile
137
166
  - Gemfile.lock
138
167
  - LICENSE