Skip to content

Instantly share code, notes, and snippets.

@zaru
Last active April 16, 2020 13:14
Show Gist options
  • Save zaru/ef495dd21106fbc0cc6b66664574ccc4 to your computer and use it in GitHub Desktop.
Save zaru/ef495dd21106fbc0cc6b66664574ccc4 to your computer and use it in GitHub Desktop.
class HardWorker
include Sidekiq::Worker
def perform
random = Random.new
init_val = random.rand(1..99999)
apeend_val = random.rand(1..99999)
Hoge.piyo({init_val: init_val, apeend_val: apeend_val})
p "HardWorker perform args: #{init_val} / #{apeend_val} => Hoge.data is #{Hoge.data}"
end
end
class Hoge
class << self
attr_accessor :data
def piyo(data)
# ここで初期化する
@data = {
init_val: data[:init_val]
}
# 適当に重い処理
5.times{ |i| BCrypt::Password.create(i.to_s) }
# ここでハッシュにデータを追加する
# すると、1番最後に初期化されたものが Sidekiq worker で利用され、
# 重い処理をし終えた順に、それぞれの追加キーがセットされて処理が継続する
@data.merge!(append_val: data[:apeend_val])
end
end
end
10.times { HardWorker.perform_async }
"HardWorker perform args: 68785 / 78839 now: {:init_val=>90834, :append_val=>78839}"
"HardWorker perform args: 14057 / 99365 now: {:init_val=>90834, :append_val=>99365}"
"HardWorker perform args: 14573 / 16901 now: {:init_val=>90834, :append_val=>16901}"
"HardWorker perform args: 87319 / 70622 now: {:init_val=>90834, :append_val=>70622}"
"HardWorker perform args: 15643 / 70140 now: {:init_val=>90834, :append_val=>70140}"
"HardWorker perform args: 90834 / 7108 now: {:init_val=>90834, :append_val=>7108}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment