~pixelinc/gmod-checker.cr

409ad399b71ae5b87f4d6d7fe319cb06afb35398 — PixeL 5 months ago c28f100
Use new Log module
2 files changed, 9 insertions(+), 9 deletions(-)

M src/job_controller.cr
M src/steam.cr
M src/job_controller.cr => src/job_controller.cr +5 -5
@@ 7,14 7,14 @@ module JobController
  TIMEOUT = 0

  @@jobs = {} of Nonce => Job
  @@logger : Logger? = nil
  @@logger : Log? = nil

  def logger=(@@logger : Logger)
  def logger=(@@logger : Log)
  end

  def create(size : Int32, &block : Job ->) : Nonce
    nonce = generate_nonce
    @@logger.try &.info "[JobController : CREATE] #{nonce} (size: #{size})"
    @@logger.try &.info { "[JobController : CREATE] #{nonce} (size: #{size})" }
    job = Job.new(size)
    @@jobs[nonce] = job
    spawn do


@@ 30,7 30,7 @@ module JobController
  def cleanup(nonce)
    sleep TIMEOUT
    @@jobs.delete(nonce)
    @@logger.try &.info "[JobController : DELETE] #{nonce}"
    @@logger.try &.info { "[JobController : DELETE] #{nonce}" }
  end

  # TODO: Blocking access to jobs with mutex?


@@ 39,7 39,7 @@ module JobController

  def dispatch(nonce : Nonce, &block : Job::PlayerResult | Job::BatchPlayers | Job::Error ->)
    job = @@jobs[nonce]
    @@logger.try &.info "[JobController : DISPATCH] #{nonce} (size: #{job.size})"
    @@logger.try &.info { "[JobController : DISPATCH] #{nonce} (size: #{job.size})" }
    job.each_result { |r| block.call(r) }
    # TODO: Move into create in its own fiber
    cleanup(nonce)

M src/steam.cr => src/steam.cr +4 -4
@@ 16,16 16,16 @@ end
class Steam::Client
  BASE_URL = "http://api.steampowered.com"

  def initialize(@api_key : String, @logger : Logger? = nil)
  def initialize(@api_key : String, @logger : Log? = nil)
  end

  def request(endpoint : String)
    @logger.try &.info "[Steam : HTTP OUT] #{endpoint}"
    @logger.try &.info { "[Steam : HTTP OUT] #{endpoint}" }
    # TODO: Implement some sort of ratelimiter here or something.
    response = HTTP::Client.get "#{BASE_URL}#{endpoint}&key=#{@api_key}"
    @logger.try &.info "[Steam : HTTP IN] #{response.status_code} #{response.status_message}"
    @logger.try &.info { "[Steam : HTTP IN] #{response.status_code} #{response.status_message}" }
    raise "Steam API request failed: #{response.inspect}" unless response.success?
    @logger.try { |l| l.debug "[HTTP IN] #{response.body}" if l.debug? }
    @logger.try { |l| l.debug { "[HTTP IN] #{response.body}" } if l.level == Log::Severity::Debug }
    response.body
  end