Sha256: 4d74814845701454fe389e0816d1ad34e079b7bf37d5fa2f2b519f1aa16d2b75

Contents?: true

Size: 1.85 KB

Versions: 54

Compression:

Stored size: 1.85 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 execute
      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

54 entries across 54 versions & 1 rubygems

Version Path
web47core-1.1.16 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.15 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.14 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.13 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.12 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.11 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-2.0.1 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-2.0.0 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.10 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.9 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.8 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.7 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.6 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.5 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.2 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.1 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.1.0 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-1.0.18 lib/app/jobs/cron/switchboard_sync_configuration.rb