Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
WFH

Kirill Shevchenko kirillshevch

💻
WFH
View GitHub Profile
View signal_trap.rb
class App
attr_reader :queue
def initialize
@queue = []
end
def call
puts "App PID: #{Process.pid}"
publish
View signal_exception.rb
class App
attr_reader :queue
def initialize
@queue = []
end
def call
puts "App PID: #{Process.pid}"
publish
View retry.rb
class SomeClass
# ...
def execute
api_client.get(id)
rescue StandardError => e
@retries = @retries + 1
retries > RETRY_LIMIT ? raise(e) : retry
end
View sidekiq.rb
class WeatherUpdateJob
include Sidekiq::Worker
sidekiq_options retry: 3
def perform
Weather.new(data: BestWeatherAPI.get)
Weather.save!
end
end
View aws-errors.rb
module Aws::S3
module Errors
extend Aws::Errors::DynamicErrors
class BucketAlreadyExists < ServiceError
def initialize(context, message, data = Aws::EmptyStructure.new)
super(context, message, data)
end
end
class BucketAlreadyOwnedByYou < ServiceError
View fallback.rb
def get
Retriable.retriable do
# code here...
end
rescue StandardError => e
logger.error 'failed_request', error: e
end
View context.rb
# Will retry all exceptions
Retriable.with_context(:aws) do
# aws_call
end
# Will retry Mysql::DeadlockException
Retriable.with_context(:mysql) do
# write_to_table
end
View config.rb
Retriable.configure do |c|
c.contexts[:aws] = {
tries: 3,
on: [Aws::Errors::ServiceError, Errno::ECONNRESET]
}
c.contexts[:mysql] = {
tries: 10,
on: Mysql::DeadlockException
}
end
View retrieble.rb
require 'retriable'
class Api
# Use it in methods that interact with unreliable services
def get
Retriable.retriable do
# code here...
end
end
end
View retry.rb
class SomeClass
attr_reader :id, :retries
ApiError = Class.new(StandardError)
RETRY_LIMIT = 3
def initialize(id)
@id = id
@retries = 0