circuit_switch 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: d036ceb2748b3432749f5a03d83d324b59d1acb340bb7d64cbe3ef86f5531c4f
4
- data.tar.gz: 32b8e3dbe02c1eaf8e25b40ab26f97036ecf33402bf974e6b71cb989db8c3178
3
+ metadata.gz: 9281021c31b4e885f3e8c8ca7fbd1fd83e713202950776c910f9b0951891a5c6
4
+ data.tar.gz: 8f4969657c64c2016724fabadd8d2cbddf063e6ffc3dd0298fb2e9e0b06e688a
5
5
  SHA512:
6
- metadata.gz: fa10c7a2196b37619dc94605efca82c8b3ce5b3182063004911f8aa28bef44f711e945adc73f38857e85a40a6f005b1139a7fac990e76e39a45080e481a93c3f
7
- data.tar.gz: 3619df1cd293ba93f271ca536bcb55f4152a20810e52fb7aba16cd44e11b6bd081645bc1804f4f01c47a49c056a1793e840de5715f4c54126b4bdda8887f1d0c
6
+ metadata.gz: 94cc63d4de19ffdc4cea5a5e331af21ab458f26f5333a6ceb4b84c7b3ba13edb44ebee7c0379c555d1ea7f390e8a31cb159c35c8d181cd8dec4a16cb049507ab
7
+ data.tar.gz: 2f490f1e01f4e05351cbde8ee6fe328e200c020dcbb0d83a1f627983a47c192f4c4ab8f5c3b3878bd58e7a026003d0bd79a5995044987b275b59b6d9902f478b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.2.1
2
+
3
+ ### New features
4
+
5
+ * Add `initially_closed` option to `run` and `open?`.
6
+ * Add `key` argument for easy handling for human more than caller.
7
+ To migrate, run next.
8
+
9
+ ```
10
+ rails generate circuit_switch:migration add_key
11
+ rails db:migrate
12
+ ```
13
+
14
+ ### Changes
15
+
16
+ * Modify log level from warn to info when default value for `close_if_reach_limit` is used.
17
+ * Suppress warning that ivar is not initialized.
18
+
1
19
  ## 0.2.0
2
20
 
3
21
  ### Breaking Changes
@@ -6,7 +24,7 @@
6
24
 
7
25
  ## 0.1.2
8
26
 
9
- * Modify `CircuitSwitch.open?` recieves same arguments as `CircuitSwtich.run`
27
+ * Modify `CircuitSwitch.open?` receives same arguments as `CircuitSwitch.run`
10
28
 
11
29
  ## 0.1.1
12
30
 
data/README.md CHANGED
@@ -37,7 +37,7 @@ rails generate circuit_switch:install
37
37
  ```
38
38
 
39
39
  Generate a migration for ActiveRecord.
40
- This table saves circuit caller, called count, limit count and so on.
40
+ This table saves named key, circuit caller, called count, limit count and so on.
41
41
 
42
42
  ```
43
43
  rails generate circuit_switch:migration circuit_switch
@@ -61,11 +61,14 @@ To switch circuit opening and closing, some conditions can be set. By default, t
61
61
  You can also set `limit_count` to close circuit when reached the specified count. Default limit_count is 10. To change this default value, modify `circuit_switches.run_limit_count` default value in the migration file.
62
62
  `run` receives optional arguments.
63
63
 
64
+ - `key`: [String] Named key to find switch instead of caller
65
+ If no key passed, use caller.
64
66
  - `if`: [Boolean, Proc] Call proc when `if` is truthy (default: true)
65
67
  - `close_if`: [Boolean, Proc] Call proc when `close_if` is falsy (default: false)
66
68
  - `close_if_reach_limit`: [Boolean] Stop calling proc when run count reaches limit (default: false)
67
69
  - `limit_count`: [Integer] Limit count. Use `run_limit_count` default value if it's nil (default: nil)
68
70
  Can't be set 0 when `close_if_reach_limit` is true
71
+ - `initially_closed`: [Boolean] Create switch with terminated mode (default: false)
69
72
 
70
73
  To close the circuit at specific date or when called 1000 times, code goes like:
71
74
 
