activejob-status 0.1.4 → 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: '069dbaec9c90cd38b7f3c776c8cb928c69e9e381da44a12859f90be51f2187c6'
4
- data.tar.gz: f75d3ba17eb3f9d56d1bc1abdbe343dc2f5b46e269bccc24c652e43dc6b0c618
3
+ metadata.gz: 409f3ad04e793c5c448ab94bf9abf6adef5caf2b37c48635b1cee46fd638b773
4
+ data.tar.gz: 9c8add1a5bf84c5d4766ddf115e83d523a13181f84bffb1ee31bdfe90ff8a206
5
5
  SHA512:
6
- metadata.gz: 6a59ed17b1979cb50b385397f1d316c87e5d4a6563a7ca7f441fff4f8e8411ddfddb835664df7f7f93ad2c558b9bc782a396e9beb6ae6a546b55a9f60746a6fe
7
- data.tar.gz: d3be935b39e3685d81adeba2a544496ca8ee506e4b469bd060546d4d7e7114080fb5bae8635039091889afc292da379ebe24f5ac3405201fecce512bfc62568b
6
+ metadata.gz: c78e3c4ff9531cc4a66ba3ca43cad0ccb22b411f7335f5adc61ed71c9da870ebfd651ae1069f5bfccbe809f2ea5bec97b8a1ad3bc8e7b22703cdceae604cf014
7
+ data.tar.gz: d2fbff1487df100ba0033bbe8e6833fd8660ac77c320c90fe920543fe2e01dc3ee65e31ea770bb9b43c5508c748fa0dea50631d12a53c2da31fd8e6f4d34970a
@@ -1,44 +1,50 @@
1
- module ActiveJob::Status
2
- class Progress
3
- delegate :[], :to_s, :to_json, :inspect, to: :hash
4
-
5
- def initialize(job)
6
- @job = job
7
- @total = 100
8
- @progress = 0
9
- end
10
-
11
- def total=(num)
12
- @total = num
13
- update
14
- end
15
-
16
- def progress=(num)
17
- update { num }
18
- end
19
-
20
- def increment(num=1)
21
- update { @progress + num }
22
- end
23
-
24
- def decrement(num=1)
25
- update { @progress - num }
26
- end
27
-
28
- def finish
29
- update { @total }
30
- end
31
-
32
- private
33
-
34
- def update
35
- @progress = yield if block_given?
36
- @job.status.update(hash)
37
- self
38
- end
39
-
40
- def hash
41
- { progress: @progress, total: @total }
1
+ module ActiveJob
2
+ module Status
3
+ class Progress
4
+ attr_reader :job, :total, :progress
5
+
6
+ delegate :[], :to_s, :to_json, :inspect, to: :to_h
7
+ delegate :status, to: :job, prefix: true
8
+
9
+ def initialize(job)
10
+ @job = job
11
+ @total = 100
12
+ @progress = 0
13
+ end
14
+
15
+ def total=(num)
16
+ @total = num
17
+ job_status.update(to_h, force: true)
18
+ self
19
+ end
20
+
21
+ def progress=(num)
22
+ @progress = num
23
+ job_status.update(to_h, force: true)
24
+ self
25
+ end
26
+
27
+ def increment(num = 1)
28
+ @progress += num
29
+ job_status.update(to_h)
30
+ self
31
+ end
32
+
33
+ def decrement(num = 1)
34
+ @progress -= num
35
+ job_status.update(to_h)
36
+ self
37
+ end
38
+
39
+ def finish
40
+ @progress = @total
41
+ job_status.update(to_h, force: true)
42
+ self
43
+ end
44
+
45
+ def to_h
46
+ { progress: @progress, total: @total }
47
+ end
42
48
  end
43
49
  end
44
50
  end
@@ -1,46 +1,51 @@
1
- module ActiveJob::Status
2
- class Status
3
- delegate :[], :to_s, :to_json, :inspect, to: :read
4
- delegate :queued?, :working?, :completed?, :failed?, to: :status_inquiry
5
-
6
- def initialize(job)
7
- @job = job
8
- end
9
-
10
- def []= key, value
11
- update(key => value)
12
- end
13
-
14
- def read
15
- Storage.read(@job)
16
- end
17
-
18
- def update(message)
19
- Storage.update(@job, message)
20
- end
21
-
22
- def delete
23
- Storage.delete(@job)
24
- end
25
-
26
- def job_id
27
- Storage.job_id(@job)
28
- end
29
-
30
- def status
31
- read[:status]
32
- end
33
-
34
- def progress
35
- read[:progress].to_f / read[:total].to_f
36
- end
37
-
38
- def present?
39
- read.present?
40
- end
41
-
42
- def status_inquiry
43
- status.to_s.inquiry
1
+ module ActiveJob
2
+ module Status
3
+ class Status
4
+ delegate :[], :to_s, :to_json, :inspect, to: :read
5
+ delegate :queued?, :working?, :completed?, :failed?, to: :status_inquiry
6
+
7
+ def initialize(job, options = {})
8
+ options = ActiveJob::Status.options.merge(options)
9
+ @storage = ActiveJob::Status::Storage.new(options)
10
+ @job = job
11
+ end
12
+
13
+ def []=(key, value)
14
+ update({ key => value }, force: true)
15
+ end
16
+
17
+ def read
18
+ @storage.read(@job)
19
+ end
20
+ alias to_h read
21
+
22
+ def update(message, **options)
23
+ @storage.update(@job, message, **options)
24
+ end
25
+
26
+ def delete
27
+ @storage.delete(@job)
28
+ end
29
+
30
+ def job_id
31
+ @storage.job_id(@job)
32
+ end
33
+
34
+ def status
35
+ read[:status]
36
+ end
37
+
38
+ def progress
39
+ read[:progress].to_f / read[:total].to_f
40
+ end
41
+
42
+ def present?
43
+ read.present?
44
+ end
45
+
46
+ def status_inquiry
47
+ status.to_s.inquiry
48
+ end
44
49
  end
