Sha256: 0e11da32b353bd4ed2b1d72e48d6358b0999e1a4c98e81a9c12756e493dec495

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

#
# Run in cron
#
module Cron
  #
  # Sync configuration with switchboard
  #
  class SwitchboardSyncConfiguration < Job
    cron_tab_entry :hourly

    #
    # Only run in environments where switchboard is configured
    #
    def self.valid_environment?
      SystemConfiguration.switchboard_configured?
    end

    #
    # Cycle through all configuration keys
    #
    def perform
      Rails.cache.reconnect
      RestClient.get(switchboard_url,
                     ACCESS_TOKEN: SystemConfiguration.switchboard_stack_api_token,
                     content_type: 'application/json') do |response, _request, _result, &block|
        case response.code
        when 200
          json = JSON.parse(response.body)
          config = SystemConfiguration.configuration
          json['results'].each { |key, value| update_config(config, key, value) }
          config.switchboard_last_sync_at = Time.now.utc
          config.save!
        else
          App47Logger.log_error "Unable to fetch switchboard config, #{response.inspect}"
          response.return!(&block)
        end
      end
    end

    #
    # First see if it's updateable against system config, then see if it's in JobCronTab
    #
    def update_config(config, key, value)
      config.send("#{key}=", value) if config.respond_to?("#{key}=")
      return unless key.end_with?('_crontab')

      name = key.chomp('_crontab')
      tab = Cron::JobTab.from_string(name, value)
      if tab.present? && tab.valid?
        tab.save
        App47Logger.log_debug "Crontab #{name} updated with #{value}"
      else
        App47Logger.log_warn "Unable to update crontab #{name} updated with #{value}"
      end
    end

    #
    # Generate the switchboard URL
    #
    def switchboard_url
      [SystemConfiguration.switchboard_base_url,
       'stacks',
       SystemConfiguration.switchboard_stack_id,
       'items.json'].join('/')
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
web47core-0.6.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.6.2 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.6.1 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.6.0 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.5.5 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.5.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.5.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.5.2 lib/app/jobs/cron/switchboard_sync_configuration.rb