Class: ProxyDaemon::Daemon
- Inherits:
-
Object
- Object
- ProxyDaemon::Daemon
- Defined in:
- lib/proxy_daemon/daemon.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
Returns the value of attribute list.
Instance Method Summary collapse
- #command(cmd, pipe, *params) ⇒ Object
-
#initialize(script, options) ⇒ Daemon
constructor
A new instance of Daemon.
- #listen(pipe, try = 0) ⇒ Object
- #start(&block) ⇒ Object
- #worker ⇒ Object
Constructor Details
#initialize(script, options) ⇒ Daemon
Returns a new instance of Daemon.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/proxy_daemon/daemon.rb', line 10 def initialize(script, ) @script = script @proxies = (([:proxies].map { |proxy| proxy.gsub /\s+/, ' ' }) || []).shuffle @urls = [:urls] || [] @workers = [([:workers] || 10), @urls.count].min @tries = [:tries] || 4 @threads = [] @list = {} @semaphore = Mutex.new end |
Instance Attribute Details
#list ⇒ Object
Returns the value of attribute list.
8 9 10 |
# File 'lib/proxy_daemon/daemon.rb', line 8 def list @list end |
Instance Method Details
#command(cmd, pipe, *params) ⇒ Object
22 23 24 25 |
# File 'lib/proxy_daemon/daemon.rb', line 22 def command(cmd, pipe, *params) Thread.current[:command] = cmd pipe.syswrite("#{cmd} #{params.join(' ')}\n") end |
#listen(pipe, try = 0) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/proxy_daemon/daemon.rb', line 27 def listen(pipe, try = 0) answer = (pipe.sysread(30) || '').strip if answer == 'proxy' raise "Broken url: #{Thread.current[:url]}" if try > @tries proxy = getProxy log "Choosing new proxy: #{(proxy || 'nil').yellow}", pipe command :proxy, pipe, proxy command :url, pipe, Thread.current[:url] listen pipe, try+1 elsif answer.empty? raise "Empty answer from worker process" elsif answer == 'timeout' || answer == 'error' raise "Process: '#{answer}'" elsif Thread.current[:command] == :url if (buf = answer.match(/^set (.+?)[\s]*:[\s]*?(.+)$/i)); @list[buf[1]] = buf[2] end log "Process: #{Thread.current[:url].cyan}: '#{answer.green}'", pipe else log "Answer: #{answer}".green, pipe end end |
#start(&block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/proxy_daemon/daemon.rb', line 100 def start(&block) @block = block if block_given? begin puts "[main] Starting " + "#{@workers}".yellow + " workers:" @workers.times { |i| @threads << Thread.new(&(->{worker})) } @threads.each { |t| t.join } rescue Interrupt => e puts "[main] Interrupted by user".yellow end end |
#worker ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/proxy_daemon/daemon.rb', line 50 def worker IO.popen("#{@script}", 'r+') { |p| if (@script == '-' && p.nil?) # child process if @block.nil? $stderr.puts "[#{Process.pid}]".magenta + ' Empty block for parsing content'.red Kernel.exit! end worker = ProxyDaemon::Worker.new worker.call(&@block) else proxy = getProxy log "Starting loop with new proxy: ".green + "#{(proxy || 'nil').yellow}", p command :proxy, p, proxy begin loop do sleep(0.1) url = getUrl if url.nil? finished "Links are finished! exitting...".green, p command :exit, p break else log 'Urls count: ' + "#{@urls.length}".green + ", #{url.green}" Thread.current[:url] = url command :url, p, url listen p end end log "Finishing loop".green, p rescue Exception => e @semaphore.synchronize { log "Exception in main: " + "#{e..red}, '#{Thread.current[:url]}'".red, p command :exit, p #puts e.backtrace } end end } if @urls.length > 0 # @threads << Thread.new(&(->{worker})) # @threads.last.join worker end end |