45
50
  end
46
51
  end
@@ -1,12 +1,15 @@
1
- module ActiveJob::Status
2
- module Storage
3
- class << self
4
- def store
5
- ActiveJob::Status.store
1
+ module ActiveJob
2
+ module Status
3
+ class Storage
4
+ def initialize(options = {})
5
+ options.assert_valid_keys(:expires_in, :throttle_interval)
6
+
7
+ @expires_in = options[:expires_in]
8
+ @throttle = ActiveJob::Status::Throttle.new(options[:throttle_interval])
6
9
  end
7
10
 
8
- def options
9
- ActiveJob::Status.options
11
+ def store
12
+ @store ||= ActiveJob::Status.store
10
13
  end
11
14
 
12
15
  def job_id(job)
@@ -21,12 +24,17 @@ module ActiveJob::Status
21
24
  store.read(key(job)) || {}
22
25
  end
23
26
 
24
- def write(job, message)
25
- store.write(key(job), message, expires_in: options[:expires_in])
27
+ def write(job, message, force: false)
28
+ @throttle.wrap(force: force) do
29
+ store.write(key(job), message, expires_in: @expires_in)
30
+ end
26
31
  end
27
32
 
28
- def update(job, message)
29
- write(job, read(job).merge(message))
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)
37
+ end
30
38
  end
31
39
 
32
40
  def delete(job)
@@ -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.4'
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -1,50 +1,59 @@
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'
4
-
5
- module ActiveJob::Status
6
- extend ActiveSupport::Concern
7
- DEFAULT_EXPIRY = 60 * 30
8
-
9
- included do
10
- before_enqueue {|job| job.status.update(status: :queued) }
11
- before_perform {|job| job.status.update(status: :working) }
12
- after_perform {|job| job.status.update(status: :completed) }
13
-
14
- rescue_from(Exception) do |e|
15
- self.status.update(status: :failed)
16
- raise e
5
+ require 'activejob-status/throttle'
6
+
7
+ module ActiveJob
8
+ module Status
9
+ extend ActiveSupport::Concern
10
+
11
+ DEFAULT_OPTIONS = {
12
+ expires_in: 60 * 30,
13
+ throttle_interval: 0
14
+ }.freeze
15
+
16
+ included do
17
+ before_enqueue { |job| job.status.update(status: :queued) }
18
+ before_perform { |job| job.status.update(status: :working) }
19
+ after_perform { |job| job.status.update(status: :completed) }
20
+
21
+ rescue_from(Exception) do |e|
22
+ status.update(status: :failed)
23
+ raise e
24
+ end
17
25
  end
18
- end
19
-
20
- def status
21
- @status ||= Status.new(self)
22
- end
23
26
 
24
- def progress
25
- @progress ||= Progress.new(self)
26
- end
27
-
28
- class << self
29
- def options= options
30
- @@options = options
27
+ def status
28
+ @status ||= Status.new(self)
31
29
  end
32
30
 
33
- def options
34
- @@options ||= { expires_in: DEFAULT_EXPIRY }
31
+ def progress
32
+ @progress ||= Progress.new(self)
35
33
  end
36
34
 
37
- def store= store
38
- store = ActiveSupport::Cache.lookup_store(store) if store.is_a?(Symbol)
39
- @@store = store
40
- end
35
+ class << self
36
+ def options=(options)
37
+ options.assert_valid_keys(*DEFAULT_OPTIONS.keys)
38
+ @@options = DEFAULT_OPTIONS.merge(options)
39
+ end
41
40
 
42
- def store
43
- @@store ||= (defined?(Rails) && Rails.cache)
44
- end
41
+ def options
42
+ @@options ||= DEFAULT_OPTIONS
43
+ end
44
+
45
+ def store=(store)
46
+ store = ActiveSupport::Cache.lookup_store(store) if store.is_a?(Symbol)
47
+ @@store = store
48
+ end
49
+
50
+ def store
51
+ @@store ||= (defined?(Rails) && Rails.cache)
52
+ end
45
53
 
46
- def get(id)
47
- Status.new(id)
54
+ def get(id)
55
+ Status.new(id)
56
+ end
48
57
  end
49
58
  end
50
59
  end
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.4
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: 2018-06-28 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,11 +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
- homepage:
54
- licenses: []
54
+ homepage: https://github.com/inkstak/activejob-status
55
+ licenses:
56
+ - MIT
55
57
  metadata: {}
56
- post_install_message:
58
+ post_install_message:
57
59
  rdoc_options: []
58
60
  require_paths:
59
61
  - lib
@@ -68,9 +70,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
72
  requirements: []
71
- rubyforge_project:
72
- rubygems_version: 2.7.7
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: []