Module: RabbitJobs::Job

Defined in:
lib/rabbit_jobs/job.rb

Overview

Module to include in client jobs.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



6
7
8
# File 'lib/rabbit_jobs/job.rb', line 6

def created_at
  @created_at
end

Class Method Details

.included(base) ⇒ Object



100
101
102
103
# File 'lib/rabbit_jobs/job.rb', line 100

def included(base)
  base.extend(ClassMethods)
  base.queue(:jobs)
end

.parse(payload) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rabbit_jobs/job.rb', line 78

def parse(payload)
  encoded = JSON.parse(payload)
  job_klass = encoded['class'].to_s.constantize
  job = job_klass.new
  job.created_at = encoded['created_at']
  [job, encoded['params']]
rescue NameError
  [:not_found, encoded['class']]
rescue JSON::ParserError
  [:parsing_error, payload]
rescue
  [:error, $!, payload]
end

.serialize(klass_name, *params) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rabbit_jobs/job.rb', line 92

def serialize(klass_name, *params)
  {
    'class' => klass_name.to_s,
    'created_at' => Time.now.to_i,
    'params' => params
  }.to_json
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/rabbit_jobs/job.rb', line 8

def expired?
  exp_in = self.class.expires_in.to_i
  return false if exp_in == 0 || created_at.nil?

  Time.now.to_i > created_at + exp_in
end

#to_ruby_string(*params) ⇒ Object



15
16
17
18
19
# File 'lib/rabbit_jobs/job.rb', line 15

def to_ruby_string(*params)
  rs = self.class.name + params_string(*params)
  rs << ", created_at: #{created_at}" if created_at
  rs
end