@@ -106,6 +109,8 @@ CircuitSwitch.report(if: some_condition)
106
109
  Same as `run`, some conditions can be set. By default, reporting is stopped when reached the specified count. The default count is 10. To change this default value, modify `circuit_switches.report_limit_count` default value in the migration file.
107
110
  `report` receives optional arguments.
108
111
 
112
+ - `key`: [String] Named key to find switch instead of caller
113
+ If no key passed, use caller.
109
114
  - `if`: [Boolean, Proc] Report when `if` is truthy (default: true)
110
115
  - `stop_report_if`: [Boolean, Proc] Report when `close_if` is falsy (default: false)
111
116
  - `stop_report_if_reach_limit`: [Boolean] Stop reporting when reported count reaches limit (default: true)
@@ -131,10 +136,11 @@ called_path: /app/services/greetings_service:21 block in validate
131
136
 
132
137
  ## Task
133
138
 
134
- When find a problem and you want to terminate running or reporting right now, execute a task with it's caller.
139
+ When find a problem and you want to terminate running or reporting right now, execute a task with it's caller.
140
+ You can specify either key or caller.
135
141
 
136
142
  ```
137
- rake circuit_switch:terminate_to_run[/app/services/greetings_service:21 block in validate]
143
+ rake circuit_switch:terminate_to_run[your_key]
138
144
  rake circuit_switch:terminate_to_report[/app/services/greetings_service:21 block in validate]
139
145
  ```
140
146
 
@@ -0,0 +1,66 @@
1
+ require_relative 'core'
2
+
3
+ module CircuitSwitch
4
+ class Builder < Core
5
+ def initialize
6
+ super
7
+ @run = false
8
+ @reported = false
9
+ end
10
+
11
+ def assign_runner(
12
+ key: nil,
13
+ if: true,
14
+ close_if: false,
15
+ close_if_reach_limit: nil,
16
+ limit_count: nil,
17
+ initially_closed: false
18
+ )
19
+ @key = key
20
+ @run_if = binding.local_variable_get(:if)
21
+ @close_if = close_if
22
+ @close_if_reach_limit = close_if_reach_limit
23
+ @run_limit_count = limit_count
24
+ @initially_closed = initially_closed
25
+ end
26
+
27
+ def assign_reporter(
28
+ key: nil,
29
+ if: true,
30
+ stop_report_if: false,
31
+ stop_report_if_reach_limit: true,
32
+ limit_count: nil
33
+ )
34
+ @key = key
35
+ @report_if = binding.local_variable_get(:if)
36
+ @stop_report_if = stop_report_if
37
+ @stop_report_if_reach_limit = stop_report_if_reach_limit
38
+ @report_limit_count = limit_count
39
+ end
40
+
41
+ def run(key: nil, if: nil, close_if: nil, close_if_reach_limit: nil, limit_count: nil, initially_closed: nil, &block)
42
+ arguments = {
43
+ key: key,
44
+ if: binding.local_variable_get(:if),
45
+ close_if: close_if,
46
+ close_if_reach_limit: close_if_reach_limit,
47
+ limit_count: limit_count,
48
+ initially_closed: initially_closed,
49
+ }.select { |_, v| v }
50
+ assign_runner(**arguments)
51
+ execute_run(&block)
52
+ end
53
+
54
+ def report(key: nil, if: nil, stop_report_if: nil, stop_report_if_reach_limit: nil, limit_count: nil)
55
+ arguments = {
56
+ key: key,
57
+ if: binding.local_variable_get(:if),
58
+ stop_report_if: stop_report_if,
59
+ stop_report_if_reach_limit: stop_report_if_reach_limit,
60
+ limit_count: limit_count
61
+ }.select { |_, v| v }
62
+ assign_reporter(**arguments)
63
+ execute_report
64
+ end
65
+ end
66
+ end
@@ -1,54 +1,55 @@
1
+ require_relative 'notification'
2
+ require_relative 'workers/reporter'
3
+ require_relative 'workers/run_count_updater'
4
+
1
5
  module CircuitSwitch
2
6
  class Core
3
7
  delegate :config, to: ::CircuitSwitch
8
+ attr_reader :key, :run_if, :close_if, :close_if_reach_limit, :run_limit_count, :initially_closed,
9
+ :report_if, :stop_report_if, :stop_report_if_reach_limit, :report_limit_count
4
10
 
5
- def run(
6
- if: true,
7
- close_if: false,
8
- close_if_reach_limit: nil,
9
- limit_count: nil,
10
- &block
11
- )
12
- if close_if_reach_limit && limit_count == 0
11
+ def execute_run(&block)
12
+ if close_if_reach_limit && run_limit_count == 0
13
13
  raise CircuitSwitchError.new('Can\'t set limit_count to 0 when close_if_reach_limit is true')
14
14
  end
15
15
  if close_if_reach_limit.nil?
16
- Logger.new($stdout).warn('Default value for close_if_reach_limit is modified from true to false at ver 0.2.0.')
17
- close_if_reach_limit = false
16
+ Logger.new($stdout).info('Default value for close_if_reach_limit is modified from true to false at ver 0.2.0.')
17
+ @close_if_reach_limit = false
18
18
  end
19
- return self if evaluate(close_if) || !evaluate(binding.local_variable_get(:if))
20
- return self if close_if_reach_limit && switch.reached_run_limit?(limit_count)
19
+
20
+ return self if evaluate(close_if) || !evaluate(run_if)
21
+ return self if close_if_reach_limit && switch.reached_run_limit?(run_limit_count)
21
22
  return self if switch.run_is_terminated?
22
23
 
23
- yield
24
+ unless switch.new_record? && initially_closed
25
+ yield
26
+ @run = true
27
+ end
24
28
  RunCountUpdater.perform_later(
25
- limit_count: limit_count,
29
+ key: key,
30
+ limit_count: run_limit_count,
26
31
  called_path: called_path,
27
- reported: reported?
32
+ reported: reported?,
33
+ initially_closed: initially_closed
28
34
  )
29
- @run = true
30
35
  self
31
36
  end
32
37
 
33
- def report(
34
- if: true,
35
- stop_report_if: false,
36
- stop_report_if_reach_limit: true,
37
- limit_count: nil
38
- )
38
+ def execute_report
39
39
  if config.reporter.nil?
40
40
  raise CircuitSwitchError.new('Set config.reporter.')
41
41
  end
42
- if stop_report_if_reach_limit && limit_count == 0
42
+ if stop_report_if_reach_limit && report_limit_count == 0
43
43
  raise CircuitSwitchError.new('Can\'t set limit_count to 0 when stop_report_if_reach_limit is true')
44
44
  end
45
45
  return self unless config.enable_report?
46
- return self if evaluate(stop_report_if) || !evaluate(binding.local_variable_get(:if))
46
+ return self if evaluate(stop_report_if) || !evaluate(report_if)
47
47
  return self if switch.report_is_terminated?
48
- return self if stop_report_if_reach_limit && switch.reached_report_limit?(limit_count)
48
+ return self if stop_report_if_reach_limit && switch.reached_report_limit?(report_limit_count)
49
49
 
50
50
  Reporter.perform_later(
51
- limit_count: limit_count,
51
+ key: key,
52
+ limit_count: report_limit_count,
52
53
  called_path: called_path,
53
54
  run: run?
54
55
  )
@@ -58,18 +59,24 @@ module CircuitSwitch
58
59
 
59
60
  # @return [Boolean]
60
61
  def run?
61
- !!@run
62
+ @run
62
63
  end
63
64
 
64
65
  # @return [Boolean]
65
66
  def reported?
66
- !!@reported
67
+ @reported
67
68
  end
68
69
 
69
70
  private
70
71
 
71
72
  def switch
72
- @switch ||= CircuitSwitch.find_or_initialize_by(caller: called_path)
73
+ return @switch if defined? @switch
74
+
75
+ if key
76
+ @switch = CircuitSwitch.find_or_initialize_by(key: key)
77
+ else
78
+ @switch = CircuitSwitch.find_or_initialize_by(caller: called_path)
79
+ end
73
80
  end
74
81
 
75
82
  def called_path
@@ -1,4 +1,4 @@
1
- require 'circuit_switch/stacktrace_modifier'
1
+ require_relative 'stacktrace_modifier'
2
2
 
3
3
  module CircuitSwitch
4
4
  class CircuitSwitchError < RuntimeError
