Class: Progressrus
- Inherits:
-
Object
- Object
- Progressrus
- Defined in:
- lib/progressrus.rb,
lib/progressrus/server.rb,
lib/progressrus/railtie.rb,
lib/progressrus/version.rb,
lib/progressrus/store/base.rb,
lib/progressrus/store/redis.rb,
lib/progressrus/store/progressbar.rb
Defined Under Namespace
Classes: InvalidStoreError, Railtie, Server, Store, StoreNotFoundError
Constant Summary collapse
- VERSION =
'1.0.2'
Class Attribute Summary collapse
-
.mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Attribute Summary collapse
-
#completed_at ⇒ Object
(also: #completed?)
readonly
Returns the value of attribute completed_at.
-
#count ⇒ Object
Returns the value of attribute count.
-
#error_count ⇒ Object
readonly
Returns the value of attribute error_count.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#failed_at ⇒ Object
(also: #failed?)
readonly
Returns the value of attribute failed_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#started_at ⇒ Object
(also: #started?)
readonly
Returns the value of attribute started_at.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#stores ⇒ Object
readonly
Returns the value of attribute stores.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
- .add_store(name, store) ⇒ Object
- .clear_stores ⇒ Object
- .find(scope, id, store: :redis) ⇒ Object
- .flush(scope, id = nil, store: :redis) ⇒ Object
- .scope(scope, store: :redis) ⇒ Object (also: all)
- .stores ⇒ Object
Instance Method Summary collapse
- #complete(now: Time.now) ⇒ Object
- #elapsed(now: Time.now) ⇒ Object
- #error(ticks = 1, now: Time.now) ⇒ Object
- #eta(now: Time.now) ⇒ Object
- #expired?(now: Time.now) ⇒ Boolean
- #fail(now: Time.now) ⇒ Object
- #flush ⇒ Object
-
#initialize(scope: "progressrus", total: nil, name: nil, id: SecureRandom.uuid, params: {}, stores: nil, completed_at: nil, started_at: nil, count: 0, failed_at: nil, error_count: 0, persist: false, expires_at: nil, persisted: false) ⇒ Progressrus
constructor
A new instance of Progressrus.
- #percentage ⇒ Object
- #persisted? ⇒ Boolean
- #running? ⇒ Boolean
- #status ⇒ Object
- #tick(ticks = 1, now: Time.now) ⇒ Object
- #to_serializeable ⇒ Object
Constructor Details
#initialize(scope: "progressrus", total: nil, name: nil, id: SecureRandom.uuid, params: {}, stores: nil, completed_at: nil, started_at: nil, count: 0, failed_at: nil, error_count: 0, persist: false, expires_at: nil, persisted: false) ⇒ Progressrus
Returns a new instance of Progressrus.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/progressrus.rb', line 86 def initialize(scope: "progressrus", total: nil, name: nil, id: SecureRandom.uuid, params: {}, stores: nil, completed_at: nil, started_at: nil, count: 0, failed_at: nil, error_count: 0, persist: false, expires_at: nil, persisted: false) raise ArgumentError, "Total cannot be negative." if total && total < 0 @name = name || id @scope = Array(scope).map(&:to_s) @total = total @id = id @params = params @stores = extract_stores(stores) @count = count @error_count = error_count @started_at = parse_time(started_at) @completed_at = parse_time(completed_at) @failed_at = parse_time(failed_at) @expires_at = parse_time(expires_at) @persisted = persisted persist(force: true) if persist end |
Class Attribute Details
.mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
35 36 37 |
# File 'lib/progressrus.rb', line 35 def mutex @mutex end |
Instance Attribute Details
#completed_at ⇒ Object (readonly) Also known as: completed?
Returns the value of attribute completed_at.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def completed_at @completed_at end |
#count ⇒ Object
Returns the value of attribute count.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def count @count end |
#error_count ⇒ Object (readonly)
Returns the value of attribute error_count.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def error_count @error_count end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def expires_at @expires_at end |
#failed_at ⇒ Object (readonly) Also known as: failed?
Returns the value of attribute failed_at.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def failed_at @failed_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def scope @scope end |
#started_at ⇒ Object (readonly) Also known as: started?
Returns the value of attribute started_at.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def started_at @started_at end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def store @store end |
#stores ⇒ Object (readonly)
Returns the value of attribute stores.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def stores @stores end |
#total ⇒ Object
Returns the value of attribute total.
77 78 79 |
# File 'lib/progressrus.rb', line 77 def total @total end |
Class Method Details
.add_store(name, store) ⇒ Object
49 50 51 52 |
# File 'lib/progressrus.rb', line 49 def add_store(name, store) validate_store!(store) stores[name] = store end |
.clear_stores ⇒ Object
37 38 39 |
# File 'lib/progressrus.rb', line 37 def clear_stores @stores = {} end |
.find(scope, id, store: :redis) ⇒ Object
59 60 61 |
# File 'lib/progressrus.rb', line 59 def find(scope, id, store: :redis) stores[store].find(scope, id) end |
.flush(scope, id = nil, store: :redis) ⇒ Object
63 64 65 |
# File 'lib/progressrus.rb', line 63 def flush(scope, id = nil, store: :redis) stores[store].flush(scope, id) end |
.scope(scope, store: :redis) ⇒ Object Also known as: all
54 55 56 |
# File 'lib/progressrus.rb', line 54 def scope(scope, store: :redis) stores[store].scope(scope) end |
Instance Method Details
#complete(now: Time.now) ⇒ Object
126 127 128 129 130 |
# File 'lib/progressrus.rb', line 126 def complete(now: Time.now) @started_at ||= now @completed_at = now persist(force: true) end |
#elapsed(now: Time.now) ⇒ Object
176 177 178 |
# File 'lib/progressrus.rb', line 176 def elapsed(now: Time.now) now - started_at end |
#error(ticks = 1, now: Time.now) ⇒ Object
117 118 119 120 |
# File 'lib/progressrus.rb', line 117 def error(ticks = 1, now: Time.now) @error_count ||= 0 @error_count += ticks end |
#eta(now: Time.now) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/progressrus.rb', line 190 def eta(now: Time.now) return if count.to_i == 0 || total.to_i == 0 processed_per_second = (count.to_f / elapsed(now: now)) left = (total - count) seconds_to_finished = left / processed_per_second now + seconds_to_finished end |
#expired?(now: Time.now) ⇒ Boolean
199 200 201 |
# File 'lib/progressrus.rb', line 199 def expired?(now: Time.now) expires_at && expires_at < now end |
#fail(now: Time.now) ⇒ Object
147 148 149 150 151 |
# File 'lib/progressrus.rb', line 147 def fail(now: Time.now) @started_at ||= now @failed_at = now persist(force: true) end |
#flush ⇒ Object
132 133 134 |
# File 'lib/progressrus.rb', line 132 def flush stores.each_value { |store| store.flush(scope, id) } end |
#percentage ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/progressrus.rb', line 180 def percentage return unless total if total > 0 count.to_f / total else 1.0 end end |
#persisted? ⇒ Boolean
203 204 205 |
# File 'lib/progressrus.rb', line 203 def persisted? @persisted end |
#running? ⇒ Boolean
143 144 145 |
# File 'lib/progressrus.rb', line 143 def running? count > 0 end |
#status ⇒ Object
136 137 138 139 140 141 |
# File 'lib/progressrus.rb', line 136 def status return :completed if completed? return :failed if failed? return :running if running? :started end |
#tick(ticks = 1, now: Time.now) ⇒ Object
111 112 113 114 115 |
# File 'lib/progressrus.rb', line 111 def tick(ticks = 1, now: Time.now) @started_at ||= now if ticks >= 1 @count += ticks persist end |
#to_serializeable ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/progressrus.rb', line 153 def to_serializeable { name: name, id: id, scope: scope, started_at: started_at, completed_at: completed_at, failed_at: failed_at, expires_at: expires_at, count: count, total: total, params: params, error_count: error_count } end |