Class: Progressrus

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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_atObject (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

#countObject

Returns the value of attribute count.



31
32
33
# File 'lib/progressrus.rb', line 31

def count
  @count
end

#error_countObject (readonly)

Returns the value of attribute error_count.



31
32
33
# File 'lib/progressrus.rb', line 31

def error_count
  @error_count
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



31
32
33
# File 'lib/progressrus.rb', line 31

def expires_at
  @expires_at
end

#failed_atObject (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

#idObject (readonly)

Returns the value of attribute id.



31
32
33
# File 'lib/progressrus.rb', line 31

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/progressrus.rb', line 31

def name
  @name
end

#paramsObject

Returns the value of attribute params.



31
32
33
# File 'lib/progressrus.rb', line 31

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



31
32
33
# File 'lib/progressrus.rb', line 31

def scope
  @scope
end

#started_atObject (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

#storeObject (readonly)

Returns the value of attribute store.



31
32
33
# File 'lib/progressrus.rb', line 31

def store
  @store
end

#storesObject (readonly)

Returns the value of attribute stores.



31
32
33
# File 'lib/progressrus.rb', line 31

def stores
  @stores
end

#totalObject

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

.storesObject



13
14
15
# File 'lib/progressrus.rb', line 13

def stores
  @@stores ||= Store.new(Store::Redis.new(::Redis.new(host: ENV["PROGRESSRUS_REDIS_HOST"] || "localhost")))
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

Returns:

  • (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

#flushObject



86
87
88
# File 'lib/progressrus.rb', line 86

def flush
  stores.each { |store| store.flush(scope, id) }
end

#percentageObject



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

Returns:

  • (Boolean)


155
156
157
# File 'lib/progressrus.rb', line 155

def persisted?
  @persisted
end

#running?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/progressrus.rb', line 97

def running?
  count > 0
end

#statusObject



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_serializeableObject



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