Skip to content

Instantly share code, notes, and snippets.

@sumitasok
Last active December 6, 2016 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumitasok/09ea010ed21b28d65ec201dbd05e1307 to your computer and use it in GitHub Desktop.
Save sumitasok/09ea010ed21b28d65ec201dbd05e1307 to your computer and use it in GitHub Desktop.
Log batch process
=begin
Usage:
[1, 4, 7, 2, 19].each do |i|
Logging::Block.start(:type, Logging::Logger) do |list|
list.item_with_uuid(i, {i: i}) do |item|
if i < 7
item.success({i: i})
else
item.error({i: i})
end
end
end
end
=end
module Logging
class Logging::Block
def self.start(logtype, logger)
yield Logging::Block.new(logtype, logger)
end
def initialize(logtype, logger)
@logtype = logtype
@logger = logger
@logblock_startat = Time.now
end
def item_with_uuid(uuid, input)
item = Item.new(input, @logger, @logblock_startat, @logtype, uuid)
begin
yield item
rescue StandardException => e
item.error({message: e})
rescue
item.error({message: "Unknown exception"})
end
end
def item(input)
uuid = SecureRandom.uuid
item = Item.new(input, @logger, @logblock_startat, @logtype, uuid)
begin
yield item
rescue StandardException => e
item.error({message: e})
rescue
item.error({message: "Unknown Exception"})
end
end
end
class Item
def initialize(input, logger, block_start_at, logtype, uuid)
@input = input
@logger = logger
@block_start_at = block_start_at
@logtype = logtype
@uuid = uuid
@logger.start(@logtype, @block_start_at, @uuid, @input)
end
def success(response)
@logger.success(@logtype, @block_start_at, @uuid, response)
end
def error(response)
@logger.error(@logtype, @block_start_at, @uuid, response)
end
end
class Logger
def self.start(logtype, block_start_at, uuid, input)
Rails.logger.info("Logger: #{logtype}::#{uuid}::init, Block started at: #{block_start_at}, process start at #{Time.now}, Input #{input}")
Logger
end
def self.success(logtype, block_start_at, uuid, response)
Rails.logger.info("Logger: #{logtype}::#{uuid}::success, Block started at: #{block_start_at}, process status at #{Time.now}, Response #{response}")
end
def self.error(logtype, block_start_at, uuid, response)
Rails.logger.error("Logger: #{logtype}::#{uuid}::error, Block started at: #{block_start_at}, process status at #{Time.now}, Response #{response}")
end
end
end
CronLog.start(:type) do |cl|
cl.item(:uuid, :input) do |item|
item.success(:response)
item.error(:error)
end
end
class CronLog
def initialize(cron_type)
@cron_type = cron_type
@cron_started_at = Time.now()
end
def self.start(cron_type)
ct = CronLog.new(cron_type)
yield ct
end
def item(uuid, input)
item = Item.new(@cron_type, @cron_started_at, @cron_item_started_at, uuid, input.to_yaml)
item.save
begin
yield item
rescue
item.error(e)
end
end
end
class Item
def success(response)
self.response = response.to_yaml
self.status = "SUCCESS"
self.crom_item_updated_at = Time.now()
self.save
end
def error(response)
self.response = response.to_yaml
self.status = "ERROR"
self.crom_item_updated_at = Time.now()
self.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment