librelist archives

« back to archive

Check for job control status

Check for job control status

From:
Nick Willever
Date:
2014-04-15 @ 05:10
Hi,

I'm running Sidekiq 3.0 and ruby 2.1

I want to start a thread inside the worker and allow that thread to check a
database record for signals.  This way the (thr) can abort the job from a
signal sent from the database without aborting the entire sidekiq process.
 I tested the code out and it seems to work with multiple workers running.

I was wondering how safe this code is and if there's a better way to do it.
I was looking around the documentation and didn't find a way to stop a busy
job without shutting down the sidekiq process.

class MyWorker
  include Sidekiq::Worker
  sidekiq_options queue: :workers, retry: false, backtrace: false
    def perform
    begin
      puts "starting worker"
              begin
          thr = Thread.handle_interrupt(RuntimeError => :immediate) do
            while true
              puts "thread is sleeping"
              sleep 5
                            job_check = JobCheck.find(1) #activerecord
database call

                            if job_check.control == "stop"
                raise RuntimeError, "Got a kill signal"
              end
            end
          end

        rescue => e
          puts e.message
          return false
        end
            puts "doing work"
      sleep 15

    rescue => e
      puts "error"
      return false
    end
  end


Code is also here <https://gist.github.com/nictrix/10703304> incase it
comes up jumbled in this email.  Maybe instead of putting it in mysql I can
put this job checker in redis to keep it as light as possible.

Thanks,

- Nick

Re: Check for job control status

From:
Nick Willever
Date:
2014-04-19 @ 06:36
Nevermind on this, I clearly didn't get it working as I thought I did after
posting this message (didn't have the while loop in).  Instead I ended up
using strategic locations of the sidekiq worker to allow it to pull its
current status stop or start from the database. It will error out if it's
stop status.

- Nick


On Mon, Apr 14, 2014 at 10:10 PM, Nick Willever <nickwillever@gmail.com>wrote:

> Hi,
>
> I'm running Sidekiq 3.0 and ruby 2.1
>
> I want to start a thread inside the worker and allow that thread to check
> a database record for signals.  This way the (thr) can abort the job from a
> signal sent from the database without aborting the entire sidekiq process.
>  I tested the code out and it seems to work with multiple workers running.
>
> I was wondering how safe this code is and if there's a better way to do
> it. I was looking around the documentation and didn't find a way to stop a
> busy job without shutting down the sidekiq process.
>
> class MyWorker
>   include Sidekiq::Worker
>   sidekiq_options queue: :workers, retry: false, backtrace: false
>     def perform
>     begin
>       puts "starting worker"
>               begin
>           thr = Thread.handle_interrupt(RuntimeError => :immediate) do
>             while true
>               puts "thread is sleeping"
>               sleep 5
>                             job_check = JobCheck.find(1) #activerecord 
database call
>
>                             if job_check.control == "stop"
>                 raise RuntimeError, "Got a kill signal"
>               end
>             end
>           end
>
>         rescue => e
>           puts e.message
>           return false
>         end
>             puts "doing work"
>       sleep 15
>
>     rescue => e
>       puts "error"
>       return false
>     end
>   end
>
>
> Code is also here <https://gist.github.com/nictrix/10703304> incase it
> comes up jumbled in this email.  Maybe instead of putting it in mysql I can
> put this job checker in redis to keep it as light as possible.
>
> Thanks,
>
> - Nick
>