@@ -1,5 +1,11 @@
1
+ require 'active_record'
2
+
1
3
  module CircuitSwitch
2
4
  class CircuitSwitch < ::ActiveRecord::Base
5
+ after_initialize do |switch|
6
+ switch.key ||= switch.caller
7
+ end
8
+
3
9
  def assign(run_limit_count: nil, report_limit_count: nil)
4
10
  self.run_limit_count = run_limit_count if run_limit_count
5
11
  self.report_limit_count = report_limit_count if report_limit_count
@@ -31,7 +37,8 @@ module CircuitSwitch
31
37
  end
32
38
 
33
39
  def message
34
- "Watching process is called for #{report_count}th. Report until for #{report_limit_count}th."
40
+ process = key == caller ? 'Watching process' : "Process for '#{key}'"
41
+ "#{process} is called for #{report_count}th. Report until for #{report_limit_count}th."
35
42
  end
36
43
 
37
44
  private
@@ -1,4 +1,4 @@
1
- require 'circuit_switch/configuration.rb'
1
+ require 'active_support/core_ext/module/delegation'
2
2
 
3
3
  module CircuitSwitch
4
4
  class StacktraceModifier
@@ -1,40 +1,40 @@
1
1
  namespace :circuit_switch do
2
2
  desc 'Update run_is_terminated to true to close circuit'
3
- task :terminate_to_run, ['caller'] => :environment do |_, arg|
4
- called_path = arg[:caller]
5
- puts "Start to update run_is_terminated of circuit_switch for '#{called_path}' to true."
3
+ task :terminate_to_run, ['key_or_caller'] => :environment do |_, arg|
4
+ key_or_caller = arg[:key_or_caller]
5
+ puts "Start to update run_is_terminated of circuit_switch for '#{key_or_caller}' to true."
6
6
  sleep(3)
7
7
 
8
- switch = CircuitSwitch::CircuitSwitch.find_by!(caller: called_path)
8
+ switch = CircuitSwitch::CircuitSwitch.find_by(key: key_or_caller) || CircuitSwitch::CircuitSwitch.find_by!(caller: key_or_caller)
9
9
  puts "circuit_switch is found. id: #{switch.id}."
10
10
 
11
11
  switch.update(run_is_terminated: true)
12
- puts "Updated run_is_terminated of circuit_switch for '#{called_path}' to true."
12
+ puts "Updated run_is_terminated of circuit_switch for '#{key_or_caller}' to true."
13
13
  end
14
14
 
15
15
  desc 'Update report_is_terminated to true to stop reporting'
16
- task :terminate_to_report, ['caller'] => :environment do |_, arg|
17
- called_path = arg[:caller]
18
- puts "Start to update report_is_terminated of circuit_switch for '#{called_path}' to true."
16
+ task :terminate_to_report, ['key_or_caller'] => :environment do |_, arg|
17
+ key_or_caller = arg[:key_or_caller]
18
+ puts "Start to update report_is_terminated of circuit_switch for '#{key_or_caller}' to true."
19
19
  sleep(3)
20
20
 
21
- switch = CircuitSwitch::CircuitSwitch.find_by!(caller: called_path)
21
+ switch = CircuitSwitch::CircuitSwitch.find_by(key: key_or_caller) || CircuitSwitch::CircuitSwitch.find_by!(caller: key_or_caller)
22
22
  puts "circuit_switch is found. id: #{switch.id}."
23
23
 
24
24
  switch.update(report_is_terminated: true)
25
- puts "Updated report_is_terminated of circuit_switch for '#{called_path}' to true."
25
+ puts "Updated report_is_terminated of circuit_switch for '#{key_or_caller}' to true."
26
26
  end
27
27
 
28
28
  desc 'Delete switch'
29
- task :delete_switch, ['caller'] => :environment do |_, arg|
30
- called_path = arg[:caller]
31
- puts "Start to delete circuit_switch for '#{called_path}'."
29
+ task :delete_switch, ['key_or_caller'] => :environment do |_, arg|
30
+ key_or_caller = arg[:key_or_caller]
31
+ puts "Start to delete circuit_switch for '#{key_or_caller}'."
32
32
  sleep(3)
33
33
 
