circuit_switch 0.3.0 → 0.5.1

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: 6a00a04ad121a8f40a84f78a55e769aaa8f3fc185ac58fab51ddff11a0678e28
4
- data.tar.gz: '06189169b6959b97e8a33617f2d9e054f9acf3d55bce2833b39a5f6567a061c6'
3
+ metadata.gz: 49d14d6e01b518ab369100ad1158bceaef24169f1f42f06203e2e9770156295f
4
+ data.tar.gz: 93871c6a146d7d010bfab916bc4f0b6b2374b85cb542131d1a25e2990a2ec5ad
5
5
  SHA512:
6
- metadata.gz: b63203a04cb3a37e4c6ac265eb8283fdde5b5cb5990c4234bb57876d72c5c26e48ade13907373046dc9f4b8f943f8c6a34e8c6957bceae9ba3857ca087c9c033
7
- data.tar.gz: ff59240799e6841334eaffcaa76706cd78268804f8dc263da2c57dfd2bdc480264768ba614fb4102186fa483750ff36eb7c2c0bcc87eb6836f70c0025bcd0361
6
+ metadata.gz: 9b19fa18b5d5e41ba88bb2642abd7fea451b95e52439059ebe6392623a258d120c3649b4cb8e0a688c59973382f6ec98a07035cc61bcac5abfe4ed988cbc7e12
7
+ data.tar.gz: 20de1194f649ce2a752d425c09182d9a45a707e044bb272db96ad17e65228e84760447de861d244ed1c159fc22dd01ea2845aec894e2164a91db4107e45a77ac
data/CHANGELOG.md CHANGED
@@ -1,3 +1,39 @@
1
+ ## 0.5.1
2
+
3
+ * Remove warning related `CircuitSwitch.open?` without `close_if_reach_limit` option.
4
+
5
+ ## 0.5.0
6
+
7
+ ### New features
8
+
9
+ * GUI has been released!
10
+ If you are on Rails, add the following to your `config/routes.rb` and access `/circuit_switch`.
11
+
12
+ ```ruby
13
+ Rails.application.routes.draw do
14
+ mount CircuitSwitch::Engine => 'circuit_switch'
15
+ end
16
+ ```
17
+
18
+ ## 0.4.1
19
+
20
+ * Fix bug `if` `stop_report_if_reach_limit` options don't receive `false`.
21
+ * Fix to report error with stacktrace.
22
+ * Fix not to update run count when run isn't executable.
23
+
24
+ ## 0.4.0
25
+
26
+ ### Breaking Changes
27
+
28
+ * Be able to choice to notify `CircuitSwitch::CalledNotification` or `String`.
29
+ Improve `config/initializers/circuit_switch.rb` like following.
30
+
31
+ ```diff
32
+ CircuitSwitch.configure do |config|
33
+ - config.reporter = ->(message) { Bugsnag.notify(message) }
34
+ + config.reporter = ->(message, error) { Bugsnag.notify(error) }
35
+ ```
36
+
1
37
  ## 0.3.0
2
38
 
3
39
  ### Breaking Changes
data/README.md CHANGED
@@ -56,24 +56,30 @@ CircuitSwitch.run do
56
56
  end
