plok 0.2.3 → 0.2.7
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 +4 -4
- data/app/models/queued_task.rb +64 -0
- data/db/migrate/20211015141837_create_plok_queued_tasks.rb +14 -0
- data/lib/plok/engine.rb +2 -2
- data/lib/plok/version.rb +1 -1
- data/spec/factories/logs.rb +5 -0
- data/spec/factories/queued_tasks.rb +5 -0
- data/spec/support/plok/loggable.rb +16 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f3772752bb79fb0e5c754451f428a38ffbc921f994c77baace8d553dc3c8534
|
4
|
+
data.tar.gz: 535cc01459273ea5486507ac0ac2c226462040985dc2aa4874170cf770ac568b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7242f280f4bc453387b191685b8a1b8857e4371379e5e4f9520cd47bd743c083344e9d9f50911657a0f129db7ce9d0ae793eeca56304bb3d6e8d2aba60432c7
|
7
|
+
data.tar.gz: eacde236f96f7a4109dcda5cf3f1647813ab82a11e7f3ba2e65e541e2106485d4964f854f9f7cdf4cc2371dede4eeb6e84374a3dcb6353421c06f0b7cab26ba6
|
@@ -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/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
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
shared_examples_for :loggable do
|
4
|
+
let(:klass) { described_class.to_s.underscore.to_sym }
|
5
|
+
|
6
|
+
it :logs do
|
7
|
+
a = create(klass)
|
8
|
+
a.log 'foo'
|
9
|
+
expect(a.logs.count).to be >= 1
|
10
|
+
expect(a.logs.unscoped.last.content).to eq 'foo'
|
11
|
+
end
|
12
|
+
|
13
|
+
it '#respond_to?' do
|
14
|
+
expect(subject).to respond_to(:logs, :log)
|
15
|
+
end
|
16
|
+
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.
|
4
|
+
version: 0.2.7
|
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-
|
12
|
+
date: 2021-10-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -82,12 +82,17 @@ 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
|
93
|
+
- spec/factories/logs.rb
|
94
|
+
- spec/factories/queued_tasks.rb
|
95
|
+
- spec/support/plok/loggable.rb
|
91
96
|
homepage: https://plok.blimp.be
|
92
97
|
licenses:
|
93
98
|
- MIT
|