Class: Progressrus
- Inherits:
-
Object
- Object
- Progressrus
- Defined in:
- lib/progressrus.rb,
lib/progressrus/store.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: Railtie, Server, Store
Constant Summary collapse
- VERSION =
"0.1.5"
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
- .find(scope, id, store: :first) ⇒ Object
- .flush(scope, id = nil, store: :first) ⇒ Object
- .scope(scope, store: :first) ⇒ 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/progressrus.rb', line 40 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 |
Instance Attribute Details
#completed_at ⇒ Object (readonly) Also known as: completed?
Returns the value of attribute completed_at.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def completed_at @completed_at end |
#count ⇒ Object
Returns the value of attribute count.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def count @count end |
#error_count ⇒ Object (readonly)
Returns the value of attribute error_count.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def error_count @error_count end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def expires_at @expires_at end |
#failed_at ⇒ Object (readonly) Also known as: failed?
Returns the value of attribute failed_at.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def failed_at @failed_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def scope @scope end |
#started_at ⇒ Object (readonly) Also known as: started?
Returns the value of attribute started_at.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def started_at @started_at end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def store @store end |
#stores ⇒ Object (readonly)
Returns the value of attribute stores.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def stores @stores end |
#total ⇒ Object
Returns the value of attribute total.
31 32 33 |
# File 'lib/progressrus.rb', line 31 def total @total end |
Class Method Details
.find(scope, id, store: :first) ⇒ Object
22 23 24 |
# File 'lib/progressrus.rb', line 22 def find(scope, id, store: :first) stores.find_by_name(store).find(scope, id) end |
.flush(scope, id = nil, store: :first) ⇒ Object
26 27 28 |
# File 'lib/progressrus.rb', line 26 def flush(scope, id = nil, store: :first) stores.find_by_name(store).flush(scope, id) end |
.scope(scope, store: :first) ⇒ Object Also known as: all
17 18 19 |
# File 'lib/progressrus.rb', line 17 def scope(scope, store: :first) stores.find_by_name(store).scope(scope) end |
Instance Method Details
#complete(now: Time.now) ⇒ Object
80 81 82 83 84 |
# File 'lib/progressrus.rb', line 80 def complete(now: Time.now) @started_at ||= now @completed_at = now persist(force: true) end |
#elapsed(now: Time.now) ⇒ Object
130 131 132 |
# File 'lib/progressrus.rb', line 130 def elapsed(now: Time.now) now - started_at end |
#error(ticks = 1, now: Time.now) ⇒ Object
71 72 73 74 |
# File 'lib/progressrus.rb', line 71 def error(ticks = 1, now: Time.now) @error_count ||= 0 @error_count += ticks end |
#eta(now: Time.now) ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/progressrus.rb', line 142 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
151 152 153 |
# File 'lib/progressrus.rb', line 151 def expired?(now: Time.now) expires_at && expires_at < now end |
#fail(now: Time.now) ⇒ Object
101 102 103 104 105 |
# File 'lib/progressrus.rb', line 101 def fail(now: Time.now) @started_at ||= now @failed_at = now persist(force: true) end |
#flush ⇒ Object
86 87 88 |
# File 'lib/progressrus.rb', line 86 def flush stores.each { |store| store.flush(scope, id) } end |
#percentage ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/progressrus.rb', line 134 def percentage if total > 0 count.to_f / total else 1.0 end end |
#persisted? ⇒ Boolean
155 156 157 |
# File 'lib/progressrus.rb', line 155 def persisted? @persisted end |
#running? ⇒ Boolean
97 98 99 |
# File 'lib/progressrus.rb', line 97 def running? count > 0 end |
#status ⇒ Object
90 91 92 93 94 95 |
# File 'lib/progressrus.rb', line 90 def status return :completed if completed? return :failed if failed? return :running if running? :started end |
#tick(ticks = 1, now: Time.now) ⇒ Object
65 66 67 68 69 |
# File 'lib/progressrus.rb', line 65 def tick(ticks = 1, now: Time.now) @started_at ||= now if ticks >= 1 @count += ticks persist end |
#to_serializeable ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/progressrus.rb', line 107 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 |