Module: Resque::Plugins::AppInstanceJob

Defined in:
lib/resque/plugins/app_instance_job.rb

Instance Method Summary collapse

Instance Method Details

#around_perform(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/resque/plugins/app_instance_job.rb', line 4

def around_perform(args)
  case args.class.name
  when "Array"
    if args.first.is_a?(Hash)
      data = args.first.merge({:worker_class => self.to_s})
    else
      data = {:worker_class => self.to_s, :args => args.to_json}
    end
  when "Hash"
    data = args.merge({:worker_class => self.to_s})
  end

  if Rails.logger.is_a?(Ougai::Logger) && !Rails.env.development?
    Rails.logger.with_fields = { zecs_service: data.transform_keys(&:to_sym), trace_id: SecureRandom.uuid }
  end

  begin
    connection_count ||= 0
    @appinstance = ZuoraConnect::AppInstance.find(args['app_instance_id'].to_i)
    job_start_log

    @appinstance.new_session(holding_pattern: true)
  rescue ActiveRecord::RecordNotFound => exception
    # If we can't find app_instance let make sure we cleanup
    if Redis.current.zscore("AppInstance:Deleted", args['app_instance_id'].to_i).present?
      Rails.logger.info("No instance found, purge")
      ZuoraConnect::AppInstance.new(id: args['app_instance_id'].to_i).prune_data
      return
    else
      raise
    end
  rescue ActiveRecord::StatementInvalid => exception
    if (connection_count += 1) <= 3 &&
      (
        exception.message.include?("PG::UnableToSend: no connection to the server") ||
        exception.message.include?("PG::ConnectionBad: PQconsumeInput()") ||
        exception.message.include?("PG::ConnectionBad: PQsocket()") ||
        exception.message.include?("PG::UnableToSend: SSL SYSCALL")
      )

      sleep 30
      ActiveRecord::Base.establish_connection
      retry
    else
      raise
    end
  rescue PG::ConnectionBad => exception
    Rails.logger.warn("Bad Connection Restart", exception)
    Resque.enqueue_to(self.job.queue, self.job.payload['class'], args)
    return
  rescue ZuoraConnect::Exceptions::ConnectCommunicationError => exception
    Rails.logger.warn("Enqueue Job Again ~ 2 mins", exception)
    @appinstance.queue_pause(time: 2.minutes.to_i)
    Resque.enqueue_to(self.job.queue, self.job.payload['class'], args)
    return
  rescue Net::ReadTimeout, Net::OpenTimeout, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => exception
    Rails.logger.warn("Enqueue Job Again ~ 2 mins", exception)
    @appinstance.queue_pause(time: 2.minutes.to_i)
    Resque.enqueue_to(self.job.queue, self.job.payload['class'], args)
    return
  end
  yield
rescue
  raise
ensure
  @appinstance.cache_app_instance if defined?(@appinstance)
  Rails.logger.flush if Rails.logger.methods.include?(:flush)
end

#job_start_logObject



73
74
75
# File 'lib/resque/plugins/app_instance_job.rb', line 73

def job_start_log
  Rails.logger.info('Starting job')
end