Class: Resque::Plugins::Meta::Metadata
- Inherits:
-
Object
- Object
- Resque::Plugins::Meta::Metadata
- Includes:
- Helpers
- Defined in:
- lib/resque/plugins/meta/metadata.rb
Instance Attribute Summary collapse
-
#before_finish_expire_in ⇒ Object
readonly
Returns the value of attribute before_finish_expire_in.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#enqueued_at ⇒ Object
readonly
Returns the value of attribute enqueued_at.
-
#expire_in ⇒ Object
readonly
Returns the value of attribute expire_in.
-
#job_class ⇒ Object
readonly
Returns the value of attribute job_class.
-
#meta_id ⇒ Object
readonly
Returns the value of attribute meta_id.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #enqueued? ⇒ Boolean
- #expire_at ⇒ Object
- #fail! ⇒ Object
- #failed? ⇒ Boolean
- #finish! ⇒ Object
- #finished? ⇒ Boolean
- #finished_at ⇒ Object
-
#initialize(data_hash) ⇒ Metadata
constructor
A new instance of Metadata.
-
#reload! ⇒ Object
Reload the metadata from the store.
-
#save ⇒ Object
Save the metadata.
- #seconds_enqueued ⇒ Object
- #seconds_processing ⇒ Object
- #start! ⇒ Object
- #started? ⇒ Boolean
- #started_at ⇒ Object
- #succeeded? ⇒ Boolean
- #working? ⇒ Boolean
Constructor Details
#initialize(data_hash) ⇒ Metadata
Returns a new instance of Metadata.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/resque/plugins/meta/metadata.rb', line 10 def initialize(data_hash) data_hash['enqueued_at'] ||= to_time_format_str(Time.now) @data = data_hash @meta_id = data_hash['meta_id'].dup @enqueued_at = from_time_format_str('enqueued_at') @job_class = data_hash['job_class'] if @job_class.is_a?(String) @job_class = constantize(data_hash['job_class']) else data_hash['job_class'] = @job_class.to_s end @expire_in = @job_class. || 0 @before_finish_expire_in = @job_class.before_finish_expire_in || 0 end |
Instance Attribute Details
#before_finish_expire_in ⇒ Object (readonly)
Returns the value of attribute before_finish_expire_in.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def before_finish_expire_in @before_finish_expire_in end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def data @data end |
#enqueued_at ⇒ Object (readonly)
Returns the value of attribute enqueued_at.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def enqueued_at @enqueued_at end |
#expire_in ⇒ Object (readonly)
Returns the value of attribute expire_in.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def expire_in @expire_in end |
#job_class ⇒ Object (readonly)
Returns the value of attribute job_class.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def job_class @job_class end |
#meta_id ⇒ Object (readonly)
Returns the value of attribute meta_id.
8 9 10 |
# File 'lib/resque/plugins/meta/metadata.rb', line 8 def @meta_id end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'lib/resque/plugins/meta/metadata.rb', line 39 def [](key) data[key] end |
#[]=(key, val) ⇒ Object
43 44 45 |
# File 'lib/resque/plugins/meta/metadata.rb', line 43 def []=(key, val) data[key.to_s] = val end |
#enqueued? ⇒ Boolean
81 82 83 |
# File 'lib/resque/plugins/meta/metadata.rb', line 81 def enqueued? !started? end |
#expire_at ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/resque/plugins/meta/metadata.rb', line 68 def expire_at # expiry after finished if finished? && expire_in > 0 finished_at.to_i + expire_in # expiry after enqueued elsif before_finish_expire_in > 0 enqueued_at.to_i + before_finish_expire_in # default ttl is forever else 0 end end |
#fail! ⇒ Object
97 98 99 100 |
# File 'lib/resque/plugins/meta/metadata.rb', line 97 def fail! self['succeeded'] = false finish! end |
#failed? ⇒ Boolean
106 107 108 |
# File 'lib/resque/plugins/meta/metadata.rb', line 106 def failed? finished? ? !self['succeeded'] : nil end |
#finish! ⇒ Object
57 58 59 60 61 62 |
# File 'lib/resque/plugins/meta/metadata.rb', line 57 def finish! data['succeeded'] = true unless data.has_key?('succeeded') @finished_at = Time.now self['finished_at'] = to_time_format_str(@finished_at) save end |
#finished? ⇒ Boolean
93 94 95 |
# File 'lib/resque/plugins/meta/metadata.rb', line 93 def finished? finished_at ? true : false end |
#finished_at ⇒ Object
64 65 66 |
# File 'lib/resque/plugins/meta/metadata.rb', line 64 def finished_at @finished_at ||= from_time_format_str('finished_at') end |
#reload! ⇒ Object
Reload the metadata from the store
26 27 28 29 30 31 |
# File 'lib/resque/plugins/meta/metadata.rb', line 26 def reload! if = job_class.() @data = .data end self end |
#save ⇒ Object
Save the metadata
34 35 36 37 |
# File 'lib/resque/plugins/meta/metadata.rb', line 34 def save job_class.(self) self end |
#seconds_enqueued ⇒ Object
110 111 112 |
# File 'lib/resque/plugins/meta/metadata.rb', line 110 def seconds_enqueued (started_at || Time.now).to_f - enqueued_at.to_f end |
#seconds_processing ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/resque/plugins/meta/metadata.rb', line 114 def seconds_processing if started? (finished_at || Time.now).to_f - started_at.to_f else 0 end end |
#start! ⇒ Object
47 48 49 50 51 |
# File 'lib/resque/plugins/meta/metadata.rb', line 47 def start! @started_at = Time.now self['started_at'] = to_time_format_str(@started_at) save end |
#started? ⇒ Boolean
89 90 91 |
# File 'lib/resque/plugins/meta/metadata.rb', line 89 def started? started_at ? true :false end |
#started_at ⇒ Object
53 54 55 |
# File 'lib/resque/plugins/meta/metadata.rb', line 53 def started_at @started_at ||= from_time_format_str('started_at') end |
#succeeded? ⇒ Boolean
102 103 104 |
# File 'lib/resque/plugins/meta/metadata.rb', line 102 def succeeded? finished? ? self['succeeded'] : nil end |
#working? ⇒ Boolean
85 86 87 |
# File 'lib/resque/plugins/meta/metadata.rb', line 85 def working? started? && !finished? end |