34
- switch = CircuitSwitch::CircuitSwitch.find_by!(caller: called_path)
34
+ switch = CircuitSwitch::CircuitSwitch.find_by(key: key_or_caller) || CircuitSwitch::CircuitSwitch.find_by!(caller: key_or_caller)
35
35
  puts "circuit_switch is found. id: #{switch.id}."
36
36
 
37
37
  switch.destroy!
38
- puts "Successfully deleted circuit_switch for '#{called_path}'."
38
+ puts "Successfully deleted circuit_switch for '#{key_or_caller}'."
39
39
  end
40
40
  end
@@ -1,3 +1,3 @@
1
1
  module CircuitSwitch
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -10,7 +10,7 @@ module CircuitSwitch
10
10
  circuit_switches = CircuitSwitch.where('due_date <= ?', Date.today).order(id: :asc)
11
11
  if circuit_switches.present?
12
12
  message = "Due date has come! Let's consider about removing switches and cleaning up code! :)\n" +
13
- circuit_switches.map { |switch| "id: #{switch.id}, caller: '#{switch.caller}' , created_at: #{switch.created_at}" }.join("\n")
13
+ circuit_switches.map { |switch| "id: #{switch.id}, key: '#{switch.key}', caller: '#{switch.caller}', created_at: #{switch.created_at}" }.join("\n")
14
14
  config.due_date_notifier.call(message)
15
15
  else
16
16
  switches_count = CircuitSwitch.all.size
@@ -1,16 +1,20 @@
1
+ require 'active_job'
2
+ require 'active_support/core_ext/module/delegation'
3
+
1
4
  module CircuitSwitch
2
5
  class Reporter < ::ActiveJob::Base
3
6
  delegate :config, to: ::CircuitSwitch
4
7
 
5
- def perform(limit_count:, called_path:, run:)
6
- circuit_switch =
7
- if run
8
- # Wait for RunCountUpdater saves circuit_switch
9
- sleep(3)
10
- CircuitSwitch.find_by!(caller: called_path)
11
- else
12
- CircuitSwitch.find_or_initialize_by(caller: called_path)
8
+ def perform(key:, limit_count:, called_path:, run:)
9
+ # Wait for RunCountUpdater saves circuit_switch
10
+ sleep(3) if run
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')
13
15
  end
16
+
17
+ circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path)
14
18
  circuit_switch.due_date ||= config.due_date
15
19
  circuit_switch.assign(report_limit_count: limit_count).increment_report_count
16
20
  raise CalledNotification.new(circuit_switch.message)
@@ -1,16 +1,20 @@
1
+ require 'active_job'
2
+ require 'active_support/core_ext/module/delegation'
3
+
1
4
  module CircuitSwitch
2
5
  class RunCountUpdater < ::ActiveJob::Base
3
6
  delegate :config, to: ::CircuitSwitch
4
7
 
5
- def perform(limit_count:, called_path:, reported:)
6
- circuit_switch =
7
- if reported
8
- # Wait for Reporter saves circuit_switch
9
- sleep(3)
10
- CircuitSwitch.find_by!(caller: called_path)
11
- else
12
- CircuitSwitch.find_or_initialize_by(caller: called_path)
13
- end
8
+ def perform(key:, limit_count:, called_path:, reported:, initially_closed:)
9
+ # Wait for Reporter saves circuit_switch
10
+ sleep(3) if reported
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
16
+
17
+ circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path, run_is_terminated: initially_closed)
14
18
  circuit_switch.due_date ||= config.due_date
15
19
  circuit_switch.assign(run_limit_count: limit_count).increment_run_count
16
20
  end
@@ -1,12 +1,9 @@
1
- require "circuit_switch/configuration.rb"
2
- require "circuit_switch/core"
3
- require "circuit_switch/notification"
4
- require "circuit_switch/orm/active_record/circuit_switch"
5
- require "circuit_switch/railtie" if defined?(Rails::Railtie)
6
- require "circuit_switch/version"
7
- require "circuit_switch/workers/due_date_notifier"
8
- require "circuit_switch/workers/reporter"
9
- require "circuit_switch/workers/run_count_updater"
1
+ require_relative 'circuit_switch/configuration'
2
+ require_relative 'circuit_switch/builder'
3
+ require_relative 'circuit_switch/orm/active_record/circuit_switch'
4
+ require_relative 'circuit_switch/railtie' if defined?(Rails::Railtie)
5
+ require_relative 'circuit_switch/version'
6
+ require_relative 'circuit_switch/workers/due_date_notifier'
10
7
 
