Skip to content

Instantly share code, notes, and snippets.

@ivawzh
Created December 1, 2017 00:06
Show Gist options
  • Save ivawzh/7a82bbc403ae46c4e1ac264372654e03 to your computer and use it in GitHub Desktop.
Save ivawzh/7a82bbc403ae46c4e1ac264372654e03 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'config'
require 'rest-client'
require 'retriable'
require 'contract_repository'
require 'group_contracts_by_agency'
require 'tzinfo'
require 'monitoring'
require 'pp'
class AutoActivation
def self.run!
new.run!
end
def run!
LOGGER.info('Starting auto activation')
process_contracts_with_retries
LOGGER.info('Finished auto activation')
rescue => e
LOGGER.fatal("Contract Auto-Activation Failed after maximum number of retries with: #{e.class}: #{e.message}")
raise e
end
def process_contracts_with_retries
Retriable.retriable(retriable_config) do
process_contracts
end
end
def retriable_config
{
on: [RestClient::Exception],
base_interval: 60,
multiplier: 2,
max_interval: 960,
tries: 10,
max_elapsed_time: 86_400,
on_retry: log_retry
}
end
def process_contracts
to_activate = ContractRepository.to_activate(today_in_melbourne)
LOGGER.info("Found #{to_activate.count} contracts to activate")
to_deactivate = ContractRepository.to_deactivate(today_in_melbourne)
LOGGER.info("Found #{to_deactivate.count} contracts to deactivate")
groups = GroupContractsByAgency.call(
to_deactivate: to_deactivate,
to_activate: to_activate
).sort_by(&:agent_desktop_id)
File.open("contracts.txt","w") do |f|
PP.pp(groups,f)
end
end
def log_retry
proc do |exception, try, elapsed_time, next_interval|
LOGGER.warn(
[
"#{exception.class}: '#{exception.message}'", "#{try} tries in #{elapsed_time} seconds",
"#{next_interval} seconds until the next try"
].join(' - ')
)
response = RestClient.get(ENVied.AGENT_DESKTOP_URI)
LOGGER.warn("Health Check for #{ENVied.AGENT_DESKTOP_URI} got #{response.code}")
end
end
def today_in_melbourne
@today ||= TZInfo::Timezone.get('Australia/Melbourne').now.to_date
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment