Skip to content

Instantly share code, notes, and snippets.

@michelson

michelson/cli.rb Secret

Last active March 18, 2016 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelson/013c7c840de541255ba7 to your computer and use it in GitHub Desktop.
Save michelson/013c7c840de541255ba7 to your computer and use it in GitHub Desktop.
module ReportCli
class Runner
include Concurrent::Async
attr_accessor :threads_num, :run_times, :caller
def initialize
@conn = Config.connection
end
# TODO use TPoolexecutor to limit queue ,
# http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/ThreadPoolExecutor.html
def run
pool = Concurrent::FixedThreadPool.new(self.threads_num)
@instance = self
pool.post{
@instance.run_times.times{
@instance.caller.exec
}
}
pool.wait_for_termination
end
def call
puts "sending reports"
self.caller.send_report
end
end
end
runner = PreyReportCli::Runner.new
runner.threads_num = 5
runner.run_times = 5
runner.caller = PreyReportCli::Report.new
runner.run
module ReportCli
class Report
def initialize
@conn = Config.connection
end
def end_point
"/api/v2/devices/#{Config.device_key}/reports.json"
end
def exec
payload = {}
payload[:screenshot] = Faraday::UploadIO.new("./#{shuffled_images}.jpg", 'image/jpeg')
@conn.post end_point, payload
end
def shuffled_images
["mat","pic","van"].shuffle.first
end
end
end
@jdantonio
Copy link

I'll post a gist of an updated script in a few minutes, but here are a few notes:

  • include Concurrent::Async makes the class inherently asynchronous. It will allow its methods to be run in the background on the global thread pool and will ensure that they are synchronized and run in order. It is not necessary in this context (nor are you actually using it).
  • pool.wait_for_termination is not used to wait for all jobs to finish, it's used for waiting until the pool shuts down. It will block forever unless you call pool.shutdown or pool.kill, neither of which you are calling.
  • pool.wait_for_termination won't work in this context. Calling #shutdown and #kill cause the pool to throw away unprocessed jobs. This is not what you want.
  • I would recommend that you do not limit the number of threads in the pool. Our thread pools dynamically grow and shrink as needed. When doing a lot of IO it's generally much more efficient to let the pool itself decide how big it needs to grow.

@jdantonio
Copy link

Also, your Report class may not be thread safe. I don't know what you are storing in Config.connection but that needs to be thread safe for this to work. If it isn't thread safe, your code isn't thread safe. I'm going to assume that it is thread safe.

@jdantonio
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment