Class: Sicily::BatchProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sicily/batch_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ BatchProcessor

Returns a new instance of BatchProcessor.



9
10
11
12
13
# File 'lib/sicily/batch_processor.rb', line 9

def initialize(files)
  @files = files
  @pool = Concurrent::FixedThreadPool.new(Sicily.config.num_thread_pool)
  @latch = Concurrent::CountDownLatch.new(@files.size)
end

Instance Method Details

#notify_beginningObject



23
24
25
# File 'lib/sicily/batch_processor.rb', line 23

def notify_beginning
  Util::NotificationUtil.notify_beginning(@files)
end

#notify_doneObject



55
56
57
# File 'lib/sicily/batch_processor.rb', line 55

def notify_done
  Util::NotificationUtil.notify_done(@files)
end

#notify_done_when_finishedObject



40
41
42
43
44
45
# File 'lib/sicily/batch_processor.rb', line 40

def notify_done_when_finished
  @pool.post do
    @latch.wait
    notify_done
  end
end

#process_added_file(file, &user_rule_block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sicily/batch_processor.rb', line 47

def process_added_file(file, &user_rule_block)
  file_processor = FileProcessor.new(file)
  file_processor.instance_eval(&user_rule_block)
  file_processor.info 'Done'
rescue Exception => e
  Sicily.logger.error e.inspect
end

#process_each_file_in_thread(file, &user_rule_block) ⇒ Object



33
34
35
36
37
38
# File 'lib/sicily/batch_processor.rb', line 33

def process_each_file_in_thread(file, &user_rule_block)
  @pool.post do
    process_added_file(file, &user_rule_block)
    @latch.count_down
  end
end

#process_files_in_thread(&user_rule_block) ⇒ Object



27
28
29
30
31
# File 'lib/sicily/batch_processor.rb', line 27

def process_files_in_thread(&user_rule_block)
  @files.each do |file|
    process_each_file_in_thread file, &user_rule_block
  end
end

#run(&user_rule_block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/sicily/batch_processor.rb', line 15

def run(&user_rule_block)
  return if @files.empty?

  notify_beginning
  process_files_in_thread(&user_rule_block)
  notify_done_when_finished
end