plok 0.2.4 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/queued_task.rb +84 -0
- data/db/migrate/20211015141837_create_plok_queued_tasks.rb +14 -0
- data/db/migrate/20211119095633_add_weight_to_queued_tasks.rb +7 -0
- data/db/migrate/20211119103846_add_attempts_to_queued_tasks.rb +7 -0
- data/db/migrate/20211119123900_add_index_to_queued_tasks_locked.rb +7 -0
- data/lib/plok/engine.rb +2 -2
- data/lib/plok/version.rb +1 -1
- data/spec/factories/queued_tasks.rb +6 -0
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e19b5d0848888824ff2eb0d4ec7a667dd745a10641abc54e82182296eaf0f02f
|
4
|
+
data.tar.gz: ec066ca66b5ea4cc671d540343d824794045e2b1741307ea1eb19897d35c9f9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d59516ea8336cd4cf8ee48d94381862129ca87317d9db55c0031c6c560f4a857c83ca99e0f248f08bf49885d3fcb99db3ad6489a3261bfa13b827da6d45aa53
|
7
|
+
data.tar.gz: 82d00ed77cb76bcdf41df081c0e98f5a68ca7ccd78388298ad89a41e95ab8433763c5f084d97f27cbd2e2aa66a8815fee4528493cc586a5b405552d6fcf248d4
|
@@ -0,0 +1,84 @@
|
|
1
|
+
class QueuedTask < ActiveRecord::Base
|
2
|
+
include Plok::Loggable
|
3
|
+
|
4
|
+
serialize :data, Hash
|
5
|
+
|
6
|
+
validates :klass, presence: true
|
7
|
+
|
8
|
+
scope :locked, -> { where(locked: true) }
|
9
|
+
scope :unlocked, -> { where('locked = 0 OR locked IS NULL') }
|
10
|
+
scope :in_past, -> { where(perform_at: nil).or(where('perform_at <= ?', Time.zone.now)) }
|
11
|
+
scope :in_future, -> { where('perform_at > ?', Time.zone.now) }
|
12
|
+
|
13
|
+
DEFAULT_PRIORITY = 0
|
14
|
+
HIGH_PRIORITY = 10
|
15
|
+
|
16
|
+
def lock!
|
17
|
+
update_attribute(:locked, true)
|
18
|
+
end
|
19
|
+
|
20
|
+
def unlock!
|
21
|
+
update_attribute(:locked, false)
|
22
|
+
end
|
23
|
+
|
24
|
+
def unlocked?
|
25
|
+
!locked?
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute!
|
29
|
+
klass.to_s.constantize.new(data).execute!
|
30
|
+
end
|
31
|
+
|
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
|
+
|
44
|
+
task.update(perform_at: data[:perform_at]) if data[:perform_at].present?
|
45
|
+
task
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.queue_unless_already_queued(klass, data, weight = DEFAULT_PRIORITY)
|
49
|
+
task = find_by(klass: klass, data: data, weight: weight)
|
50
|
+
return task if task.present?
|
51
|
+
self.queue(klass, data, weight)
|
52
|
+
end
|
53
|
+
|
54
|
+
def dequeue!
|
55
|
+
destroy
|
56
|
+
end
|
57
|
+
|
58
|
+
def increase_attempts!
|
59
|
+
update_column(:attempts, attempts + 1)
|
60
|
+
end
|
61
|
+
|
62
|
+
def process!
|
63
|
+
lock!
|
64
|
+
|
65
|
+
begin
|
66
|
+
execute!
|
67
|
+
dequeue!
|
68
|
+
rescue
|
69
|
+
raise
|
70
|
+
ensure
|
71
|
+
if persisted?
|
72
|
+
increase_attempts!
|
73
|
+
unlock!
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def stuck?
|
79
|
+
return false if locked?
|
80
|
+
# Make sure task is past its perform_at timestamp.
|
81
|
+
return perform_at <= 30.minutes.ago if perform_at.present?
|
82
|
+
created_at <= 30.minutes.ago
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreatePlokQueuedTasks < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
return if table_exists?(:queued_tasks)
|
4
|
+
|
5
|
+
create_table :queued_tasks do |t|
|
6
|
+
t.string :klass
|
7
|
+
t.text :data
|
8
|
+
t.boolean :locked
|
9
|
+
t.datetime :perform_at
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
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
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.
|
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-
|
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: :
|
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
|
@@ -82,13 +82,19 @@ files:
|
|
82
82
|
- app/controllers/plok/version_controller.rb
|
83
83
|
- app/models/concerns/plok/loggable.rb
|
84
84
|
- app/models/log.rb
|
85
|
+
- app/models/queued_task.rb
|
85
86
|
- config/routes.rb
|
86
87
|
- db/migrate/20211008130809_create_plok_logs.rb
|
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
|
87
92
|
- lib/plok.rb
|
88
93
|
- lib/plok/engine.rb
|
89
94
|
- lib/plok/version.rb
|
90
95
|
- lib/tasks/plok_tasks.rake
|
91
96
|
- spec/factories/logs.rb
|
97
|
+
- spec/factories/queued_tasks.rb
|
92
98
|
- spec/support/plok/loggable.rb
|
93
99
|
homepage: https://plok.blimp.be
|
94
100
|
licenses:
|