11
8
  module CircuitSwitch
12
9
  class << self
@@ -18,73 +15,69 @@ module CircuitSwitch
18
15
  @config ||= Configuration.new
19
16
  end
20
17
 
18
+ # @param key [String] Named key to find switch instead of caller
21
19
  # @param if [Boolean, Proc] Call proc when `if` is truthy (default: true)
22
20
  # @param close_if [Boolean, Proc] Call proc when `close_if` is falsy (default: false)
23
21
  # @param close_if_reach_limit [Boolean] Stop calling proc when run count reaches limit (default: false)
24
22
  # @param limit_count [Integer] Limit count. Use `run_limit_count` default value if it's nil
25
23
  # Can't be set 0 when `close_if_reach_limit` is true (default: nil)
24
+ # @param initially_closed [Boolean] Create switch with terminated mode (default: false)
26
25
  # @param [Proc] block
27
- def run(
28
- if: true,
29
- close_if: false,
30
- close_if_reach_limit: nil,
31
- limit_count: nil,
32
- &block
33
- )
34
- Core.new.run(
26
+ def run(key: nil, if: nil, close_if: nil, close_if_reach_limit: nil, limit_count: nil, initially_closed: nil, &block)
27
+ arguments = {
28
+ key: key,
35
29
  if: binding.local_variable_get(:if),
36
30
  close_if: close_if,
37
31
  close_if_reach_limit: close_if_reach_limit,
38
32
  limit_count: limit_count,
39
- &block
40
- )
33
+ initially_closed: initially_closed,
34
+ }.select { |_, v| v }
35
+ Builder.new.run(**arguments, &block)
41
36
  end
42
37
 
38
+ # @param key [String] Named key to find switch instead of caller
43
39
  # @param if [Boolean, Proc] Report when `if` is truthy (default: true)
44
40
  # @param stop_report_if [Boolean, Proc] Report when `close_if` is falsy (default: false)
45
41
  # @param stop_report_if_reach_limit [Boolean] Stop reporting when reported count reaches limit (default: true)
46
42
  # @param limit_count [Integer] Limit count. Use `report_limit_count` default value if it's nil
47
43
  # Can't be set 0 when `stop_report_if_reach_limit` is true (default: nil)
48
- def report(
49
- if: true,
50
- stop_report_if: false,
51
- stop_report_if_reach_limit: true,
52
- limit_count: nil
53
- )
44
+ def report(key: nil, if: nil, stop_report_if: nil, stop_report_if_reach_limit: nil, limit_count: nil)
54
45
  if block_given?
55
46
  raise ArgumentError.new('CircuitSwitch.report doesn\'t receive block. Use CircuitSwitch.run if you want to pass block.')
56
47
  end
57
48
 
58
- Core.new.report(
49
+ arguments = {
50
+ key: key,
59
51
  if: binding.local_variable_get(:if),
60
52
  stop_report_if: stop_report_if,
61
53
  stop_report_if_reach_limit: stop_report_if_reach_limit,
62
54
  limit_count: limit_count
63
- )
55
+ }.select { |_, v| v }
56
+ Builder.new.report(**arguments)
64
57
  end
65
58
 
66
59
  # Syntax sugar for `CircuitSwitch.run`
60
+ # @param key [String] Named key to find switch instead of caller
67
61
  # @param if [Boolean, Proc] `CircuitSwitch.run` is runnable when `if` is truthy (default: true)
68
62
  # @param close_if [Boolean, Proc] `CircuitSwitch.run` is runnable when `close_if` is falsy (default: false)
69
63
  # @param close_if_reach_limit [Boolean] `CircuitSwitch.run` is NOT runnable when run count reaches limit (default: true)
70
64
  # @param limit_count [Integer] Limit count. Use `run_limit_count` default value if it's nil. Can't be set 0 (default: nil)
65
+ # @param initially_closed [Boolean] Create switch with terminated mode (default: false)
71
66
  # @return [Boolean]
