plok 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f3772752bb79fb0e5c754451f428a38ffbc921f994c77baace8d553dc3c8534
4
- data.tar.gz: 535cc01459273ea5486507ac0ac2c226462040985dc2aa4874170cf770ac568b
3
+ metadata.gz: e19b5d0848888824ff2eb0d4ec7a667dd745a10641abc54e82182296eaf0f02f
4
+ data.tar.gz: ec066ca66b5ea4cc671d540343d824794045e2b1741307ea1eb19897d35c9f9a
5
5
  SHA512:
6
- metadata.gz: a7242f280f4bc453387b191685b8a1b8857e4371379e5e4f9520cd47bd743c083344e9d9f50911657a0f129db7ce9d0ae793eeca56304bb3d6e8d2aba60432c7
7
- data.tar.gz: eacde236f96f7a4109dcda5cf3f1647813ab82a11e7f3ba2e65e541e2106485d4964f854f9f7cdf4cc2371dede4eeb6e84374a3dcb6353421c06f0b7cab26ba6
6
+ metadata.gz: 2d59516ea8336cd4cf8ee48d94381862129ca87317d9db55c0031c6c560f4a857c83ca99e0f248f08bf49885d3fcb99db3ad6489a3261bfa13b827da6d45aa53
7
+ data.tar.gz: 82d00ed77cb76bcdf41df081c0e98f5a68ca7ccd78388298ad89a41e95ab8433763c5f084d97f27cbd2e2aa66a8815fee4528493cc586a5b405552d6fcf248d4
@@ -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
data/lib/plok/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Plok
2
- VERSION = '0.2.7'
2
+ VERSION = '0.2.8'
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.7
4
+ version: 0.2.8
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-22 00:00:00.000000000 Z
12
+ date: 2021-11-19 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,9 @@ 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
89
92
  - lib/plok.rb
90
93
  - lib/plok/engine.rb
91
94
  - lib/plok/version.rb