circuit_switch 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 948143d874eac4049767af92dacd5f8175ad016a951b93e95e05e3a3585eaf70
4
- data.tar.gz: 98981a823c9fa4dd0c20259003766ecf789dd44f7ad0e7bd8983223338f5a550
3
+ metadata.gz: 6a00a04ad121a8f40a84f78a55e769aaa8f3fc185ac58fab51ddff11a0678e28
4
+ data.tar.gz: '06189169b6959b97e8a33617f2d9e054f9acf3d55bce2833b39a5f6567a061c6'
5
5
  SHA512:
6
- metadata.gz: 317208a17b15dfcc29e333c217ee7320b0133a721a918d2917cd3d172615a86ffe73394e74d407454b9ff8e16fa2be702d6d22cc91467793a9260a84bae792ad
7
- data.tar.gz: 3ec9b0893cca1eaee59e1aa40c7c3be95348b90760aff1a5fef59053e7dbeb72c627b93940e04b14d0b3c2c2d5159beb00dc13b6b6145b340871734346138428
6
+ metadata.gz: b63203a04cb3a37e4c6ac265eb8283fdde5b5cb5990c4234bb57876d72c5c26e48ade13907373046dc9f4b8f943f8c6a34e8c6957bceae9ba3857ca087c9c033
7
+ data.tar.gz: ff59240799e6841334eaffcaa76706cd78268804f8dc263da2c57dfd2bdc480264768ba614fb4102186fa483750ff36eb7c2c0bcc87eb6836f70c0025bcd0361
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 0.3.0
2
+
3
+ ### Breaking Changes
4
+
5
+ * Modify `key` to unique by default.
6
+ To migrate, run next.
7
+
8
+ ```
9
+ rails generate circuit_switch:migration circuit_switch make_key_unique
10
+ rails db:migrate
11
+ ```
12
+
13
+ ### Changes
14
+
15
+ * Fix to save switch when block for `CircuitSwitch.run` raises error.
16
+
1
17
  ## 0.2.2
2
18
 
3
19
  ### New features
data/README.md CHANGED
@@ -134,6 +134,24 @@ called_path: /app/services/greetings_service:21 block in validate
134
134
  /app/controllers/greetings_controller.rb:93 create
