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
Constant Summary collapse
- VERSION =
'1.0.0'
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: Progressrus.stores, 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: Progressrus.stores, 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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/progressrus.rb', line 76 def initialize(scope: "progressrus", total: nil, name: nil, id: SecureRandom.uuid, params: {}, stores: Progressrus.stores, 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 = 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.
25 26 27 |
# File 'lib/progressrus.rb', line 25 def mutex @mutex end |
Instance Attribute Details
#completed_at ⇒ Object (readonly) Also known as: completed?
Returns the value of attribute completed_at.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def completed_at @completed_at end |
#count ⇒ Object
Returns the value of attribute count.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def count @count end |
#error_count ⇒ Object (readonly)
Returns the value of attribute error_count.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def error_count @error_count end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def expires_at @expires_at end |
#failed_at ⇒ Object (readonly) Also known as: failed?
Returns the value of attribute failed_at.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def failed_at @failed_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def scope @scope end |
#started_at ⇒ Object (readonly) Also known as: started?
Returns the value of attribute started_at.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def started_at @started_at end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def store @store end |
#stores ⇒ Object (readonly)
Returns the value of attribute stores.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def stores @stores end |
#total ⇒ Object
Returns the value of attribute total.
67 68 69 |
# File 'lib/progressrus.rb', line 67 def total @total end |
Class Method Details
.add_store(name, store) ⇒ Object
39 40 41 42 |
# File 'lib/progressrus.rb', line 39 def add_store(name, store) validate_store!(store) stores[name] = store end |
.clear_stores ⇒ Object
27 28 29 |
# File 'lib/progressrus.rb', line 27 def clear_stores @stores = {} end |
.find(scope, id, store: :redis) ⇒ Object
49 50 51 |
# File 'lib/progressrus.rb', line 49 def find(scope, id, store: :redis) stores[store].find(scope, id) end |
.flush(scope, id = nil, store: :redis) ⇒ Object
53 54 55 |
# File 'lib/progressrus.rb', line 53 def flush(scope, id = nil, store: :redis) stores[store].flush(scope, id) end |
.scope(scope, store: :redis) ⇒ Object Also known as: all
44 45 46 |
# File 'lib/progressrus.rb', line 44 def scope(scope, store: :redis) stores[store].scope(scope) end |
Instance Method Details
#complete(now: Time.now) ⇒ Object
116 117 118 119 120 |
# File 'lib/progressrus.rb', line 116 def complete(now: Time.now) @started_at ||= now @completed_at = now persist(force: true) end |
#elapsed(now: Time.now) ⇒ Object
166 167 168 |
# File 'lib/progressrus.rb', line 166 def elapsed(now: Time.now) now - started_at end |
#error(ticks = 1, now: Time.now) ⇒ Object
107 108 109 110 |
# File 'lib/progressrus.rb', line 107 def error(ticks = 1, now: Time.now) @error_count ||= 0 @error_count += ticks end |
#eta(now: Time.now) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/progressrus.rb', line 180 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
189 190 191 |
# File 'lib/progressrus.rb', line 189 def expired?(now: Time.now) expires_at && expires_at < now end |
#fail(now: Time.now) ⇒ Object
137 138 139 140 141 |
# File 'lib/progressrus.rb', line 137 def fail(now: Time.now) @started_at ||= now @failed_at = now persist(force: true) end |
#flush ⇒ Object
122 123 124 |
# File 'lib/progressrus.rb', line 122 def flush stores.each_value { |store| store.flush(scope, id) } end |
#percentage ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/progressrus.rb', line 170 def percentage return unless total if total > 0 count.to_f / total else 1.0 end end |
#persisted? ⇒ Boolean
193 194 195 |
# File 'lib/progressrus.rb', line 193 def persisted? @persisted end |
#running? ⇒ Boolean
133 134 135 |
# File 'lib/progressrus.rb', line 133 def running? count > 0 end |
#status ⇒ Object
126 127 128 129 130 131 |
# File 'lib/progressrus.rb', line 126 def status return :completed if completed? return :failed if failed? return :running if running? :started end |
#tick(ticks = 1, now: Time.now) ⇒ Object
101 102 103 104 105 |
# File 'lib/progressrus.rb', line 101 def tick(ticks = 1, now: Time.now) @started_at ||= now if ticks >= 1 @count += ticks persist end |
#to_serializeable ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/progressrus.rb', line 143 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 |