57
57
  ```
58
58
 
59
- `run` calls received proc, and when conditions are met, closes it's circuit.
60
- To switch circuit opening and closing, some conditions can be set. By default, the circuit is always opened.
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
- `run` receives optional arguments.
63
-
64
- - `key`: [String] Named key to find switch instead of caller
65
- If no key passed, use caller.
66
- - `if`: [Boolean, Proc] Call proc when `if` is truthy (default: true)
67
- - `close_if`: [Boolean, Proc] Call proc when `close_if` is falsy (default: false)
68
- - `close_if_reach_limit`: [Boolean] Stop calling proc when run count reaches limit (default: false)
69
- - `limit_count`: [Integer] Limit count. Use `run_limit_count` default value if it's nil (default: nil)
70
- Can't be set 0 when `close_if_reach_limit` is true
71
- - `initially_closed`: [Boolean] Create switch with terminated mode (default: false)
72
-
73
- To close the circuit at specific date or when called 1000 times, code goes like:
59
+ `run` basically calls the received proc. But when a condition is met, it closes the circuit and does not evaluate the proc.
60
+ To switch circuit opening and closing, a set of options can be set. Without options, the circuit is always open.
61
+ You can set `close_if_reach_limit: true` so that the circuit is only open for 10 invocations. The constant 10 comes from the table definition we have arbitrarily chosen. In case you need a larger number, specify it in the `limit_count` option in combination with `close_if_reach_limit: true`, or alter default constraint on `circuit_switches.run_limit_count`.
62
+
63
+ - `key`: [String] The identifier to find record by. If `key` has not been passed, `circuit_switches.caller` is chosen as an alternative.
64
+ - `if`: [Boolean, Proc] Calls proc when the value of `if` is evaluated truthy (default: true)
65
+ - `close_if`: [Boolean, Proc] Calls proc when the value of `close_if` is evaluated falsy (default: false)
66
+ - `close_if_reach_limit`: [Boolean] Stops calling proc when `circuit_switches.run_count` has reached `circuit_switches.run_limit_count` (default: false)
67
+ - `limit_count`: [Integer] Mutates `circuit_switches.run_limit_count` whose value defined in schema is 10 by default. (default: nil)
68
+ Can't be set to 0 when `close_if_reach_limit` is true. This option is only relevant when `close_if_reach_limit` is set to true.
69
+ - `initially_closed`: [Boolean] Creates switch with terminated mode (default: false)
70
+
71
+ To close the circuit at a specific date, code goes like:
72
+
73
+ ```ruby
74
+ CircuitSwitch.run(close_if: -> { Date.today >= some_day }) do
75
+ # testing codes
76
+ end
77
+ ```
78
+
79
+ Or when the code of concern has been called 1000 times:
74
80
 
75
81
  ```ruby
76
- CircuitSwitch.run(close_if: -> { Date.today >= some_day }, limit_count: 1_000) do
82
+ CircuitSwitch.run(close_if_reach_limit: true, limit_count: 1_000) do
77
83
  # testing codes
78
84
  end
79
85
  ```
@@ -105,19 +111,17 @@ When you just want to report, set your `reporter` to initializer and then call `
105
111
  CircuitSwitch.report(if: some_condition)
