active_job_tracker 0.2.0 → 0.3.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
  SHA256:
3
- metadata.gz: 3fa0e770ace4f14af8adb0907741243fd3c73e5da3be0ce81c9759ceab1ab06e
4
- data.tar.gz: 42121c16e596aabd423ed4899997cdb909e1c61620cbe231a80e1f3674e3059e
3
+ metadata.gz: 2b8342cf50be46737b9d191b7d631008c6cecad77fc81d1e0fcfca23c4ff6007
4
+ data.tar.gz: 02a1d61e46c41f6241b8be9d25ba311c42f882b1db71e94afc7d19062bbe3e31
5
5
  SHA512:
6
- metadata.gz: 0a9d3c246ade707d9504a7ca986ee17e0c5db4e4dbd42bd6de360983f631523d589eecc2dd8f5a382e247d0ce2112dc108131e053907f0bf0198b496c7f2fc02
7
- data.tar.gz: b844a3cd0174ea768cd07f67e4f8b1e5ac0e4565ef9147335147f90d93df576ad9f2dc4aa2b55287ed1cfb6fe9364e80c2b4c2a99f73968c5686addea38cd77b
6
+ metadata.gz: 1b46f5815fd0809154e222e50e388f6aa49bde11cb904d12bb8ff12fe9ecf1ccf2f41b0e250620b09accf18f0755e3e275b997380ee9284d9b1e290f6b2ead6b
7
+ data.tar.gz: f60e1e47250e60af0693f3a9f81cfcc8777289b2640e1a0b8f96b2b29bd3d61f9e42624148169fc2ba3b32ef186ee2e9c3c75f43784368020c36444b35d1f0e9
data/README.md CHANGED
@@ -63,6 +63,9 @@ ActiveJobTracker.configure do |config|
63
63
  # When true, job updates are automatically broadcast via ActionCable
64
64
  config.auto_broadcast = true
65
65
 
66
+ # Whether to raise an error when progress increments the current value beyond the target value (default: false)
67
+ config.raise_error_when_target_exceeded = false
68
+
66
69
  # Default partial path for rendering job trackers
67
70
  # (default: 'active_job_tracker/active_job_tracker')
68
71
  config.default_partial = 'active_job_tracker/active_job_tracker'
@@ -130,13 +133,27 @@ class ProcessImportJob < ApplicationJob
130
133
  records.each do |record|
131
134
  # Process item
132
135
 
133
- # Update progress (increments by 1)
136
+ # Update progress (increments by 1 by default)
134
137
  active_job_tracker_progress
135
138
  end
136
139
  end
137
140
  end
138
141
  ```
139
142
 
143
+ Optionally, you can use a custom value to increment your progress by:
144
+
145
+ ```ruby
146
+ def perform
147
+ # Default target is 100
148
+
149
+ 10.times do
150
+ # Process item
151
+
152
+ active_job_tracker_progress(increment_by: 10)
153
+ end
154
+ end
155
+ ```
156
+
140
157
  For more efficient progress tracking with many updates, use threadsafe progress caching:
141
158
 
142
159
  ```ruby
@@ -269,7 +286,7 @@ end
269
286
 
270
287
  ## Development
271
288
 
