Class: Cloudist::Job

Inherits:
Object show all
Defined in:
lib/cloudist/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Job

Returns a new instance of Job.



5
6
7
8
9
10
11
12
13
14
# File 'lib/cloudist/job.rb', line 5

def initialize(payload)
  @payload = payload

  if payload.reply_to
    @reply_queue = ReplyQueue.new(payload.reply_to)
    reply_queue.setup
  else
    @reply_queue = nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/cloudist/job.rb', line 76

def method_missing(meth, *args, &blk)
  if meth.to_s.ends_with?("!")
    event(meth.to_s.gsub(/(!)$/, ''), args.shift)
  else
    super
  end
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



3
4
5
# File 'lib/cloudist/job.rb', line 3

def payload
  @payload
end

#reply_queueObject (readonly)

Returns the value of attribute reply_queue.



3
4
5
# File 'lib/cloudist/job.rb', line 3

def reply_queue
  @reply_queue
end

Instance Method Details

#bodyObject



24
25
26
# File 'lib/cloudist/job.rb', line 24

def body
  data
end

#cleanupObject



32
33
34
# File 'lib/cloudist/job.rb', line 32

def cleanup
  # :noop
end

#dataObject



20
21
22
# File 'lib/cloudist/job.rb', line 20

def data
  payload.body
end

#event(event_name, event_data = {}, options = {}) ⇒ Object



61
62
63
64
# File 'lib/cloudist/job.rb', line 61

def event(event_name, event_data = {}, options = {})
  event_data ||= {}
  reply(event_data, {:event => event_name, :message_type => 'event'}, options)
end

#handle_error(e) ⇒ Object



72
73
74
# File 'lib/cloudist/job.rb', line 72

def handle_error(e)
  reply({:exception => e.class.name.to_s, :message => e.message.to_s, :backtrace => e.backtrace}, {:message_type => 'error'})
end

#idObject



16
17
18
# File 'lib/cloudist/job.rb', line 16

def id
  payload.id
end

#logObject



28
29
30
# File 'lib/cloudist/job.rb', line 28

def log
  Cloudist.log
end

#progress(percentage, description = nil) ⇒ Object

Sends a progress update Inputs: percentage - Integer Optional description, this could be displayed to the user e.g. Resizing image



57
58
59
# File 'lib/cloudist/job.rb', line 57

def progress(percentage, description = nil)
  reply({:progress => percentage, :description => description}, {:message_type => 'progress'})
end

#reply(body, headers = {}, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudist/job.rb', line 36

def reply(body, headers = {}, options = {})
  raise ArgumentError, "Reply queue not ready" unless reply_queue

  options = {
    :echo => false
  }.update(options)

  headers = {
    :message_id => payload.id,
    :message_type => "reply"
  }.update(headers)

  reply_payload = Payload.new(body, headers)
  published_headers = reply_queue.publish(reply_payload)

  reply_payload
end

#safely(&blk) ⇒ Object



66
67
68
69
70
# File 'lib/cloudist/job.rb', line 66

def safely(&blk)
  yield
rescue Exception => e
  handle_error(e)
end