106
112
  ```
107
113
 
108
- `report` just reports the line of code is called. It doesn't receive proc. It's useful for refactoring or removing dead codes.
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.
110
- `report` receives optional arguments.
114
+ `report` just reports which line of code is called. It doesn't receive proc. It's useful for refactoring or removing dead codes.
115
+ Same as `run`, a set of options can be set. By default, this method does not send reports more than 10 times. The constant 10 comes from the table definition we have arbitrarily chosen. In case you need a larger number, specify it in the `limit_count` option, or alter default constraint on `circuit_switches.report_limit_count`.
111
116
 
112
- - `key`: [String] Named key to find switch instead of caller
113
- If no key passed, use caller.
114
- - `if`: [Boolean, Proc] Report when `if` is truthy (default: true)
115
- - `stop_report_if`: [Boolean, Proc] Report when `close_if` is falsy (default: false)
116
- - `stop_report_if_reach_limit`: [Boolean] Stop reporting when reported count reaches limit (default: true)
117
- - `limit_count`: [Integer] Limit count. Use `report_limit_count` default value if it's nil (default: nil)
118
- Can't be set 0 when `stop_report_if_reach_limit` is true
117
+ - `key`: [String] The identifier to find record by. If `key` has not been passed, `circuit_switches.caller` is chosen as an alternative.
118
+ - `if`: [Boolean, Proc] Reports when the value of `if` is evaluated truthy (default: true)
119
+ - `stop_report_if`: [Boolean, Proc] Reports when the value of `stop_report_if` is evaluated falsy (default: false)
120
+ - `stop_report_if_reach_limit`: [Boolean] Stops reporting when `circuit_switches.report_count` has reached `circuit_switches.report_limit_count` (default: true)
121
+ - `limit_count`: [Integer] Mutates `circuit_switches.report_limit_count` whose value defined in schema is 10 by default. (default: nil)
122
+ Can't be set to 0 when `stop_report_if_reach_limit` is true.
119
123
 
120
- To know about report is executed or not, you can get through `report?`.
124
+ To know if `report` has already been executed or not, you can get through `reported?`.
121
125
  Of course you can chain `report` and `run` or `open?`.
122
126
 
123
127
  #### `with_backtrace`
@@ -176,7 +180,27 @@ By default, due_date is 10 days after today. To modify, set `due_date` to initia
176
180
 
177
181
  ## GUI to manage switches
178
182
 
179
- Under development :)
183
+ ![GUI](circuit_switch.png)
184
+
185
+ GUI is now only for Rails.
186
+ Add the following to your `config/routes.rb` and access `/circuit_switch`.
187
+
188
+ ```ruby
189
+ Rails.application.routes.draw do
190
+ mount CircuitSwitch::Engine => 'circuit_switch'
191
+ end
192
+ ```
193
+
194
+ ### Authentication
195
+
196
+ In production, you may need access protection.
197
+ With Devise, code goes like:
198
+
199
+ ```ruby
200
+ authenticate :user, lambda { |user| user.admin? } do
201
+ mount CircuitSwitch::Engine => 'circuit_switch'
202
+ end
203
+ ```
180
204
 
181
205
  ## Contributing
182
206
 
@@ -0,0 +1,5 @@
1
+ module CircuitSwitch
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require_dependency 'circuit_switch/application_controller'
2
+
3
+ module CircuitSwitch
4
+ class CircuitSwitchController < ::CircuitSwitch::ApplicationController
5
+ def index
6
+ @circuit_switches = ::CircuitSwitch::CircuitSwitch.all.order(order_by)
7
+ end
8
+
9
+ def edit
10
+ @circuit_switch = ::CircuitSwitch::CircuitSwitch.find(params[:id])
11
+ end
12
+
13
+ def update
14
+ @circuit_switch = ::CircuitSwitch::CircuitSwitch.find(params[:id])
15
+ if @circuit_switch.update circuit_switch_params
16
+ flash[:success] = "Switch for `#{@circuit_switch.key}` is successfully updated."
17
+ redirect_to circuit_switch_index_path
18
+ else
19
+ render :edit
20
+ end
21
+ end
22
+
23
+ def destroy
24
+ @circuit_switch = ::CircuitSwitch::CircuitSwitch.find(params[:id])
25
+ @circuit_switch.destroy!
26
+ flash[:success] = "Switch for `#{@circuit_switch.key}` is successfully destroyed."
27
+ redirect_to circuit_switch_index_path
28
+ end
29
+
30
+ private
31
+
32
+ def order_by
33
+ params[:order_by].in?(%w[id due_date]) ? params[:order_by] : 'id'
34
+ end
35
+
36
+ def circuit_switch_params
37
+ params.require(:circuit_switch).permit(:key, :caller, :run_count, :run_limit_count, :run_is_terminated, :report_count, :report_limit_count, :report_is_terminated, :due_date)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,129 @@
1
+ <h1 class="p-3 fw-light">Edit switch</h1>
2
+
3
+ <div class="container">
4
+ <% if @circuit_switch.errors.any? %>
5
+ <div class="alert alert-danger">
6
+ <ul class="mb-0">
7
+ <% @circuit_switch.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <%= form_with(model: @circuit_switch, local: true) do |form| %>
15
+ <div class="fw-light">
16
+ <div class="py-1 row">
17
+ <div class="col-10 mt-auto">
18
+ id: <%= @circuit_switch.id %>
19
+ <span class="px-4 text-muted">
20
+ created_at: <%= @circuit_switch.created_at %> /
21
+ updated_at: <%= @circuit_switch.updated_at %>
22
+ </span>
23
+ </div>
24
+ <div class="col-2 text-end">
25
+ <%= link_to 'Back', circuit_switch_index_path, class: 'btn btn-outline-dark' %>
26
+ </div>
27
+ </div>
28
+
29
+ <div class="py-1">
30
+ <div class="py-1">
31
+ <%= form.label :key, 'key:', class: 'form-label' %>
32
+ <%= form.text_area :key, class: 'form-control font-monospace' %>
33
+ </div>
34
+ <div class="py-1">
35
+ <%= form.label :caller, 'caller:', class: 'form-label' %>
36
+ <%= form.text_area :caller, class: 'form-control font-monospace' %>
37
+ </div>
38
+ </div>
39
+
40
+ <div class="py-1">
41
+ <div class="py-2 row">
42
+ <%= form.label :run_is_terminated, 'run_mode:', class: 'col-sm-2 col-form-label' %>
43
+ <div class="col-sm-2">
44
+ <%= form.select :run_is_terminated, { open: false, closed: true }, { selected: @circuit_switch.run_is_terminated }, { class: ['form-select', (@circuit_switch.run_is_terminated ? 'alert-dark' : 'alert-primary')] } %>
45
+ </div>
46
+ </div>
47
+ <div class="py-2 row">
48
+ <div class="col-sm-2">
49
+ <%= form.label :run_count, 'run_count:', class: 'col-form-label' %>
50
+ <i class="fas fa-info-circle text-black-50 align-baseline px-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Increment only when run_mode is open"></i>
51
+ </div>
52
+ <div class="col-sm-2">
53
+ <%= form.text_field :run_count, class: 'form-control' %>
54
+ </div>
55
+ </div>
56
+ <div class="py-2 row">
57
+ <div class="col-sm-2">
58
+ <%= form.label :run_limit_count, 'run_limit_count:', class: 'col-form-label' %>
59
+ <i class="fas fa-info-circle text-black-50 align-baseline px-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Reference only when call with `close_if_reach_limit` true"></i>
60
+ </div>
61
+ <div class="col-sm-2">
62
+ <%= form.text_field :run_limit_count, class: 'form-control' %>
63
+ </div>
64
+ </div>
65
+ <div class="py-2 row">
66
+ <%= form.label :report_is_terminated, 'report_mode:', class: 'col-sm-2 col-sm-2 col-form-label' %>
67
+ <div class="col-sm-2">
68
+ <%= form.select :report_is_terminated, { reporting: false, terminated: true }, { selected: @circuit_switch.report_is_terminated }, { class: ['form-select', (@circuit_switch.report_is_terminated ? 'alert-dark' : 'alert-primary')] } %>
69
+ </div>
70
+ </div>
71
+ <div class="py-2 row">
72
+ <div class="col-sm-2">
73
+ <%= form.label :report_count, 'report_count:', class: 'col-form-label' %>
74
+ <i class="fas fa-info-circle text-black-50 align-baseline px-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Increment only when report_mode is reporting"></i>
75
+ </div>
76
+ <div class="col-sm-2">
77
+ <%= form.text_field :report_count, class: 'form-control' %>
78
+ </div>
79
+ </div>
80
+ <div class="py-2 row">
81
+ <div class="col-sm-2">
82
+ <%= form.label :report_limit_count, 'report_limit_count:', class: 'col-form-label' %>
83
+ <i class="fas fa-info-circle text-black-50 align-baseline px-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Reference only when call with `stop_report_if_reach_limit` true"></i>
84
+ </div>
85
+ <div class="col-sm-2">
86
+ <%= form.text_field :report_limit_count, class: 'form-control' %>
87
+ </div>
88
+ </div>
89
+ </div>
90
+
91
+ <div class="py-1">
92
+ <div class="py-2 row">
93
+ <div class="col-sm-2">
94
+ <%= form.label :due_date, 'due_date:', class: 'col-form-label' %>
95
+ <i class="fas fa-info-circle text-black-50 align-baseline px-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Date to remove switch"></i>
96
+ </div>
97
+ <div class="col-sm-2">
98
+ <%= form.text_field :due_date, class: 'form-control' %>
99
+ </div>
100
+ </div>
101
+ </div>
102
+
103
+ <div class="py-2">
104
+ <%= form.submit 'update', class: 'btn btn-dark' %>
105
+ </div>
106
+ </div>
107
+ <% end %>
108
+
109
+ <%= button_to 'delete', circuit_switch_path(@circuit_switch), method: :delete, class: 'btn btn-danger btn-destroy' %>
110
+ </div>
111
+
112
+ <script>
113
+ $('select').on('change', function() {
114
+ style = $(this).val() == 'true' ? 'alert-dark' : 'alert-primary'
115
+ $(this).removeClass('alert-primary alert-dark')
116
+ $(this).addClass(style)
117
+ })
118
+
119
+ $('.btn-destroy').on('click', function (event) {
120
+ if (!window.confirm('Are you sure to destroy?')) {
121
+ event.preventDefault()
122
+ }
123
+ })
124
+
125
+ tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
126
+ tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
127
+ return new bootstrap.Tooltip(tooltipTriggerEl)
128
+ })
129
+ </script>
@@ -0,0 +1,83 @@
1
+ <h1 class="p-3 fw-light">
2
+ circuit_switch <i class="fas fa-xs fa-toggle-on text-warning"></i>
3
+ </h1>
4
+
5
+ <div class="container">
6
+ <% if flash[:success].present? %>
7
+ <div class="alert alert-info"><%= flash[:success] %></div>
8
+ <% end %>
9
+
10
+ <div class="dropdown py-2 text-end">
11
+ <a class="btn btn-sm btn-outline-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
12
+ <%= params[:order_by] == 'due_date' ? 'due date ordered' : 'id ordered' %>
13
+ </a>
14
+
15
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
16
+ <li><%= link_to 'id ordered', circuit_switch_index_path(order_by: :id), class: 'dropdown-item' %></li>
17
+ <li><%= link_to 'due date ordered', circuit_switch_index_path(order_by: :due_date), class: 'dropdown-item' %></li>
18
+ </ul>
19
+ </div>
20
+
21
+ <table class="table table-striped table-hover striped-primary table-bordered border fw-light">
22
+ <thead class="table-dark border border-dark">
23
+ <tr>
24
+ <th scope="col" class="px-3 py-4 fw-light text-center">id</th>
25
+ <th scope="col" class="px-3 py-4 fw-light">key</th>
26
+ <th scope="col" class="px-3 py-4 fw-light">caller</th>
27
+ <th scope="col" class="px-1 py-4 fw-light">due_date</th>
28
+ <th scope="col" class="px-3 py-4 fw-light text-center">run</th>
29
+ <th scope="col" class="px-3 py-4 fw-light text-center">report</th>
30
+ <th scope="col" class="px-3 py-4 fw-light text-center">show/edit</th>
31
+ <th scope="col" class="px-3 py-4 fw-light text-center">destroy</th>
32
+ </tr>
33
+ </thead>
34
+
35
+ <tbody>
36
+ <% @circuit_switches.each do |circuit_switch| %>
37
+ <tr>
38
+ <td class="text-center align-middle"><%= circuit_switch.id %></td>
39
+ <td class="text-break small font-monospace"><%= circuit_switch.key %></td>
40
+ <td class="text-break small font-monospace"><%= circuit_switch.caller %></td>
41
+ <td class="text-nowrap"><%= circuit_switch.due_date %></td>
42
+ <td class="text-center align-middle">
43
+ <% if circuit_switch.run_is_terminated %>
44
+ <i class="fas fa-lg fa-ban text-muted" data-bs-toggle="tooltip" data-bs-placement="top" title="closed mode"></i>
45
+ <% else %>
46
+ <i class="fas fa-lg fa-walking " data-bs-toggle="tooltip" data-bs-placement="top" title="opened mode"></i>
47
+ <% end %>
48
+ </td>
49
+ <td class="text-center align-middle">
50
+ <% if circuit_switch.report_is_terminated %>
51
+ <i class="fas fa-lg fa-ban text-muted" data-bs-toggle="tooltip" data-bs-placement="top" title="terminated mode"></i>
52
+ <% else %>
53
+ <i class="fas fa-lg fa-bullhorn " data-bs-toggle="tooltip" data-bs-placement="top" title="reporting mode"></i>
54
+ <% end %>
55
+ </td>
56
+ <td class="text-center align-middle py-3">
57
+ <%= button_to edit_circuit_switch_path(circuit_switch), method: :get, class: 'btn btn-outline-dark' do %>
58
+ <i class="fas fa-edit"></i>
59
+ <% end %>
60
+ </td>
61
+ <td class="text-center align-middle py-3">
62
+ <%= button_to circuit_switch_path(circuit_switch), method: :delete, class: 'btn btn-danger btn-destroy' do %>
63
+ <i class="fas fa-broom"></i>
64
+ <% end %>
65
+ </td>
66
+ </tr>
67
+ <% end %>
68
+ </tbody>
69
+ </table>
70
+ </div>
71
+
72
+ <script>
73
+ $('.btn-destroy').on('click', function (event) {
74
+ if (!window.confirm('Are you sure to destroy?')) {
75
+ event.preventDefault()
76
+ }
77
+ })
78
+
79
+ tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
80
+ tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
81
+ return new bootstrap.Tooltip(tooltipTriggerEl)
82
+ })
83
+ </script>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <title>circuit_switch</title>
6
+ <%= csrf_meta_tags %>
7
+ <%= csp_meta_tag %>
8
+ <%= javascript_include_tag 'https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js', integrity: 'sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW', crossorigin: 'anonymous' %>
9
+ <%= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js' %>
10
+ <%= javascript_include_tag 'https://kit.fontawesome.com/3e20336786.js', crossorigin: 'anonymous' %>
11
+ <%= stylesheet_link_tag 'https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css', media: 'all', integrity: 'sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1', crossorigin: 'anonymous' %>
12
+ </head>
13
+ <body>
14
+ <%= yield %>
15
+ </body>
16
+ </html>
Binary file
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ CircuitSwitch::Engine.routes.draw do
2
+ resources 'circuit_switch', only: [:index, :edit, :update, :destroy], controller: 'circuit_switch'
3
+ root to: 'circuit_switch#index'
4
+ end
@@ -12,7 +12,7 @@ module CircuitSwitch
12
12
  key: nil,
