Skip to content

Commit

Permalink
making errors work, actor can call fail to immediately end execution …
Browse files Browse the repository at this point in the history
…and return error
  • Loading branch information
lee committed May 16, 2009
1 parent 25f6c63 commit 39c5e5d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
17 changes: 15 additions & 2 deletions lib/pelvis/actor.rb
Expand Up @@ -69,14 +69,18 @@ def start
if self.class.resources
my_resources = [params[:resources]].flatten.compact & self.class.resources
if my_resources.empty?
raise "invalid resources #{params[:resources]} supplied"
fail :code => 404, :message => "invalid resources #{params[:resources]} supplied"
end
params[:resources] = my_resources
end

send(@operation)
rescue => e
@invocation.failed(e.message)
if @__ERROR__
failed(@__ERROR__)
else
failed(:code => 500, :message => e.message)
end
end

def finish
Expand Down Expand Up @@ -107,5 +111,14 @@ def job
def params
job.args
end

def fail(data)
@__ERROR__ = data
raise
end

def failed(*args)
@invocation.failed(*args)
end
end
end
2 changes: 1 addition & 1 deletion lib/pelvis/protocols/xmpp.rb
Expand Up @@ -35,7 +35,7 @@ def call(stanza)
token = node["token"]

type = node["type"]
unless %w( init begin data end ).include?(type)
unless %w( init begin data end error ).include?(type)
raise "Unable to handle job XML of type: #{type.inspect}"
end

Expand Down
12 changes: 11 additions & 1 deletion lib/pelvis/protocols/xmpp/remote_agent.rb
Expand Up @@ -38,7 +38,7 @@ def handle_job_init(stanza, node, token)
end
incall.on_failed do |error|
logger.error "got an error for token: #{token}"
send_error(job.token, error)
send_job_error(job.token, error)
end
incall
end
Expand All @@ -58,6 +58,12 @@ def handle_job_end(stanza, node, token)
send_result(stanza)
end

def handle_job_error(stanza, node, token)
data = JSON.parse(Base64.decode64(node.content)).to_mash
incall_for(token).failed(data)
send_result(stanza)
end

def handle_result(stanza, node, token)
block, original = outbounds.delete(stanza.id)
if block
Expand Down Expand Up @@ -107,6 +113,10 @@ def send_job_end(token, &block)
send_stanza(:set, "end", nil, :token => token, &block)
end

def send_job_error(token, error, &block)
send_stanza(:set, 'error', error, :token => token, &block)
end

def send_result(stanza)
reply = stanza.reply
reply.attributes[:type] = :result
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -57,4 +57,4 @@ def echo

require File.dirname(__FILE__) + '/helpers'

Pelvis.logger.level = Logger::ERROR
Pelvis.logger.level = Logger::FATAL

0 comments on commit 39c5e5d

Please sign in to comment.