Skip to content

Commit

Permalink
Try to Reduce Watch CPU Usage
Browse files Browse the repository at this point in the history
This commit adds a `lazy mode` to `watch` and changes the default options hash that `watch` passes to `Listen`

`Watch` runs in `lazy mode` by default. In this mode, `watch` only fires the following rules when there are added or removed files, ignoring file modification. The `lazy mode` behavior can be overridden by giving options with `{ lazy : false}`.

When there's no options given, `watch` will use the default options, which will check for changes every 10 seconds and ignore the downloading files.

Try to resolve maid#149
  • Loading branch information
songchenwen committed Apr 24, 2015
1 parent 34cbbb5 commit a67742c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/maid/watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ class Maid::Watch

def initialize(maid, path, options = {}, &rules)
@maid = maid
@options = options
if options.nil?
@lazy = true
@options = { wait_for_delay: 10,
ignore: [/\.crdownload$/, /\.download$/, /\.aria2$/, /\.td$/, /\.td.cfg$/, /\.part$/] }
else
@lazy = options.delete(:lazy) { |key| true }
@options = options
end
@logger = maid.logger # TODO: Maybe it's better to create seperate loggers?
@path = File.expand_path(path)
initialize_rules(&rules)
Expand All @@ -15,7 +22,9 @@ def initialize(maid, path, options = {}, &rules)
def run
unless rules.empty?
@listener = Listen.to(path, @options) do |modified, added, removed|
follow_rules(modified, added, removed)
if !@lazy || added.any? || removed.any?
follow_rules(modified, added, removed)
end
end
@listener.start
end
Expand Down

0 comments on commit a67742c

Please sign in to comment.