13
13
  if: true,
14
14
  close_if: false,
15
- close_if_reach_limit: nil,
15
+ close_if_reach_limit: false,
16
16
  limit_count: nil,
17
17
  initially_closed: false
18
18
  )
@@ -46,7 +46,7 @@ module CircuitSwitch
46
46
  close_if_reach_limit: close_if_reach_limit,
47
47
  limit_count: limit_count,
48
48
  initially_closed: initially_closed,
49
- }.select { |_, v| v }
49
+ }.reject { |_, v| v.nil? }
50
50
  assign_runner(**arguments)
51
51
  execute_run(&block)
52
52
  end
@@ -58,7 +58,7 @@ module CircuitSwitch
58
58
  stop_report_if: stop_report_if,
59
59
  stop_report_if_reach_limit: stop_report_if_reach_limit,
60
60
  limit_count: limit_count
61
- }.select { |_, v| v }
61
+ }.reject { |_, v| v.nil? }
62
62
  assign_reporter(**arguments)
63
63
  execute_report
64
64
  end
@@ -9,37 +9,40 @@ module CircuitSwitch
9
9
  :report_if, :stop_report_if, :stop_report_if_reach_limit, :report_limit_count
10
10
 
11
11
  def execute_run(&block)
12
+ run_executable = false
12
13
  if close_if_reach_limit && run_limit_count == 0
