Sha256: 209bf7b64edea580ab4188e5e79dd62a573eb3140cab35b3ce7a962b46b55520

Contents?: true

Size: 1.88 KB

Versions: 33

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?
    rescue StandardError
      false
    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

33 entries across 33 versions & 1 rubygems

Version Path
web47core-3.2.20 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.19 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.18 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.17 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.16 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.15 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.14 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.13 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.12 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.9 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.8 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.7 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.6 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.5 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.4 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-2.2.20 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-2.2.19 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.3 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-3.2.2 lib/app/jobs/cron/switchboard_sync_configuration.rb
web47core-2.2.15 lib/app/jobs/cron/switchboard_sync_configuration.rb