class MyEventMachineDojin

Public Class Methods

new(args={ }) click to toggle source
Calls superclass method MyMachine::new
# File lib/lib/machine.rb, line 43
def initialize(args={ })
  super(args)
  @args = args
  @args[:start] ||= 3000
  @args[:stop] ||= 3100
  @args[:concurrency] ||= 10
  @args[:savedir] ||= "#{ENV['HOME']}/Downloads/jpg"
  @endbooks = []
  require 'rubygems'
  require 'eventmachine'
  @connection_count = 0
  require 'thread'
  @connection_que = Queue.new
  @list = []
  @gaman = 0
end

Public Instance Methods

connection_count!() click to toggle source
# File lib/lib/machine.rb, line 82
def connection_count!
  @connection_que.push(:t)
end
connection_end!() click to toggle source
# File lib/lib/machine.rb, line 86
def connection_end!
  @connection_que.pop
end
connection_exceed?() click to toggle source
# File lib/lib/machine.rb, line 78
def connection_exceed?
  @args[:concurrency] <= @connection_que.size
end
go() click to toggle source

EventMachine用の定義

# File lib/lib/machine.rb, line 61
def go
  EM.run do
    EM.add_periodic_timer(0.00001) do
      print "."
      EM.stop if should_stop_machine?
      if !connection_exceed?
        unless @queue.empty?
          job = @queue.pop
          job.run if job
        end
      end
    end
  end
  p @endbooks.uniq!.sort if @debug
  puts "End of fetch".green.bold
end
savecontent(path) click to toggle source
# File lib/lib/machine.rb, line 90
def savecontent(path)
  @list <<  path
end
write() click to toggle source
# File lib/lib/machine.rb, line 94
def write
  open("#{@args[:savedir]}/emit.txt" ,"w") do |io|
    io.write('["')
    io.write(@list.sort.join('","'))
    io.write('"]')
  end
end

Private Instance Methods

setupjobs() click to toggle source
# File lib/lib/machine.rb, line 104
def setupjobs
  (0..100).each do |p|
    (@args[:start]..@args[:stop]).each do |b|
      job = MyJobDojinEventMachine.new(
        :savedir => @args[:savedir],
        :server => '1patu.net',
        :book => b,
        :page => p,
        :machine => self,
        :debug  => @debug,
        )
      @queue.push job
    end
  end
end
should_stop_machine?() click to toggle source

Machineは終了すべきか?

# File lib/lib/machine.rb, line 121
def should_stop_machine?
  @gaman += 1  if @queue.size < 10
  if @gaman > 200
    write if @queue.size == 0
    return true
  end
end