13
14
  raise CircuitSwitchError.new('Can\'t set limit_count to 0 when close_if_reach_limit is true')
14
15
  end
15
- if close_if_reach_limit.nil?
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
- end
19
16
 
20
17
  return self if evaluate(close_if) || !evaluate(run_if)
21
18
  return self if close_if_reach_limit && switch.reached_run_limit?(run_limit_count)
22
19
  return self if switch.run_is_terminated?
23
20
 
21
+ run_executable = true
24
22
  unless switch.new_record? && initially_closed
25
23
  yield
26
24
  @run = true
27
25
  end
28
26
  self
29
27
  ensure
30
- RunCountUpdater.perform_later(
31
- key: key,
32
- limit_count: run_limit_count,
33
- called_path: called_path,
34
- reported: reported?,
35
- initially_closed: initially_closed
36
- )
28
+ if run_executable
29
+ RunCountUpdater.perform_later(
30
+ key: key,
31
+ limit_count: run_limit_count,
32
+ called_path: called_path,
33
+ reported: reported?,
34
+ initially_closed: initially_closed
35
+ )
36
+ end
37
37
  end
38
38
 
39
39
  def execute_report
40
40
  if config.reporter.nil?
41
41
  raise CircuitSwitchError.new('Set config.reporter.')
42
42
  end
