worker.rb - warvox - VoIP based wardialing tool, forked from rapid7/warvox.
 (HTM) git clone git://jay.scot/warvox
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       worker.rb (1877B)
       ---
            1 #!/usr/bin/env ruby
            2 ###################
            3 
            4 #
            5 # Load the library path
            6 #
            7 base = __FILE__
            8 while File.symlink?(base)
            9   base = File.expand_path(File.readlink(base), File.dirname(base))
           10 end
           11 $:.unshift(File.join(File.expand_path(File.dirname(base)), '..', 'lib'))
           12 
           13 require 'warvox'
           14 require 'fileutils'
           15 
           16 
           17 ENV['RAILS_ENV'] ||= 'production'
           18 $:.unshift(File.join(File.expand_path(File.dirname(base)), '..'))
           19 
           20 @task = nil
           21 @job  = nil
           22 
           23 def usage
           24   $stderr.puts "Usage: #{$0} [JID]"
           25   exit(1)
           26 end
           27 
           28 def stop
           29   if @task
           30     @task.stop() rescue nil
           31   end
           32   if @job
           33     Job.where(id: @job_id).update_all({ status: 'stopped', completed_at: Time.now })
           34   end
           35   exit(0)
           36 end
           37 
           38 #
           39 # Script
           40 #
           41 
           42 jid = ARGV.shift() || usage()
           43 if (jid and jid =="-h") or (! jid)
           44   usage()
           45 end
           46 
           47 require 'config/boot'
           48 require 'config/environment'
           49 
           50 trap("SIGTERM") { stop() }
           51 
           52 jid = jid.to_i
           53 
           54 @job = Job.where(id: jid).first
           55 
           56 unless @job
           57   $stderr.puts "Error: Specified job not found"
           58   WarVOX::Log.warn("Worker rejected invalid Job #{jid}")
           59   exit(1)
           60 end
           61 
           62 $0 = "warvox worker: #{jid} "
           63 
           64 Job.where(id: @job.id).update_all({ started_at: Time.now.utc, status: 'running'})
           65 
           66 args = Marshal.load(@job.args) rescue {}
           67 
           68 
           69 WarVOX::Log.debug("Worker #{@job.id} #{@job.task} is running #{@job.task} with parameters #{ args.inspect }")
           70 
           71 begin
           72 
           73 case @job.task
           74 when 'dialer'
           75   @task = WarVOX::Jobs::Dialer.new(@job.id, args)
           76   @task.start
           77 when 'analysis'
           78   @task = WarVOX::Jobs::Analysis.new(@job.id, args)
           79   @task.start
           80 else
           81   Job.where(id: @job.id).update_all({ error: 'unsupported', status: 'error' })
           82 end
           83 
           84 @job.update_progress(100)
           85 
           86 rescue ::SignalException, ::SystemExit
           87   raise $!
           88 rescue ::Exception => e
           89   WarVOX::Log.warn("Worker #{@job.id} #{@job.task} threw an exception: #{e.class} #{e} #{e.backtrace}")
           90   Job.where(id: @job.id).update_all({ error: "Exception: #{e.class} #{e}", status: 'error', completed_at: Time.now.utc })
           91 end