Sha256: 5a633e91d515b418679c34a74e4d9b994191cbd877cbeaef7e0ed8f12f28c2b7

Contents?: true

Size: 1.82 KB

Versions: 22

Compression:

Stored size: 1.82 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.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

22 entries across 22 versions & 1 rubygems

Version Path
web47core-0.4.5 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.4.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.4.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.4.2 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.4.0 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.3.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.3.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.3.2 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.3.1 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.3.0 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.11 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.10 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.9 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.8 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.7 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.6 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.5 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-0.1.2 lib/app/jobs/cron/switchboard_sync_configuration.rb