43
+ if config.reporter.arity == 1
44
+ Logger.new($stdout).info('config.reporter now receives 2 arguments. Improve your `config/initialzers/circuit_switch.rb`.')
45
+ end
43
46
  if stop_report_if_reach_limit && report_limit_count == 0
44
47
  raise CircuitSwitchError.new('Can\'t set limit_count to 0 when stop_report_if_reach_limit is true')
45
48
  end
@@ -52,6 +55,7 @@ module CircuitSwitch
52
55
  key: key,
53
56
  limit_count: report_limit_count,
54
57
  called_path: called_path,
58
+ stacktrace: StacktraceModifier.call(backtrace: caller),
55
59
  run: run?
56
60
  )
57
61
  @reported = true
@@ -0,0 +1,9 @@
1
+ module CircuitSwitch
2
+ def self.table_name_prefix
3
+ ''
4
+ end
5
+
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace ::CircuitSwitch
8
+ end
9
+ end
@@ -10,7 +10,7 @@ module CircuitSwitch
10
10
  class CalledNotification < CircuitSwitchNotification
11
11
  def to_message(called_path:)
12
12
  if ::CircuitSwitch.config.with_backtrace
13
- "#{message}\ncalled_path: #{called_path}\n#{StacktraceModifier.call(backtrace: backtrace)}"
13
+ "#{message}\ncalled_path: #{called_path}\n#{backtrace.join("\n")}"
14
14
  else