272
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
289
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rails test` to run the tests.
273
290
 
274
291
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
275
292
 
@@ -43,14 +43,15 @@ class ActiveJobTrackerRecord < ApplicationRecord
43
43
  [ current.to_f / target.to_f, 1.0 ].min
44
44
  end
45
45
 
46
- def progress(use_cache = true)
46
+ def progress(use_cache: true, increment_by: 1)
47
47
  if use_cache
48
48
  key = progress_cache_key
49
49
  should_flush = false
50
50
 
51
51
  @@mutex.synchronize do
52
52
  current_value = Rails.cache.fetch(key, expires_in: 1.week) { 0 }.to_i
53
- new_value = current_value + 1
53
+ new_value = incremented_value(current_value: current_value, increment_by: increment_by)
54
+
54
55
  Rails.cache.write(key, new_value, expires_in: 1.week)
55
56
 
56
57
  should_flush = new_value >= self.cache_threshold
@@ -60,7 +61,7 @@ class ActiveJobTrackerRecord < ApplicationRecord
60
61
  flush_progress_cache if should_flush
61
62
  else
62
63
  with_lock do
63
- self.current += 1
64
+ self.current = incremented_value(current_value: current, increment_by: increment_by)
64
65
  save!
65
66
  end
66
67
  end
@@ -86,6 +87,22 @@ class ActiveJobTrackerRecord < ApplicationRecord
86
87
 
87
88
  private
88
89
 
90
+ def incremented_value(current_value:, increment_by:)
91
+ new_value = current_value + increment_by
92
+
93
+ if new_value > target
94
+ if ActiveJobTracker.configuration.raise_error_when_target_exceeded
95
+ raise ActiveJobTracker::Error::TargetExceeded.new(
96
+ "The current value of #{new_value} exceeds the target value of #{target}"
97
+ )
98
+ end
99
+
100
+ target
101
+ else
102
+ new_value
103
+ end
104
+ end
105
+
89
106
  def broadcast_changes
90
107
  ActiveJobTracker.configuration.turbo_stream_channel.constantize.broadcast_replace_to(
91
108
  "active_job_trackers",
@@ -19,6 +19,10 @@ module ActiveJobTracker
19
19
  # @return [String]
20
20
  attr_accessor :default_partial
21
21
 
22
+ # Whether to raise an error when progress increments the current value beyond the target value
23
+ # @return [Boolean]
24
+ attr_accessor :raise_error_when_target_exceeded
25
+
22
26
  # Whether to include the style in the job tracker
23
27
  # @return [Boolean]
24
28
  attr_accessor :include_style
@@ -32,6 +36,7 @@ module ActiveJobTracker
32
36
  @default_target = 100
33
37
  @cache_threshold = 10
34
38
  @auto_broadcast = true
39
+ @raise_error_when_target_exceeded = false
35
40
  @default_partial = "active_job_tracker/active_job_tracker"
36
41
  @include_style = true
37
42
  @turbo_stream_channel = "Turbo::StreamsChannel"
@@ -1,3 +1,3 @@
1
1
  module ActiveJobTracker
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,6 +6,7 @@ module ActiveJobTracker
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  class Error < StandardError
9
+ class TargetExceeded < Error; end
9
10
  end
10
11
 
11
12
  def self.configuration
@@ -35,8 +36,8 @@ module ActiveJobTracker
35
36
  active_job_tracker.update(target: target)
36
37
  end
37
38
 
38
- def active_job_tracker_progress(cache: false)
39
- active_job_tracker.progress(cache)
39
+ def active_job_tracker_progress(cache: false, increment_by: 1)
40
+ active_job_tracker.progress(use_cache: cache, increment_by: increment_by)
40
41
  end
41
42
 
42
43
  def active_job_tracker
@@ -15,6 +15,9 @@ ActiveJobTracker.configure do |config|
15
15
  # When true, job updates are automatically broadcast via ActionCable
16
16
  config.auto_broadcast = true
17
17
 
18
+ # Whether to raise an error when progress increments the current value beyond the target value
19
+ config.raise_error_when_target_exceeded = false
20
+
18
21
  # Default partial path for rendering job trackers
19
22
  # (default: 'active_job_tracker/shared/active_job_tracker')
20
23
  config.default_partial = "active_job_tracker/active_job_tracker"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_job_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seena Sabti
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-09 00:00:00.000000000 Z
10
+ date: 2025-03-10 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: 8.0.1
26
+ - !ruby/object:Gem::Dependency
27
+ name: turbo-rails
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.11
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.0.11
26
40
  - !ruby/object:Gem::Dependency
27
41
  name: mocha
28
42
  requirement: !ruby/object:Gem::Requirement