activejob-status 0.1.6 → 0.2.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: 6224b3f14c08054d81bc9fbe675a1f120bca7291d7ed20b644b9bcf833c34458
4
- data.tar.gz: 3691a1628e4eff782cfdd228ea2bc6eda0f16e970e4c8e8cde479612037d51d3
3
+ metadata.gz: 409f3ad04e793c5c448ab94bf9abf6adef5caf2b37c48635b1cee46fd638b773
4
+ data.tar.gz: 9c8add1a5bf84c5d4766ddf115e83d523a13181f84bffb1ee31bdfe90ff8a206
5
5
  SHA512:
6
- metadata.gz: 1d537c5d9794dcca9eb1171d400cee21958716e89989fd87db61600475929e565d026b10b49fe0778b4e3dbd80d11f907564daa7434c6b47652fe8c7bc573ffb
7
- data.tar.gz: 7957e2ee9d3deb2227e2dcef5e9d3945bf89f279afb7b70aa96c6c9838302535e297a0c92e2743b60ecaee5ce15e74695cd5e567d16865275f39d4a45b1f1b20
6
+ metadata.gz: c78e3c4ff9531cc4a66ba3ca43cad0ccb22b411f7335f5adc61ed71c9da870ebfd651ae1069f5bfccbe809f2ea5bec97b8a1ad3bc8e7b22703cdceae604cf014
7
+ data.tar.gz: d2fbff1487df100ba0033bbe8e6833fd8660ac77c320c90fe920543fe2e01dc3ee65e31ea770bb9b43c5508c748fa0dea50631d12a53c2da31fd8e6f4d34970a
@@ -14,36 +14,37 @@ module ActiveJob
14
14
 
15
15
  def total=(num)
16
16
  @total = num
17
- update
17
+ job_status.update(to_h, force: true)
18
+ self
18
19
  end
19
20
 
20
21
  def progress=(num)
21
- update { num }
22
+ @progress = num
23
+ job_status.update(to_h, force: true)
24
+ self
22
25
  end
23
26
 
24
27
  def increment(num = 1)
25
- update { @progress + num }
28
+ @progress += num
29
+ job_status.update(to_h)
30
+ self
26
31
  end
27
32
 
28
33
  def decrement(num = 1)
29
- update { @progress - num }
34
+ @progress -= num
35
+ job_status.update(to_h)
36
+ self
30
37
  end
31
38
 
32
39
  def finish
33
- update { @total }
40
+ @progress = @total
41
+ job_status.update(to_h, force: true)
42
+ self
34
43
  end
35
44
 
36
45
  def to_h
37
46
  { progress: @progress, total: @total }
38
47
  end
39
-
40
- private
41
-
42
- def update
43
- @progress = yield if block_given?
44
- job_status.update(to_h)
45
- self
46
- end
47
48
  end
48
49
  end
49
50
  end
@@ -4,29 +4,31 @@ module ActiveJob
4
4
  delegate :[], :to_s, :to_json, :inspect, to: :read
5
5
  delegate :queued?, :working?, :completed?, :failed?, to: :status_inquiry
6
6
 
7
- def initialize(job)
8
- @job = job
7
+ def initialize(job, options = {})
8
+ options = ActiveJob::Status.options.merge(options)
9
+ @storage = ActiveJob::Status::Storage.new(options)
10
+ @job = job
9
11
  end
10
12
 
11
13
  def []=(key, value)
12
- update(key => value)
14
+ update({ key => value }, force: true)
13
15
  end
14
16
 
15
17
  def read
16
- Storage.read(@job)
18
+ @storage.read(@job)
17
19
  end
18
20
  alias to_h read
19
21
 
20
- def update(message)
21
- Storage.update(@job, message)
22
+ def update(message, **options)
23
+ @storage.update(@job, message, **options)
22
24
  end
23
25
 
24
26
  def delete
25
- Storage.delete(@job)
27
+ @storage.delete(@job)
26
28
  end
27
29
 
28
30
  def job_id
29
- Storage.job_id(@job)
31
+ @storage.job_id(@job)
30
32
  end
31
33
 
32
34
  def status
@@ -1,39 +1,45 @@
1
1
  module ActiveJob
2
2
  module Status
3
- module Storage
4
- class << self
5
- def store
6
- ActiveJob::Status.store
7
- end
3
+ class Storage
4
+ def initialize(options = {})
5
+ options.assert_valid_keys(:expires_in, :throttle_interval)
8
6
 
9
- def options
10
- ActiveJob::Status.options
11
- end
7
+ @expires_in = options[:expires_in]
8
+ @throttle = ActiveJob::Status::Throttle.new(options[:throttle_interval])
9
+ end
12
10
 
13
- def job_id(job)
14
- job.is_a?(String) ? job : job.job_id
15
- end
11
+ def store
12
+ @store ||= ActiveJob::Status.store
13
+ end
16
14
 
17
- def key(job)
18
- "activejob:status:#{job_id(job)}"
19
- end
15
+ def job_id(job)
16
+ job.is_a?(String) ? job : job.job_id
17
+ end
20
18
 
21
- def read(job)
22
- store.read(key(job)) || {}
23
- end
19
+ def key(job)
20
+ "activejob:status:#{job_id(job)}"
21
+ end
24
22
 
25
- def write(job, message)
26
- store.write(key(job), message, expires_in: options[:expires_in])
27
- end
23
+ def read(job)
24
+ store.read(key(job)) || {}
25
+ end
28
26
 
29
- def update(job, message)
30
- write(job, read(job).merge(message))
27
+ def write(job, message, force: false)
28
+ @throttle.wrap(force: force) do
29
+ store.write(key(job), message, expires_in: @expires_in)
31
30
  end
31
+ end
32
32
 
33
- def delete(job)
34
- store.delete(key(job))
33
+ def update(job, message, force: false)
34
+ @throttle.wrap(force: force) do
35
+ message = read(job).merge(message)
36
+ store.write(key(job), message, expires_in: @expires_in)
35
37
  end
36
38
  end
39
+
40
+ def delete(job)
41
+ store.delete(key(job))
42
+ end
37
43
  end
38
44
  end
39
45
  end
@@ -0,0 +1,22 @@
1
+
2
+ module ActiveJob
3
+ module Status
4
+ class Throttle
5
+ def initialize(interval)
6
+ @interval = interval
7
+ @started_at = Time.current
8
+ end
9
+
10
+ def wrap(force: false)
11
+ return yield if force || @interval.nil? || @interval.zero?
12
+
13
+ now = Time.current
14
+ elasped = now - @started_at
15
+ return if @interval > elasped
16
+
17
+ yield
18
+ @started_at = now
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveJob
2
2
  module Status
3
- VERSION = '0.1.6'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -1,11 +1,17 @@
1
+ require 'active_support/core_ext/hash'
1
2
  require 'activejob-status/storage'
2
3
  require 'activejob-status/status'
3
4
  require 'activejob-status/progress'
5
+ require 'activejob-status/throttle'
4
6
 
5
7
  module ActiveJob
6
8
  module Status
7
9
  extend ActiveSupport::Concern
8
- DEFAULT_EXPIRY = 60 * 30
10
+
11
+ DEFAULT_OPTIONS = {
12
+ expires_in: 60 * 30,
13
+ throttle_interval: 0
14
+ }.freeze
9
15
 
10
16
  included do
11
17
  before_enqueue { |job| job.status.update(status: :queued) }
@@ -28,11 +34,12 @@ module ActiveJob
28
34
 
29
35
  class << self
30
36
  def options=(options)
31
- @@options = options
37
+ options.assert_valid_keys(*DEFAULT_OPTIONS.keys)
38
+ @@options = DEFAULT_OPTIONS.merge(options)
32
39
  end
33
40
 
34
41
  def options
35
- @@options ||= { expires_in: DEFAULT_EXPIRY }
42
+ @@options ||= DEFAULT_OPTIONS
36
43
  end
37
44
 
38
45
  def store=(store)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activejob-status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Savater Sebastien
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-14 00:00:00.000000000 Z
11
+ date: 2021-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.2'
41
- description:
41
+ description:
42
42
  email:
43
43
  - savater.sebastien@gmail.com
44
44
  executables: []
@@ -49,12 +49,13 @@ files:
49
49
  - lib/activejob-status/progress.rb
50
50
  - lib/activejob-status/status.rb
51
51
  - lib/activejob-status/storage.rb
52
+ - lib/activejob-status/throttle.rb
52
53
  - lib/activejob-status/version.rb
53
54
  homepage: https://github.com/inkstak/activejob-status
54
55
  licenses:
55
56
  - MIT
56
57
  metadata: {}
57
- post_install_message:
58
+ post_install_message:
58
59
  rdoc_options: []
59
60
  require_paths:
60
61
  - lib
@@ -69,8 +70,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0'
71
72
  requirements: []
72
- rubygems_version: 3.0.6
73
- signing_key:
73
+ rubygems_version: 3.1.4
74
+ signing_key:
74
75
  specification_version: 4
75
76
  summary: Monitor your jobs
76
77
  test_files: []