135
135
  ```
136
136
 
137
+ ## Test
138
+
139
+ To test, FactoryBot will look like this;
140
+
141
+ ```ruby
142
+ FactoryBot.define do
143
+ factory :circuit_switch, class: 'CircuitSwitch::CircuitSwitch' do
144
+ sequence(:key) { |n| "/path/to/file:#{n}" }
145
+ sequence(:caller) { |n| "/path/to/file:#{n}" }
146
+ due_date { Date.tomorrow }
147
+
148
+ trait :initially_closed do
149
+ run_is_terminated { true }
150
+ end
151
+ end
152
+ end
153
+ ```
154
+
137
155
  ## Task
138
156
 
139
157
  When find a problem and you want to terminate running or reporting right now, execute a task with it's caller.
@@ -1,5 +1,3 @@
1
- require 'active_support/core_ext/module/aliasing'
2
-
3
1
  module CircuitSwitch
4
2
  class Configuration
5
3
  CIRCUIT_SWITCH = 'circuit_switch'.freeze
@@ -25,6 +25,8 @@ module CircuitSwitch
25
25
  yield
26
26
  @run = true
27
27
  end
28
+ self
29
+ ensure
28
30
  RunCountUpdater.perform_later(
29
31
  key: key,
30
32
  limit_count: run_limit_count,
@@ -32,7 +34,6 @@ module CircuitSwitch
32
34
  reported: reported?,
33
35
  initially_closed: initially_closed
34
36
  )
35
- self
36
37
  end
37
38
 
38
39
  def execute_report
@@ -2,6 +2,8 @@ require 'active_record'
2
2
 
3
3
  module CircuitSwitch
4
4
  class CircuitSwitch < ::ActiveRecord::Base
5
+ validates :key, uniqueness: true
6
+
5
7
  after_initialize do |switch|
6
8
  switch.key ||= switch.caller
7
9
  end
@@ -28,11 +30,11 @@ module CircuitSwitch
28
30
  end
29
31
  end
30
32
 
31
- def increment_run_count
33
+ def increment_run_count!
32
34
  with_writable { update!(run_count: run_count + 1) }
33
35
  end
34
36
 
35
- def increment_report_count
37
+ def increment_report_count!
36
38
  with_writable { update!(report_count: report_count + 1) }
37
39
  end
38
40
 
@@ -1,3 +1,3 @@
1
1
  module CircuitSwitch
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -9,17 +9,26 @@ module CircuitSwitch
9
9
  # Wait for RunCountUpdater saves circuit_switch
10
10
  sleep(3) if run
11
11
 
12
- circuit_switch = key ? CircuitSwitch.find_by(key: key) : CircuitSwitch.find_by(caller: called_path)
13
- if run && circuit_switch.nil?
14
- raise ActiveRecord::RecordNotFound.new('Couldn\'t find CircuitSwitch::CircuitSwitch')
15
- end
12
+ first_raise = true
13
+ begin
14
+ circuit_switch = key ? CircuitSwitch.find_by(key: key) : CircuitSwitch.find_by(caller: called_path)
15
+ if run && circuit_switch.nil?
16
+ raise ActiveRecord::RecordNotFound.new('Couldn\'t find CircuitSwitch::CircuitSwitch')
17
+ end
18
+
19
+ circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path)
20
+ circuit_switch.due_date ||= config.due_date
21
+ circuit_switch.assign(report_limit_count: limit_count).increment_report_count!
22
+ raise CalledNotification.new(circuit_switch.message)
23
+ rescue ActiveRecord::RecordInvalid => e
24
+ raise e unless first_raise
16
25
 
17
- circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path)
18
- circuit_switch.due_date ||= config.due_date
19
- circuit_switch.assign(report_limit_count: limit_count).increment_report_count
20
- raise CalledNotification.new(circuit_switch.message)
21
- rescue CalledNotification => notification
22
- config.reporter.call(notification.to_message(called_path: called_path))
26
+ first_raise = false
27
+ sleep(2)
28
+ retry
29
+ rescue CalledNotification => notification
30
+ config.reporter.call(notification.to_message(called_path: called_path))
31
+ end
23
32
  end
24
33
  end
25
34
  end
@@ -9,14 +9,23 @@ module CircuitSwitch
9
9
  # Wait for Reporter saves circuit_switch
10
10
  sleep(3) if reported
11
11
 
12
- circuit_switch = key ? CircuitSwitch.find_by(key: key) : CircuitSwitch.find_by(caller: called_path)
13
- if reported && circuit_switch.nil?
14
- raise ActiveRecord::RecordNotFound.new('Couldn\'t find CircuitSwitch::CircuitSwitch')
15
- end
12
+ first_raise = true
13
+ begin
14
+ circuit_switch = key ? CircuitSwitch.find_by(key: key) : CircuitSwitch.find_by(caller: called_path)
15
+ if reported && circuit_switch.nil?
16
+ raise ActiveRecord::RecordNotFound.new('Couldn\'t find CircuitSwitch::CircuitSwitch')
17
+ end
18
+
19
+ circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path, run_is_terminated: initially_closed)
20
+ circuit_switch.due_date ||= config.due_date
21
+ circuit_switch.assign(run_limit_count: limit_count).increment_run_count!
22
+ rescue ActiveRecord::RecordInvalid => e
23
+ raise e unless first_raise
16
24
 
17
- circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path, run_is_terminated: initially_closed)
18
- circuit_switch.due_date ||= config.due_date
19
- circuit_switch.assign(run_limit_count: limit_count).increment_run_count
25
+ first_raise = false
26
+ sleep(2)
27
+ retry
28
+ end
20
29
  end
21
30
  end
22
31
  end
@@ -5,12 +5,15 @@ module CircuitSwitch
5
5
  desc 'Create a migration to manage circuit switches state'
6
6
  source_root File.expand_path('templates', __dir__)
7
7
  argument :migration_type, required: false, type: :array, default: ['create'],
8
- desc: 'Type of migration to create or add key column. By default to create.',
8
+ desc: 'Type of migration to create or add key column or make key unique. By default to create.',
9
9
  banner: 'create or add_key'
10
10
 
11
11
  def generate_migration
12
- if migration_type == ['add_key']
12
+ case migration_type
13
+ when ['add_key']
13
14
  migration_template 'add_key.rb.erb', 'db/migrate/add_key_to_circuit_switches.rb', migration_version: migration_version
15
+ when ['make_key_unique']
16
+ migration_template 'make_key_unique.rb.erb', 'db/migrate/make_key_unique_for_circuit_switches.rb', migration_version: migration_version
14
17
  else
15
18
  migration_template 'migration.rb.erb', 'db/migrate/create_circuit_switches.rb', migration_version: migration_version
16
19
  end
@@ -29,7 +29,7 @@ CircuitSwitch.configure do |config|
29
29
 
30
30
  # Option to contain error backtrace for report
31
31
  # You don't need backtrace when you report to some bug report tool.
32
- # You may be want backtrace when report to plain feed; e.g. Slack or email.
32
+ # You may want backtrace when reporting to a plain feed; e.g. Slack or email.
33
33
  # config.with_backtrace = false
34
34
 
35
35
  # Allowed backtrace paths to report
@@ -0,0 +1,11 @@
1
+ class MakeKeyUniqueForCircuitSwitches < ActiveRecord::Migration<%= migration_version %>
2
+ def up
3
+ remove_index :circuit_switches, :key
4
+ add_index :circuit_switches, :key, unique: true
5
+ end
6
+
7
+ def down
8
+ remove_index :circuit_switches, :key
9
+ add_index :circuit_switches, :key
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
1
  class CreateCircuitSwitches < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :circuit_switches do |t|
4
- t.string :key, null: false, index: true
4
+ t.string :key, null: false
5
5
  t.string :caller, null: false
6
6
  t.integer :run_count, default: 0, null: false
7
7
  t.integer :run_limit_count, default: 10, null: false
@@ -12,5 +12,7 @@ class CreateCircuitSwitches < ActiveRecord::Migration<%= migration_version %>
12
12
  t.date :due_date, null: false
13
13
  t.timestamps
14
14
  end
15
+
16
+ add_index :circuit_switches, [:key], unique: true
15
17
  end
16
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuit_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - makicamel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-31 00:00:00.000000000 Z
11
+ date: 2021-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -145,6 +145,7 @@ files:
145
145
  - lib/generators/circuit_switch/migration_generator.rb
146
146
  - lib/generators/circuit_switch/templates/add_key.rb.erb
147
147
  - lib/generators/circuit_switch/templates/initializer.rb
148
+ - lib/generators/circuit_switch/templates/make_key_unique.rb.erb
148
149
  - lib/generators/circuit_switch/templates/migration.rb.erb
149
150
  homepage: https://github.com/makicamel/circuit_switch
150
151
  licenses: