0

I am using delayed_job to perform a system call. This system call invokes a Groovy script.

class Job
    def perform
        command "groovy file.groovy"
        system command
    end

    def success(job)
        # handle success
    end

    def error(job, exception)
        # handle error, exception
    end
end

This is all working perfectly but I always get back a "success" status since my groovy always exits correctly. I am currently throwing RuntimeExeptions in the Groovy Script to provoke failing job. When I call system "groovy progra.groovy" and it raises an exception (The groovy program raises an Exception), the return value of system call is, as expected false. But when doing the same via delayed_job, it never accesses the def error method but the def success method.

Do you have any advise on how delayed_job actually controls the returns of the perform method? When does it enter the error or failure hook. Unfortunately I have not found anything on this topic in the documentation.

Thanks in advance

pabera
  • 1,042
  • 1
  • 13
  • 22

1 Answers1

3

Have you tried something like this :

def perform
    command = "groovy file.groovy"
    system command || raise "Error executing command : #{command}"
end

I'm pretty sure delayed_job wraps the perform call in a rescue block and call success or error based on any exception it catches.

systho
  • 1,161
  • 7
  • 17
  • Unfortunately, the system command does not raise any exception. Even when the exit code is 1+ ... – pabera Dec 21 '12 at 11:47
  • this is precisely the reason why I suggest you to add '|| raise "Error executing command : #{command}"' after the system() call. You could also check if the result is nil and in that case raise an Exception like "command #{command} failed (reason : $?)", my first proposition was just a check for "falsy" values – systho Dec 21 '12 at 12:49
  • In combination with [this](http://stackoverflow.com/questions/690151/getting-output-of-system-calls-in-ruby), I got my error handling running. Thank you a lot. – pabera Dec 21 '12 at 13:39