plok 0.2.4 → 0.2.5
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 +64 -0
- data/db/migrate/20211015141837_create_plok_queued_tasks.rb +14 -0
- data/lib/plok/version.rb +1 -1
- data/spec/factories/queued_tasks.rb +5 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e4560c0f41784811539873f92cbda25d73197ac1403d3ee1c1b3f046b0e0a0d
|
4
|
+
data.tar.gz: 95a866614905153ac92414e9dd8e12a60e0d6539118fd4dbf5b9427db323bd3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc64c91c32ea195fdfab4596acdfbc1611d4790d71121ff083eb8297b1b59ac7b2427de5f02ce95ce3382f57bc679d30e147b70e31bd1fc5a142c06e6aba7528
|
7
|
+
data.tar.gz: 63bbbb1e6dadc2b8bd667e6172c59110f893050d6978707bb492807872f6aad702c73fb8b5f3f76e448f58f2c4dd24b3eaa0bfd58a4064734e51d324ef4a762c
|
@@ -0,0 +1,64 @@
|
|
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
|
+
def lock!
|
14
|
+
update_attribute :locked, true
|
15
|
+
end
|
16
|
+
|
17
|
+
def unlock!
|
18
|
+
update_attribute :locked, false
|
19
|
+
end
|
20
|
+
|
21
|
+
def unlocked?
|
22
|
+
!locked?
|
23
|
+
end
|
24
|
+
|
25
|
+
def execute!
|
26
|
+
klass.to_s.constantize.new(data).execute!
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.queue(klass, data)
|
30
|
+
task = create!(klass: klass.to_s, data: data.except(:perform_at))
|
31
|
+
task.update(perform_at: data[:perform_at]) if data[:perform_at].present?
|
32
|
+
task
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.queue_unless_already_queued(klass, data)
|
36
|
+
task = find_by(klass: klass, data: data)
|
37
|
+
return task if task.present?
|
38
|
+
self.queue(klass, data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def dequeue!
|
42
|
+
destroy
|
43
|
+
end
|
44
|
+
|
45
|
+
def process!
|
46
|
+
lock!
|
47
|
+
|
48
|
+
begin
|
49
|
+
execute!
|
50
|
+
dequeue!
|
51
|
+
rescue
|
52
|
+
raise
|
53
|
+
ensure
|
54
|
+
unlock! unless destroyed?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def stuck?
|
59
|
+
return false if locked?
|
60
|
+
# Make sure task is past its perform_at timestamp.
|
61
|
+
return perform_at <= 30.minutes.ago if perform_at.present?
|
62
|
+
created_at <= 30.minutes.ago
|
63
|
+
end
|
64
|
+
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/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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Davy Hellemans
|
@@ -82,13 +82,16 @@ 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
|
87
89
|
- lib/plok.rb
|
88
90
|
- lib/plok/engine.rb
|
89
91
|
- lib/plok/version.rb
|
90
92
|
- lib/tasks/plok_tasks.rake
|
91
93
|
- spec/factories/logs.rb
|
94
|
+
- spec/factories/queued_tasks.rb
|
92
95
|
- spec/support/plok/loggable.rb
|
93
96
|
homepage: https://plok.blimp.be
|
94
97
|
licenses:
|