72
- def open?(
73
- if: true,
74
- close_if: false,
75
- close_if_reach_limit: true,
76
- limit_count: nil
77
- )
67
+ def open?(key: nil, if: nil, close_if: nil, close_if_reach_limit: nil, limit_count: nil, initially_closed: nil)
78
68
  if block_given?
79
69
  raise ArgumentError.new('CircuitSwitch.open doesn\'t receive block. Use CircuitSwitch.run if you want to pass block.')
80
70
  end
81
71
 
82
- Core.new.run(
72
+ arguments = {
73
+ key: key,
83
74
  if: binding.local_variable_get(:if),
84
75
  close_if: close_if,
85
76
  close_if_reach_limit: close_if_reach_limit,
86
- limit_count: limit_count
87
- ) {}.run?
77
+ limit_count: limit_count,
78
+ initially_closed: initially_closed,
79
+ }.select { |_, v| v }
80
+ Builder.new.run(**arguments) {}.run?
88
81
  end
89
82
  end
90
83
  end
@@ -4,9 +4,16 @@ module CircuitSwitch
4
4
  class MigrationGenerator < ActiveRecord::Generators::Base
5
5
  desc 'Create a migration to manage circuit switches state'
6
6
  source_root File.expand_path('templates', __dir__)
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.',
9
+ banner: 'create or add_key'
7
10
 
8
11
  def generate_migration
9
- migration_template 'migration.rb.erb', 'db/migrate/create_circuit_switches.rb', migration_version: migration_version
12
+ if migration_type == ['add_key']
13
+ migration_template 'add_key.rb.erb', 'db/migrate/add_key_to_circuit_switches.rb', migration_version: migration_version
14
+ else
15
+ migration_template 'migration.rb.erb', 'db/migrate/create_circuit_switches.rb', migration_version: migration_version
16
+ end
10
17
  end
11
18
 
12
19
  def migration_version
@@ -0,0 +1,13 @@
1
+ class AddKeyToCircuitSwitches < ActiveRecord::Migration<%= migration_version %>
2
+ def up
3
+ add_column :circuit_switches, :key, :string, after: :id
4
+ CircuitSwitch::CircuitSwitch.all.each { |switch| switch.update_column(:key, switch.caller) }
5
+ change_column_null :circuit_switches, :key, false
6
+ add_index :circuit_switches, :key
7
+ end
8
+
9
+ def down
10
+ remove_index :circuit_switches, :key
11
+ remove_column :circuit_switches, :key
12
+ end
13
+ end
@@ -29,7 +29,7 @@ CircuitSwitch.configure do |config|
29
29
  # You may be want backtrace when report to plain feed; e.g. Slack or email.
30
30
  # config.with_backtrace = false
31
31
 
32
- # Allowd backtrace paths to report
32
+ # Allowed backtrace paths to report
33
33
  # Specify with `with_backtrace` option.
34
34
  # Allowed all paths when set `[]`.
35
35
  # config.allowed_backtrace_paths = [Dir.pwd]
@@ -1,6 +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
5
  t.string :caller, null: false
5
6
  t.integer :run_count, default: 0, null: false
6
7
  t.integer :run_limit_count, default: 10, null: false
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.0
4
+ version: 0.2.1
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-13 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -114,6 +114,7 @@ files:
114
114
  - bin/setup
115
115
  - circuit_switch.gemspec
116
116
  - lib/circuit_switch.rb
117
+ - lib/circuit_switch/builder.rb
117
118
  - lib/circuit_switch/configuration.rb
118
119
  - lib/circuit_switch/core.rb
119
120
  - lib/circuit_switch/notification.rb
@@ -128,6 +129,7 @@ files:
128
129
  - lib/circuit_switch/workers/run_count_updater.rb
129
130
  - lib/generators/circuit_switch/install_generator.rb
130
131
  - lib/generators/circuit_switch/migration_generator.rb
132
+ - lib/generators/circuit_switch/templates/add_key.rb.erb
131
133
  - lib/generators/circuit_switch/templates/initializer.rb
132
134
  - lib/generators/circuit_switch/templates/migration.rb.erb
133
135
  homepage: https://github.com/makicamel/circuit_switch