This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
domain_ids = [1, 2, 3] | |
option = domain_ids.map{|domain_id| "-q hoge-#{domain_id},1"}.join(' ') | |
cli = Sidekiq::CLI.instance | |
cli.parse(option.split(' ')) | |
cli.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sidekiq::Client.enqueue(HogeWorker.domainify(1), param1) | |
Sidekiq::Client.enqueue(HogeWorker.domainify(2), param2) | |
Sidekiq::Client.enqueue(HogeWorker.domainify(3), param3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Crawler | |
class DomainSpecificWorker | |
include Sidekiq::Worker | |
def self.domainify(domain_id) | |
klass_name = "#{self.name.split('::').last}_#{domain_id}" | |
unless Crawler.const_defined?(klass_name) | |
Crawler.const_set(klass_name.to_sym, self.clone) | |
klass = Crawler.const_get(klass_name.to_sym) | |
queue = klass.class_variable_get(:@@queue).to_s + "-#{domain_id}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/sidekiq/fetch.rb:101 | |
def retrieve_work | |
work = Sidekiq.redis { |conn| conn.brpop(*queues_cmd) } | |
UnitOfWork.new(*work) if work | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/resque/worker.rb:262 | |
queues.each do |queue| | |
log! "Checking #{queue}" | |
if job = Resque.reserve(queue) # 最終的にはLPOP(http://redis.io/commands/lpop)が呼ばれる | |
log! "Found job on #{queue}" | |
return job | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Logger.measure_execution_time(1) do | |
# 怪しそうな処理1 | |
end | |
Logger.measure_execution_time(2) do | |
# 怪しそうな処理2 | |
end | |
Logger.measure_execution_time(3) do | |
# 怪しそうな処理3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fluent-logger' | |
class Logger | |
def self.measure_execution_time(type) | |
before_time = Time.now | |
res = yield | |
after_time = Time.now | |
elapsed_time = after_time - before_time | |
data = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 本に載っていたコードは大体Qiitaに書いてしまったので、趣味で書いてみた円周率の計算コードを幾つか | |
-- 効率とかは気にしていないので収束はかなり遅い | |
-- ライプニッツの公式 | |
4 * (sum (take 2000 (zipWith (/) (iterate negate 1) [1,3..]))) | |
-- ウォリス積 | |
2 * (product $ take 2000 $ zipWith (/) (concat $ map (replicate 2) [2, 4..]) (tail.concat $ map (replicate 2) [1, 3..])) | |
-- バーゼル問題(zeta(2) = π^2 / 6) オイラーさんマジ天才 |
NewerOlder