-
-
Save michelson/013c7c840de541255ba7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 |
This file contains hidden or 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
| runner = PreyReportCli::Runner.new | |
| runner.threads_num = 5 | |
| runner.run_times = 5 | |
| runner.caller = PreyReportCli::Report.new | |
| runner.run |
This file contains hidden or 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 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 |
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.
@michelson Please take a look at this: https://gist.github.com/jdantonio/baf400950605e7e01f36
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll post a gist of an updated script in a few minutes, but here are a few notes:
include Concurrent::Asyncmakes 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_terminationis 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 callpool.shutdownorpool.kill, neither of which you are calling.pool.wait_for_terminationwon't work in this context. Calling#shutdownand#killcause the pool to throw away unprocessed jobs. This is not what you want.