plok 0.2.5 → 0.2.9

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: 4e4560c0f41784811539873f92cbda25d73197ac1403d3ee1c1b3f046b0e0a0d
4
- data.tar.gz: 95a866614905153ac92414e9dd8e12a60e0d6539118fd4dbf5b9427db323bd3f
3
+ metadata.gz: 75df059f748c988f11b00cab9ebc496052a5da7c129dfeb84034cc08b4742abc
4
+ data.tar.gz: cb9fdcb200ea259378af6600ce6f906e1f89b19c41fb5185067c3f2643ba54a9
5
5
  SHA512:
6
- metadata.gz: bc64c91c32ea195fdfab4596acdfbc1611d4790d71121ff083eb8297b1b59ac7b2427de5f02ce95ce3382f57bc679d30e147b70e31bd1fc5a142c06e6aba7528
7
- data.tar.gz: 63bbbb1e6dadc2b8bd667e6172c59110f893050d6978707bb492807872f6aad702c73fb8b5f3f76e448f58f2c4dd24b3eaa0bfd58a4064734e51d324ef4a762c
6
+ metadata.gz: d9f558ae1cce6166ac50f3c229b25a874110e736ee93465708f776f83a2d23bc85c92ddb139b46cf5681f64bcdf4b221fbbc4847e0b9b5fdb77af869a27e3555
7
+ data.tar.gz: b3512d28ccdc1d53c90959376fa603efac9a6f297b99405fd4d0c330fedd0b97b095f3b36b4d77c28dbc07b118661a145bfd69b082c305d91704edef02cfe9dd
@@ -10,12 +10,15 @@ class QueuedTask < ActiveRecord::Base
10
10
  scope :in_past, -> { where(perform_at: nil).or(where('perform_at <= ?', Time.zone.now)) }
11
11
  scope :in_future, -> { where('perform_at > ?', Time.zone.now) }
12
12
 
13
+ DEFAULT_PRIORITY = 0
14
+ HIGH_PRIORITY = 10
15
+
13
16
  def lock!
14
- update_attribute :locked, true
17
+ update_attribute(:locked, true)
15
18
  end
16
19
 
17
20
  def unlock!
18
- update_attribute :locked, false
21
+ update_attribute(:locked, false)
19
22
  end
20
23
 
21
24
  def unlocked?
@@ -26,22 +29,36 @@ class QueuedTask < ActiveRecord::Base
26
29
  klass.to_s.constantize.new(data).execute!
27
30
  end
28
31
 
29
- def self.queue(klass, data)
30
- task = create!(klass: klass.to_s, data: data.except(:perform_at))
32
+ # TODO: Might be good to use named parameters for data and weight here.
33
+ # Might be able to use the data var to store weight like the perform_at key.
34
+ #
35
+ # TODO: Refactor to a separate class.
36
+ def self.queue(klass, data, weight = DEFAULT_PRIORITY)
37
+ task = create!(
38
+ klass: klass.to_s,
39
+ weight: weight,
40
+ attempts: 0,
41
+ data: data.except(:perform_at)
42
+ )
43
+
31
44
  task.update(perform_at: data[:perform_at]) if data[:perform_at].present?
32
45
  task
33
46
  end
34
47
 
35
- def self.queue_unless_already_queued(klass, data)
36
- task = find_by(klass: klass, data: data)
48
+ def self.queue_unless_already_queued(klass, data, weight = DEFAULT_PRIORITY)
49
+ task = find_by(klass: klass, data: data, weight: weight)
37
50
  return task if task.present?
38
- self.queue(klass, data)
51
+ self.queue(klass, data, weight)
39
52
  end
40
53
 
41
54
  def dequeue!
42
55
  destroy
43
56
  end
44
57
 
58
+ def increase_attempts!
59
+ update_column(:attempts, attempts + 1)
60
+ end
61
+
45
62
  def process!
46
63
  lock!
47
64
 
@@ -51,7 +68,10 @@ class QueuedTask < ActiveRecord::Base
51
68
  rescue
52
69
  raise
53
70
  ensure
54
- unlock! unless destroyed?
71
+ if persisted?
72
+ increase_attempts!
73
+ unlock!
74
+ end
55
75
  end
56
76
  end
57
77
 
@@ -0,0 +1,7 @@
1
+ class AddWeightToQueuedTasks < ActiveRecord::Migration[6.1]
2
+ def change
3
+ return if column_exists?(:queued_tasks, :weight)
4
+
5
+ add_column :queued_tasks, :weight, :integer, after: :locked, index: true
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class AddAttemptsToQueuedTasks < ActiveRecord::Migration[6.1]
2
+ def change
3
+ return if column_exists?(:queued_tasks, :attempts)
4
+
5
+ add_column :queued_tasks, :attempts, :integer, after: :weight
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class AddIndexToQueuedTasksLocked < ActiveRecord::Migration[6.1]
2
+ def change
3
+ return if index_exists?(:queued_tasks, :locked)
4
+
5
+ add_index :queued_tasks, :locked
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class AddFileToLogs < ActiveRecord::Migration[6.1]
2
+ def change
3
+ return if column_exists?(:logs, :file)
4
+
5
+ add_column :logs, :file, :string, after: :loggable_id
6
+ end
7
+ end
data/lib/plok/engine.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # Can't use Plok::Engine.root yet at this point.
2
2
  engine_root = File.expand_path('../..', __dir__)
3
- Dir.glob("#{engine_root}/app/models/**/*.rb").each { |file| require file }
3
+ Dir.glob("#{engine_root}/app/models/**/*.rb").sort.each { |file| require file }
4
4
 
5
5
  module Plok
6
6
  class Engine < ::Rails::Engine
@@ -40,7 +40,7 @@ module Plok
40
40
  # You cannot call it in the engine itself, because RSpec won't have the same
41
41
  # context available when tests are ran.
42
42
  def load_spec_supports
43
- Dir.glob("#{root}/spec/{factories,support}/**/*.rb").each { |f| require f }
43
+ Dir.glob("#{root}/spec/{factories,support}/**/*.rb").sort.each { |f| require f }
44
44
  end
45
45
  end
46
46
  end
data/lib/plok/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Plok
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.9'
3
3
  end
@@ -1,5 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :queued_task do
3
3
  klass { 'SomeClass' }
4
+ attempts { 0 }
4
5
  end
5
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plok
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davy Hellemans
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-10-15 00:00:00.000000000 Z
12
+ date: 2021-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -29,16 +29,16 @@ dependencies:
29
29
  name: mysql2
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
34
+ version: '0.5'
35
+ type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '0.5'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec-rails
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -86,6 +86,10 @@ files:
86
86
  - config/routes.rb
87
87
  - db/migrate/20211008130809_create_plok_logs.rb
88
88
  - db/migrate/20211015141837_create_plok_queued_tasks.rb
89
+ - db/migrate/20211119095633_add_weight_to_queued_tasks.rb
90
+ - db/migrate/20211119103846_add_attempts_to_queued_tasks.rb
91
+ - db/migrate/20211119123900_add_index_to_queued_tasks_locked.rb
92
+ - db/migrate/20211203103118_add_file_to_logs.rb
89
93
  - lib/plok.rb
90
94
  - lib/plok/engine.rb
91
95
  - lib/plok/version.rb