Module: ThecoreBackgroundJobs

Defined in:
lib/thecore_background_jobs.rb,
lib/thecore_background_jobs/engine.rb,
lib/thecore_background_jobs/version.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

VERSION =
"#{`git describe --tags $(git rev-list --tags --max-count=1)`.chomp}"

Class Method Summary collapse

Class Method Details

.email_setupObject

Your code goes here…



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/thecore_background_jobs.rb', line 10

def self.email_setup
  # Fill variables with the correct values
  domain = ThecoreSettings::Setting.where(ns: :smtp, key: :domain).first.raw
  address = ThecoreSettings::Setting.where(ns: :smtp, key: :address).first.raw
  port = ThecoreSettings::Setting.where(ns: :smtp, key: :port).first.raw.to_i
  enable_starttls_auto = ThecoreSettings::Setting.where(ns: :smtp, key: :enable_starttls_auto).first.raw == "true"
  user_name = ThecoreSettings::Setting.where(ns: :smtp, key: :user_name).first.raw
  password = ThecoreSettings::Setting.where(ns: :smtp, key: :password).first.raw
  authentication = ThecoreSettings::Setting.where(ns: :smtp, key: :authentication).first.raw

  delivery_options = { 
    domain: domain, 
    address: address, 
    port: port, 
    enable_starttls_auto: enable_starttls_auto
  }
  # Other Options
  delivery_options.merge!({user_name: user_name}) unless user_name.blank?
  delivery_options.merge!({password: password}) unless password.blank?
  delivery_options.merge!({authentication: authentication}) unless authentication.blank? || authentication == "none"
  delivery_options
end

.update_cron_schedulesObject



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
# File 'lib/thecore_background_jobs.rb', line 33

def self.update_cron_schedules
  # {
  #  "AlertForLongTermStockJob"=>{"cron"=>"0 0 9 * * *", "queue"=>"notset_default", "description"=>"This job Alerts a reference email for items in stock for too long", "enabled"=>true, "class"=>"AlertForLongTermStockJob"}, 
  #  "ScheduleEmployeeReminderForInStockParcelsJob"=>{"cron"=>"0 0 8,14 * * *", "queue"=>"notset_default", "description"=>"This job checks periodically for new received parcels", "enabled"=>true, "class"=>"ScheduleEmployeeReminderForInStockParcelsJob"}
  # }
  begin
    Sidekiq.get_all_schedules.each_pair do |key, config|
      Rails.logger.info "Key: #{key}, Config: #{config}"
      schedule = "cron_for_#{key.underscore}"
      setting = ThecoreSettings::Setting.where(ns: "schedules", key: schedule).pluck(:raw).first
      Rails.logger.info "Setting is #{schedule} = #{setting}"
      # Installing initial sidekiq.yml configuration if setting is not present
      if setting.blank?
        Rails.logger.info "Setting #{schedule} doesn't exist, creating based on current value: #{config["cron"]}"
        ThecoreSettings::Setting.create(ns: "schedules", key: schedule, raw: config["cron"])
        # Settings.ns("schedules").send("#{schedule}=", config["cron"])
      elsif config["cron"].squeeze(" ").strip != setting.squeeze(" ").strip
        # If we have a setting and is different from the currently loaded, then replace it 
        # in scheduler configuration
        Rails.logger.info "Setting #{schedule} exists: #{setting}"
        Sidekiq.set_schedule(key, { cron: setting.squeeze(" ").strip, queue: "#{ENV["COMPOSE_PROJECT_NAME"]}_default", class: key })
        Rails.logger.info "Reloading schedules"
        SidekiqScheduler::Scheduler.instance.reload_schedule!
      end
    end 
  rescue => exception
    Rails.logger.info "Thecore Background Jobs: REDIS not reachable:"
    Rails.logger.info exception.message
  end
end