15
15
  message
16
16
  end
@@ -6,10 +6,14 @@ module CircuitSwitch
6
6
  delegate :config, to: ::CircuitSwitch
7
7
 
8
8
  def call(backtrace:)
9
- backtrace
10
- .select { |path| /(#{config.allowed_backtrace_paths.join('|')})/.match?(path) }
11
- .map { |path| path.sub(/(#{config.strip_paths.join('|')})/, '') }
12
- .join("\n")
9
+ if config.with_backtrace
10
+ backtrace
11
+ .select { |path| /(#{config.allowed_backtrace_paths.join('|')})/.match?(path) }
12
+ .map { |path| path.sub(/(#{config.strip_paths.join('|')})/, '') }
13
+ else
14
+ backtrace
15
+ .select { |path| /(#{config.allowed_backtrace_paths.join('|')})/.match?(path) }
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -1,3 +1,3 @@
1
1
  module CircuitSwitch
2
- VERSION = '0.3.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -5,7 +5,7 @@ module CircuitSwitch
5
5
  class Reporter < ::ActiveJob::Base
6
6
  delegate :config, to: ::CircuitSwitch
7
7
 
8
- def perform(key:, limit_count:, called_path:, run:)
8
+ def perform(key:, limit_count:, called_path:, stacktrace:, run:)
9
9
  # Wait for RunCountUpdater saves circuit_switch
10
10
  sleep(3) if run
11
11
 
@@ -27,7 +27,12 @@ module CircuitSwitch
27
27
  sleep(2)
28
28
  retry
29
29
  rescue CalledNotification => notification
30
- config.reporter.call(notification.to_message(called_path: called_path))
30
+ notification.set_backtrace(stacktrace)
31
+ if config.reporter.arity == 1
32
+ config.reporter.call(notification.to_message(called_path: called_path))
33
+ else
34
+ config.reporter.call(notification.to_message(called_path: called_path), notification)
35
+ end
31
36
  end
32
37
  end
33
38
  end
@@ -1,6 +1,7 @@
1
1
  require_relative 'circuit_switch/configuration'
2
2
  require_relative 'circuit_switch/builder'
3
3
  require_relative 'circuit_switch/orm/active_record/circuit_switch'
4
+ require_relative 'circuit_switch/engine' if defined?(Rails)
4
5
  require_relative 'circuit_switch/railtie' if defined?(Rails::Railtie)
5
6
  require_relative 'circuit_switch/version'
6
7
  require_relative 'circuit_switch/workers/due_date_notifier'
@@ -31,7 +32,7 @@ module CircuitSwitch
31
32
  close_if_reach_limit: close_if_reach_limit,
32
33
  limit_count: limit_count,
33
34
  initially_closed: initially_closed,
34
- }.select { |_, v| v }
35
+ }.reject { |_, v| v.nil? }
35
36
  Builder.new.run(**arguments, &block)
36
37
  end
37
38
 
@@ -52,7 +53,7 @@ module CircuitSwitch
52
53
  stop_report_if: stop_report_if,
53
54
  stop_report_if_reach_limit: stop_report_if_reach_limit,
54
55
  limit_count: limit_count
55
- }.select { |_, v| v }
56
+ }.reject { |_, v| v.nil? }
56
57
  Builder.new.report(**arguments)
57
58
  end
58
59
 
@@ -76,7 +77,7 @@ module CircuitSwitch
76
77
  close_if_reach_limit: close_if_reach_limit,
77
78
  limit_count: limit_count,
78
79
  initially_closed: initially_closed,
79
- }.select { |_, v| v }
80
+ }.reject { |_, v| v.nil? }
80
81
  Builder.new.run(**arguments) {}.run?
81
82
  end
82
83
  end
@@ -2,7 +2,8 @@
2
2
 
3
3
  CircuitSwitch.configure do |config|
4
4
  # Specify proc to call your report tool: like;
5
- # config.reporter = -> (message) { Bugsnag.notify(message) }
5
+ # config.reporter = -> (message, error) { Bugsnag.notify(error) }
6
+ # config.reporter = -> (message, error) { Sentry::Rails.capture_message(message) }
6
7
  config.reporter = nil
7
8
 
8
9
  # Condition to report
@@ -33,7 +34,6 @@ CircuitSwitch.configure do |config|
33
34
  # config.with_backtrace = false
34
35
 
35
36
  # Allowed backtrace paths to report
36
- # Specify with `with_backtrace` option.
37
37
  # Allowed all paths when set `[]`.
38
38
  # config.allowed_backtrace_paths = [Dir.pwd]
39
39
 
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.3.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - makicamel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-03 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -124,13 +124,21 @@ files:
124
124
  - LICENSE.txt
125
125
  - README.md
126
126
  - Rakefile
127
+ - app/controllers/circuit_switch/application_controller.rb
128
+ - app/controllers/circuit_switch/circuit_switch_controller.rb
129
+ - app/views/circuit_switch/circuit_switch/edit.html.erb
130
+ - app/views/circuit_switch/circuit_switch/index.html.erb
131
+ - app/views/layouts/circuit_switch/application.html.erb
127
132
  - bin/console
128
133
  - bin/setup
129
134
  - circuit_switch.gemspec
135
+ - circuit_switch.png
136
+ - config/routes.rb
130
137
  - lib/circuit_switch.rb
131
138
  - lib/circuit_switch/builder.rb
132
139
  - lib/circuit_switch/configuration.rb
133
140
  - lib/circuit_switch/core.rb
141
+ - lib/circuit_switch/engine.rb
134
142
  - lib/circuit_switch/notification.rb
135
143
  - lib/circuit_switch/orm/active_record/circuit_switch.rb
136
144
  - lib/circuit_switch/railtie.rb
@@ -154,7 +162,7 @@ metadata:
154
162
  homepage_uri: https://github.com/makicamel/circuit_switch
155
163
  source_code_uri: https://github.com/makicamel/circuit_switch
156
164
  changelog_uri: https://github.com/makicamel/circuit_switch/blob/main/CHANGELOG.md
157
- post_install_message:
165
+ post_install_message:
158
166
  rdoc_options: []
159
167
  require_paths:
160
168
  - lib
@@ -169,8 +177,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
177
  - !ruby/object:Gem::Version
170
178
  version: '0'
171
179
  requirements: []
172
- rubygems_version: 3.1.2
173
- signing_key:
180
+ rubygems_version: 3.2.32
181
+ signing_key:
174
182
  specification_version: 4
175
183
  summary: Circuit switch with report tools
176
184
  test_files: []