delay_henka 0.3.3 → 0.4.0

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
  SHA1:
3
- metadata.gz: cbdcde775e9289dbb082a616c1283ae3ff2f3f63
4
- data.tar.gz: 4a809b9c2f32a7048ce78a39eb215a4e9d25bff4
3
+ metadata.gz: e5061e5afca8b978824aca8b8b33372bf9254a70
4
+ data.tar.gz: 266de2770b1bb768edf0fe080a9c17c4c09b05bf
5
5
  SHA512:
6
- metadata.gz: 7f4e5997974490a3b7b809adef7b9728e6a6cc5a8ddc05e4f84723a608a653d2b22d573d7a4dd4244dd6b4c820e0ef54f98b77025c483213f014ecf3a3c6561c
7
- data.tar.gz: 7a7fa29f2501a046e5bc85413b3f95d849ce0d0adefb0bfdbff56eddb07f8b642bdf26dbcdd3409c84d61991afd5b6f1138d93cff36dcee73b8b4a7360f28863
6
+ metadata.gz: 287d1d32a51cf1fd3df5774e9c1b5d182052eb71d4ec68b38745362b2f7091ee2fce43c8889a2cb6b258736513bd3cb0594cd2f86cb4031a05537551ed001402
7
+ data.tar.gz: 889d79073d7ca291fb1e9f5e457431679b5fbdf0058c70ca003a635873b54b81c59bb3ebff1faf636cdc61d57098912881b5077ec62c82139dc738cc497de07c
@@ -0,0 +1,61 @@
1
+ module DelayHenka
2
+ class ScheduledAction < ApplicationRecord
3
+
4
+ STATES = {
5
+ STAGED: 'staged',
6
+ COMPLETED: 'completed',
7
+ ERRORED: 'errored'
8
+ }
9
+
10
+ belongs_to :actionable, polymorphic: true
11
+
12
+ validates :submitted_by_id, :schedule_at, presence: true
13
+ validates :state, inclusion: { in: STATES.values }
14
+ after_initialize :set_initial_state, if: :new_record?
15
+
16
+ scope :staged, -> { where(state: STATES[:STAGED]) }
17
+
18
+ def self.schedule(record:, method_name:, argument: nil, by_id:, schedule_at: Time.current)
19
+ Keka.run do
20
+ begin
21
+ arity = record.method(method_name.to_sym).arity
22
+ Keka.err_unless! (arity == 0 && argument.nil?) || (arity == 1 && !argument.nil?), 'wrong arity'
23
+ DelayHenka::ScheduledAction.create(
24
+ actionable: record,
25
+ method_name: method_name,
26
+ argument: argument.to_json,
27
+ submitted_by_id: by_id,
28
+ schedule_at: schedule_at
29
+ )
30
+ rescue NameError => e
31
+ Keka.err_if! true, e.message
32
+ end
33
+ end
34
+ end
35
+
36
+ def apply_action
37
+ unless actionable
38
+ # Caution: model validations are bypassed
39
+ update_columns(state: STATES[:ERRORED], error_message: 'Target record cannot be found')
40
+ return
41
+ end
42
+
43
+ begin
44
+ output = actionable.method(method_name.to_sym).arity == 0 ?
45
+ actionable.send(method_name) :
46
+ actionable.send(method_name, JSON.parse(argument))
47
+ update!(state: STATES[:COMPLETED], return_value: output.to_json)
48
+ rescue => e
49
+ update!(state: STATES[:ERRORED], error_message: e.message)
50
+ raise e
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def set_initial_state
57
+ self.state ||= STATES[:STAGED]
58
+ end
59
+
60
+ end
61
+ end
@@ -49,12 +49,12 @@ module DelayHenka
49
49
  def apply_change
50
50
  if changeable
51
51
  if changeable.update(attribute_name => new_value)
52
- update(state: STATES[:COMPLETED])
52
+ update!(state: STATES[:COMPLETED])
53
53
  else
54
- update(state: STATES[:ERRORED], error_message: changeable.errors.full_messages.join(', '))
54
+ update!(state: STATES[:ERRORED], error_message: changeable.errors.full_messages.join(', '))
55
55
  end
56
56
  else
57
- update(state: STATES[:ERRORED], error_message: 'Target record cannot be found')
57
+ update_columns(state: STATES[:ERRORED], error_message: 'Target record cannot be found')
58
58
  end
59
59
  end
60
60
 
@@ -0,0 +1,14 @@
1
+ module DelayHenka
2
+ class ApplyActionsWorker
3
+
4
+ include Sidekiq::Worker
5
+
6
+ def perform
7
+ ScheduledAction.staged
8
+ .where('schedule_at <= ?', Time.current)
9
+ .includes(:actionable)
10
+ .find_each(&:apply_action)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ class CreateDelayHenkaScheduledActions < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :delay_henka_scheduled_actions do |t|
4
+ t.references :actionable, polymorphic: true, null: false, index: { name: 'actionable_index' }
5
+ t.string :method_name, null: false
6
+ t.string :state, null: false, index: true
7
+ t.string :error_message
8
+ t.integer :submitted_by_id, null: false
9
+ t.datetime :schedule_at, index: true, null: false
10
+ t.jsonb :argument
11
+ t.jsonb :return_value
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module DelayHenka
2
- VERSION = '0.3.3'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delay_henka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-31 00:00:00.000000000 Z
11
+ date: 2019-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -125,15 +125,18 @@ files:
125
125
  - app/controllers/delay_henka/web/admin/scheduled_changes_controller.rb
126
126
  - app/helpers/delay_henka/application_helper.rb
127
127
  - app/models/delay_henka/application_record.rb
128
+ - app/models/delay_henka/scheduled_action.rb
128
129
  - app/models/delay_henka/scheduled_change.rb
129
130
  - app/services/delay_henka/whether_schedule.rb
130
131
  - app/views/delay_henka/web/admin/scheduled_changes/_summary_table.html.haml
131
132
  - app/views/delay_henka/web/admin/scheduled_changes/index.html.haml
132
133
  - app/views/layouts/delay_henka/application.html.erb
134
+ - app/workers/delay_henka/apply_actions_worker.rb
133
135
  - app/workers/delay_henka/apply_changes_worker.rb
134
136
  - config/routes.rb
135
137
  - db/migrate/20181228205218_create_delay_henka_scheduled_changes.rb
136
138
  - db/migrate/20190225140848_add_schedule_at.rb
139
+ - db/migrate/20190408130947_create_delay_henka_scheduled_actions.rb
137
140
  - lib/delay_henka.rb
138
141
  - lib/delay_henka/engine.rb
139
142
  - lib/delay_henka/version.rb