Class: ErpTechSvcs::DelayedJobs::NotificationJob

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_tech_svcs/delayed_jobs/notification_job.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotificationJob

Returns a new instance of NotificationJob.



8
9
10
# File 'lib/erp_tech_svcs/delayed_jobs/notification_job.rb', line 8

def initialize
  @priority = 1
end

Class Method Details

.schedule_job(schedule_at) ⇒ Object



44
45
46
# File 'lib/erp_tech_svcs/delayed_jobs/notification_job.rb', line 44

def self.schedule_job(schedule_at)
  Delayed::Job.enqueue(ErpTechSvcs::DelayedJobs::NotificationJob.new, @priority, schedule_at)
end

Instance Method Details

#performObject



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
# File 'lib/erp_tech_svcs/delayed_jobs/notification_job.rb', line 12

def perform
  unless Dir.exists?(File.join(Rails.root, 'log/delayed_jobs'))
    Dir.mkdir(File.join(Rails.root, 'log/delayed_jobs'))
  end

  logger = Logger.new(File.join(Rails.root,"log/delayed_jobs/#{Rails.env}-notifications_job.log"), "weekly")
  logger.level = Logger::INFO

  time = Benchmark.measure do
    begin
      Notification.where('current_state = ?', 'pending').each do |notification|
        notification.deliver_notification
      end

    rescue Exception => ex
      logger.error("#{Time.now}**************************************************")
      logger.error("Job Error: #{ex.message}")
      logger.error("Trace: #{ex.backtrace.join("\n")}")
      logger.error("*************************************************************")

      # email notification
      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier
    end
  end

  start_time = Chronic.parse(ErpTechSvcs::Config.notification_job_delay)
  Delayed::Job.enqueue(ErpTechSvcs::DelayedJobs::NotificationJob.new, @priority, start_time)

  #update job tracker
  JobTracker.job_ran('Notification Job', self.class.name, ("(%.4fs)" % time.real), start_time)
end