# encoding: utf-8 require 'sidekiq' module Mutx module Workers class AlertsWorker include Sidekiq::Worker sidekiq_options retry: false def perform(args) stringio = StringIO.new @alert = Alert.find(name: args['name']) status = nil begin PTY.spawn("ruby #{args['path']}") do |stdout, stdin, pid| begin stdout.each do |line| stringio.puts line end rescue Errno::EIO => e stringio.puts e.message stringio.puts e.backtrace ensure Process.wait pid status = $?.exitstatus end end rescue => e stringio.puts e.message stringio.puts e.backtrace raise e ensure @alert.update_status(status, info: stringio.string) end end end end end