listen 3.2.1 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +110 -65
- data/lib/listen.rb +15 -20
- data/lib/listen/adapter.rb +6 -8
- data/lib/listen/adapter/base.rb +17 -29
- data/lib/listen/adapter/bsd.rb +3 -1
- data/lib/listen/adapter/config.rb +2 -0
- data/lib/listen/adapter/darwin.rb +10 -12
- data/lib/listen/adapter/linux.rb +7 -4
- data/lib/listen/adapter/polling.rb +2 -0
- data/lib/listen/adapter/windows.rb +6 -4
- data/lib/listen/backend.rb +2 -0
- data/lib/listen/change.rb +5 -3
- data/lib/listen/cli.rb +3 -2
- data/lib/listen/directory.rb +4 -2
- data/lib/listen/event/config.rb +8 -18
- data/lib/listen/event/loop.rb +41 -65
- data/lib/listen/event/processor.rb +31 -25
- data/lib/listen/event/queue.rb +4 -5
- data/lib/listen/file.rb +9 -2
- data/lib/listen/fsm.rb +69 -71
- data/lib/listen/listener.rb +24 -23
- data/lib/listen/listener/config.rb +2 -0
- data/lib/listen/logger.rb +27 -24
- data/lib/listen/options.rb +3 -1
- data/lib/listen/queue_optimizer.rb +2 -0
- data/lib/listen/record.rb +16 -2
- data/lib/listen/record/entry.rb +3 -1
- data/lib/listen/record/symlink_detector.rb +2 -0
- data/lib/listen/silencer.rb +2 -0
- data/lib/listen/silencer/controller.rb +2 -0
- data/lib/listen/thread.rb +52 -0
- data/lib/listen/version.rb +3 -1
- metadata +7 -23
- data/lib/listen/internals/thread_pool.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e11b5e927f693205d422a0ea65147a53f65b08acde3a5dec7b370ac3f66b9707
|
4
|
+
data.tar.gz: 29bf36d64485cbbdc806451381d402067796430d8a5cd6ca1903a394d73d2c84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18fa31e48db9c6b9f99108e2dabed785ba15409d0d79b3b2bbcc47be0ea6c55b82418e44ce48639b32fe137fa0871623b8e593c1ffbaeee18fa4847c484cd3d9
|
7
|
+
data.tar.gz: a2cec7e23abba56a0b47e2f736d8fe37cb01a574da6a54049156eace74af67cf3783f2cbded1f7a2778ae1e26bbc75342144f2c16b05a30ced06ad7f91fa4e9e
|
data/README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
## IMPORTANT: If you cannot install Listen (e.g. on Travis/CI builds), [a workaround is here](https://github.com/guard/listen/wiki/Ruby-version-requirements)
|
2
|
-
|
3
|
-
:exclamation: Listen is currently accepting more maintainers. Please [read this](https://github.com/guard/guard/wiki/Maintainers) if you're interested in joining the team.
|
4
|
-
|
5
1
|
# Listen
|
6
2
|
|
7
|
-
|
3
|
+
The `listen` gem listens to file modifications and notifies you about the changes.
|
4
|
+
|
5
|
+
:exclamation: `Listen` is currently accepting more maintainers. Please [read this](https://github.com/guard/guard/wiki/Maintainers) if you're interested in joining the team.
|
8
6
|
|
9
|
-
|
7
|
+
[](https://github.com/guard/listen/actions?workflow=Development)
|
8
|
+
[](http://badge.fury.io/rb/listen)
|
9
|
+
[](https://codeclimate.com/github/guard/listen)
|
10
|
+
[](https://coveralls.io/r/guard/listen)
|
10
11
|
|
11
12
|
## Features
|
12
13
|
|
@@ -15,7 +16,7 @@ The Listen gem listens to file modifications and notifies you about the changes.
|
|
15
16
|
* You can watch multiple directories.
|
16
17
|
* Regexp-patterns for ignoring paths for more accuracy and speed
|
17
18
|
* Increased change detection accuracy on OS X HFS and VFAT volumes.
|
18
|
-
*
|
19
|
+
* Continuous Integration: tested on selected Ruby environments via [Github Workflows](https:///github.com/guard/listen/master/.github/workflows).
|
19
20
|
|
20
21
|
## Issues / limitations
|
21
22
|
|
@@ -24,37 +25,81 @@ The Listen gem listens to file modifications and notifies you about the changes.
|
|
24
25
|
* Symlinked directories pointing within a watched directory are not supported ([#273](https://github.com/guard/listen/pull/273)- see [Duplicate directory errors](https://github.com/guard/listen/wiki/Duplicate-directory-errors)).
|
25
26
|
* No directory/adapter-specific configuration options.
|
26
27
|
* Support for plugins planned for future.
|
27
|
-
* TCP functionality was removed in
|
28
|
+
* TCP functionality was removed in `listen` [3.0.0](https://github.com/guard/listen/releases/tag/v3.0.0) ([#319](https://github.com/guard/listen/issues/319), [#218](https://github.com/guard/listen/issues/218)). There are plans to extract this feature to separate gems ([#258](https://github.com/guard/listen/issues/258)), until this is finished, you can use by locking the `listen` gem to version `'~> 2.10'`.
|
28
29
|
* Some filesystems won't work without polling (VM/Vagrant Shared folders, NFS, Samba, sshfs, etc.).
|
29
30
|
* Specs suite on JRuby and Rubinius aren't reliable on Travis CI, but should work.
|
30
31
|
* Windows and \*BSD adapter aren't continuously and automatically tested.
|
31
32
|
* OSX adapter has some performance limitations ([#342](https://github.com/guard/listen/issues/342)).
|
32
|
-
*
|
33
|
+
* FreeBSD users need patched version of rb-kqueue (as of 2020/11). See #475 for the issue, mat813/rb-kqueue#12 for the patch, and Bug 250432 in bugzilla.
|
33
34
|
* Listeners do not notify across forked processes, if you wish for multiple processes to receive change notifications you must [listen inside of each process](https://github.com/guard/listen/issues/398#issuecomment-223957952).
|
34
35
|
|
35
36
|
Pull requests or help is very welcome for these.
|
36
37
|
|
37
38
|
## Install
|
38
39
|
|
39
|
-
The simplest way to install
|
40
|
+
The simplest way to install `listen` is to use [Bundler](http://bundler.io).
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'listen', '~> 3.3' # NOTE: for TCP functionality, use '~> 2.10' for now
|
44
|
+
```
|
40
45
|
|
46
|
+
## Complete Example
|
47
|
+
Here is a complete example of using the `listen` gem:
|
41
48
|
```ruby
|
42
|
-
|
49
|
+
require 'listen'
|
50
|
+
|
51
|
+
listener = Listen.to('/srv/app') do |modified, added, removed|
|
52
|
+
puts(modified: modified, added: added, removed: removed)
|
53
|
+
end
|
54
|
+
listener.start
|
55
|
+
sleep
|
56
|
+
```
|
57
|
+
Running the above in the background, you can see the callback block being called in response to each command:
|
58
|
+
```
|
59
|
+
$ cd /srv/app
|
60
|
+
$ touch a.txt
|
61
|
+
{:modified=>[], :added=>["/srv/app/a.txt"], :removed=>[]}
|
62
|
+
|
63
|
+
$ echo more >> a.txt
|
64
|
+
{:modified=>["/srv/app/a.txt"], :added=>[], :removed=>[]}
|
65
|
+
|
66
|
+
$ mv a.txt b.txt
|
67
|
+
{:modified=>[], :added=>["/srv/app/b.txt"], :removed=>["/srv/app/a.txt"]}
|
68
|
+
|
69
|
+
$ vi b.txt
|
70
|
+
# add a line to this new file and press ZZ to save and exit
|
71
|
+
{:modified=>["/srv/app/b.txt"], :added=>[], :removed=>[]}
|
72
|
+
|
73
|
+
$ vi c.txt
|
74
|
+
# add a line and press ZZ to save and exit
|
75
|
+
{:modified=>[], :added=>["/srv/app/c.txt"], :removed=>[]}
|
76
|
+
|
77
|
+
$ rm b.txt c.txt
|
78
|
+
{:modified=>[], :added=>[], :removed=>["/srv/app/b.txt", "/srv/app/c.txt"]}
|
43
79
|
```
|
44
80
|
|
45
81
|
## Usage
|
46
82
|
|
47
|
-
Call `Listen.to` with
|
83
|
+
Call `Listen.to` with one or more directories and the "changes" callback passed as a block.
|
48
84
|
|
49
85
|
``` ruby
|
50
86
|
listener = Listen.to('dir/to/listen', 'dir/to/listen2') do |modified, added, removed|
|
51
|
-
puts "modified absolute path: #{modified}"
|
52
|
-
puts "added absolute path: #{added}"
|
53
|
-
puts "removed absolute path: #{removed}"
|
87
|
+
puts "modified absolute path array: #{modified}"
|
88
|
+
puts "added absolute path array: #{added}"
|
89
|
+
puts "removed absolute path array: #{removed}"
|
54
90
|
end
|
55
|
-
listener.start # not
|
91
|
+
listener.start # starts a listener thread--does not block
|
92
|
+
|
93
|
+
# do whatever you want here...just don't exit the process :)
|
94
|
+
|
56
95
|
sleep
|
57
96
|
```
|
97
|
+
## Changes Callback
|
98
|
+
|
99
|
+
Changes to the listened-to directories are reported by the listener thread in a callback.
|
100
|
+
The callback receives **three** array parameters: `modified`, `added` and `removed`, in that order.
|
101
|
+
Each of these three is always an array with 0 or more entries.
|
102
|
+
Each array entry is an absolute path.
|
58
103
|
|
59
104
|
### Pause / unpause / stop
|
60
105
|
|
@@ -75,13 +120,14 @@ listener.unpause # resumes processing changes ("start" would do the same)
|
|
75
120
|
listener.stop # stop both listening to changes and processing them
|
76
121
|
```
|
77
122
|
|
78
|
-
Note: While paused,
|
123
|
+
Note: While paused, `listen` keeps on collecting changes in the background - to clear them, call `stop`.
|
79
124
|
|
80
|
-
Note: You should keep track of all started listeners and stop them properly on finish.
|
125
|
+
Note: You should keep track of all started listeners and `stop` them properly on finish.
|
81
126
|
|
82
127
|
### Ignore / ignore!
|
83
128
|
|
84
|
-
Listen ignores some directories and extensions by default (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer)
|
129
|
+
`Listen` ignores some directories and extensions by default (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer).
|
130
|
+
You can add ignoring patterns with the `ignore` option/method or overwrite default with `ignore!` option/method.
|
85
131
|
|
86
132
|
``` ruby
|
87
133
|
listener = Listen.to('dir/path/to/listen', ignore: /\.txt/) { |modified, added, removed| # ... }
|
@@ -93,11 +139,11 @@ sleep
|
|
93
139
|
|
94
140
|
Note: `:ignore` regexp patterns are evaluated against relative paths.
|
95
141
|
|
96
|
-
Note: Ignoring paths does not improve performance, except when Polling ([#274](https://github.com/guard/listen/issues/274))
|
142
|
+
Note: Ignoring paths does not improve performance, except when Polling ([#274](https://github.com/guard/listen/issues/274)).
|
97
143
|
|
98
144
|
### Only
|
99
145
|
|
100
|
-
Listen
|
146
|
+
`Listen` watches all files (less the ignored ones) by default. If you want to only listen to a specific type of file (i.e., just `.rb` extension), you should use the `only` option/method.
|
101
147
|
|
102
148
|
``` ruby
|
103
149
|
listener = Listen.to('dir/path/to/listen', only: /\.rb$/) { |modified, added, removed| # ... }
|
@@ -109,35 +155,6 @@ sleep
|
|
109
155
|
Note: `:only` regexp patterns are evaluated only against relative **file** paths.
|
110
156
|
|
111
157
|
|
112
|
-
## Changes callback
|
113
|
-
|
114
|
-
Changes to the listened-to directories gets reported back to the user in a callback.
|
115
|
-
The registered callback gets invoked, when there are changes, with **three** parameters:
|
116
|
-
`modified`, `added` and `removed` paths, in that particular order.
|
117
|
-
Paths are always returned in their absolute form.
|
118
|
-
|
119
|
-
Example:
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
listener = Listen.to('path/to/app') do |modified, added, removed|
|
123
|
-
# This block will be called when there are changes.
|
124
|
-
end
|
125
|
-
listener.start
|
126
|
-
sleep
|
127
|
-
```
|
128
|
-
|
129
|
-
or ...
|
130
|
-
|
131
|
-
```ruby
|
132
|
-
# Create a callback
|
133
|
-
callback = Proc.new do |modified, added, removed|
|
134
|
-
# This proc will be called when there are changes.
|
135
|
-
end
|
136
|
-
listener = Listen.to('dir', &callback)
|
137
|
-
listener.start
|
138
|
-
sleep
|
139
|
-
```
|
140
|
-
|
141
158
|
## Options
|
142
159
|
|
143
160
|
All the following options can be set through the `Listen.to` after the directory path(s) params.
|
@@ -167,16 +184,42 @@ polling_fallback_message: 'custom message' # Set a custom polling fallback
|
|
167
184
|
# default: "Listen will be polling for changes. Learn more at https://github.com/guard/listen#listen-adapters."
|
168
185
|
```
|
169
186
|
|
170
|
-
## Debugging
|
187
|
+
## Logging and Debugging
|
171
188
|
|
172
|
-
|
189
|
+
`Listen` logs its activity to `Listen.logger`.
|
190
|
+
This is the primary method of debugging.
|
173
191
|
|
174
|
-
|
192
|
+
### Custom Logger
|
193
|
+
You can call `Listen.logger =` to set a custom `listen` logger for the process. For example:
|
194
|
+
```
|
195
|
+
Listen.logger = Rails.logger
|
196
|
+
```
|
175
197
|
|
198
|
+
### Default Logger
|
199
|
+
If no custom logger is set, a default `listen` logger which logs to to `STDERR` will be created and assigned to `Listen.logger`.
|
176
200
|
|
177
|
-
|
201
|
+
The default logger defaults to the `error` logging level (severity).
|
202
|
+
You can override the logging level by setting the environment variable `LISTEN_GEM_DEBUGGING=<level>`.
|
203
|
+
For `<level>`, all standard `::Logger` levels are supported, with any mix of upper-/lower-case:
|
204
|
+
```
|
205
|
+
export LISTEN_GEM_DEBUGGING=debug # or 2 [deprecated]
|
206
|
+
export LISTEN_GEM_DEBUGGING=info # or 1 or true or yes [deprecated]
|
207
|
+
export LISTEN_GEM_DEBUGGING=warn
|
208
|
+
export LISTEN_GEM_DEBUGGING=fatal
|
209
|
+
export LISTEN_GEM_DEBUGGING=error
|
210
|
+
```
|
211
|
+
The default of `error` will be used if an unsupported value is set.
|
178
212
|
|
179
|
-
The
|
213
|
+
Note: The alternate values `1`, `2`, `true` and `yes` shown above are deprecated and will be removed from `listen` v4.0.
|
214
|
+
|
215
|
+
### Disabling Logging
|
216
|
+
If you want to disable `listen` logging, set
|
217
|
+
```
|
218
|
+
Listen.logger = ::Logger.new('/dev/null')
|
219
|
+
```
|
220
|
+
## Listen Adapters
|
221
|
+
|
222
|
+
The `Listen` gem has a set of adapters to notify it when there are changes.
|
180
223
|
|
181
224
|
There are 4 OS-specific adapters to support Darwin, Linux, \*BSD and Windows.
|
182
225
|
These adapters are fast as they use some system-calls to implement the notifying function.
|
@@ -184,9 +227,9 @@ These adapters are fast as they use some system-calls to implement the notifying
|
|
184
227
|
There is also a polling adapter - although it's much slower than other adapters,
|
185
228
|
it works on every platform/system and scenario (including network filesystems such as VM shared folders).
|
186
229
|
|
187
|
-
The Darwin and Linux adapters are dependencies of the
|
230
|
+
The Darwin and Linux adapters are dependencies of the `listen` gem so they work out of the box. For other adapters a specific gem will have to be added to your Gemfile, please read below.
|
188
231
|
|
189
|
-
The
|
232
|
+
The `listen` gem will choose the best adapter automatically, if present. If you
|
190
233
|
want to force the use of the polling adapter, use the `:force_polling` option
|
191
234
|
while initializing the listener.
|
192
235
|
|
@@ -218,31 +261,33 @@ end
|
|
218
261
|
|
219
262
|
Please visit the [installation section of the Listen WIKI](https://github.com/guard/listen/wiki#installation) for more information and options for potential fixes.
|
220
263
|
|
221
|
-
### Issues and
|
264
|
+
### Issues and Troubleshooting
|
265
|
+
|
266
|
+
If the gem doesn't work as expected, start by setting `LISTEN_GEM_DEBUGGING=debug` or `LISTEN_GEM_DEBUGGING=info` as described above in [Logging and Debugging](#logging-and-debugging).
|
222
267
|
|
223
|
-
*NOTE: without providing the output after setting the `LISTEN_GEM_DEBUGGING=
|
268
|
+
*NOTE: without providing the output after setting the `LISTEN_GEM_DEBUGGING=debug` environment variable, it is usually impossible to guess why `listen` is not working as expected.*
|
224
269
|
|
225
270
|
See [TROUBLESHOOTING](https://github.com/guard/listen/wiki/Troubleshooting)
|
226
271
|
|
227
272
|
## Performance
|
228
273
|
|
229
|
-
If
|
274
|
+
If `listen` seems slow or unresponsive, make sure you're not using the Polling adapter (you should see a warning upon startup if you are).
|
230
275
|
|
231
276
|
Also, if the directories you're watching contain many files, make sure you're:
|
232
277
|
|
233
278
|
* not using Polling (ideally)
|
234
279
|
* using `:ignore` and `:only` options to avoid tracking directories you don't care about (important with Polling and on MacOS)
|
235
|
-
* running
|
280
|
+
* running `listen` with the `:latency` and `:wait_for_delay` options not too small or too big (depends on needs)
|
236
281
|
* not watching directories with log files, database files or other frequently changing files
|
237
|
-
* not using a version of
|
238
|
-
* not getting silent crashes within
|
239
|
-
* not running multiple instances of
|
282
|
+
* not using a version of `listen` prior to 2.7.7
|
283
|
+
* not getting silent crashes within `listen` (see `LISTEN_GEM_DEBUGGING=debug`)
|
284
|
+
* not running multiple instances of `listen` in the background
|
240
285
|
* using a file system with atime modification disabled (ideally)
|
241
286
|
* not using a filesystem with inaccurate file modification times (ideally), e.g. HFS, VFAT
|
242
287
|
* not buffering to a slow terminal (e.g. transparency + fancy font + slow gfx card + lots of output)
|
243
288
|
* ideally not running a slow encryption stack, e.g. btrfs + ecryptfs
|
244
289
|
|
245
|
-
When in doubt, LISTEN_GEM_DEBUGGING=
|
290
|
+
When in doubt, `LISTEN_GEM_DEBUGGING=debug` can help discover the actual events and time they happened.
|
246
291
|
|
247
292
|
See also [Tips and Techniques](https://github.com/guard/listen/wiki/Tips-and-Techniques).
|
248
293
|
|
data/lib/listen.rb
CHANGED
@@ -1,23 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'logger'
|
4
|
+
require 'weakref'
|
2
5
|
require 'listen/logger'
|
3
6
|
require 'listen/listener'
|
4
7
|
|
5
|
-
require 'listen/internals/thread_pool'
|
6
|
-
|
7
|
-
# Always set up logging by default first time file is required
|
8
|
-
#
|
9
|
-
# NOTE: If you need to clear the logger completely, do so *after*
|
10
|
-
# requiring this file. If you need to set a custom logger,
|
11
|
-
# require the listen/logger file and set the logger before requiring
|
12
|
-
# this file.
|
13
|
-
Listen.setup_default_logger_if_unset
|
14
|
-
|
15
8
|
# Won't print anything by default because of level - unless you've set
|
16
9
|
# LISTEN_GEM_DEBUGGING or provided your own logger with a high enough level
|
17
|
-
Listen
|
18
|
-
Listen
|
10
|
+
Listen.logger.info "Listen loglevel set to: #{Listen.logger.level}"
|
11
|
+
Listen.logger.info "Listen version: #{Listen::VERSION}"
|
19
12
|
|
20
13
|
module Listen
|
14
|
+
@listeners = Queue.new
|
15
|
+
|
21
16
|
class << self
|
22
17
|
# Listens to file system modifications on a either single directory or
|
23
18
|
# multiple directories.
|
@@ -32,21 +27,21 @@ module Listen
|
|
32
27
|
# @return [Listen::Listener] the listener
|
33
28
|
#
|
34
29
|
def to(*args, &block)
|
35
|
-
@listeners ||= []
|
36
30
|
Listener.new(*args, &block).tap do |listener|
|
37
|
-
@listeners
|
31
|
+
@listeners.enq(WeakRef.new(listener))
|
38
32
|
end
|
39
33
|
end
|
40
34
|
|
41
35
|
# This is used by the `listen` binary to handle Ctrl-C
|
42
36
|
#
|
43
37
|
def stop
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
38
|
+
while (listener = @listeners.deq(true))
|
39
|
+
begin
|
40
|
+
listener.stop
|
41
|
+
rescue WeakRef::RefError
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue ThreadError
|
50
45
|
end
|
51
46
|
end
|
52
47
|
end
|
data/lib/listen/adapter.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'listen/adapter/base'
|
2
4
|
require 'listen/adapter/bsd'
|
3
5
|
require 'listen/adapter/darwin'
|
@@ -13,15 +15,15 @@ module Listen
|
|
13
15
|
|
14
16
|
class << self
|
15
17
|
def select(options = {})
|
16
|
-
|
18
|
+
Listen.logger.debug 'Adapter: considering polling ...'
|
17
19
|
return Polling if options[:force_polling]
|
18
|
-
|
20
|
+
Listen.logger.debug 'Adapter: considering optimized backend...'
|
19
21
|
return _usable_adapter_class if _usable_adapter_class
|
20
|
-
|
22
|
+
Listen.logger.debug 'Adapter: falling back to polling...'
|
21
23
|
_warn_polling_fallback(options)
|
22
24
|
Polling
|
23
25
|
rescue
|
24
|
-
|
26
|
+
Listen.logger.warn format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
|
25
27
|
$ERROR_POSITION * "\n")
|
26
28
|
raise
|
27
29
|
end
|
@@ -36,10 +38,6 @@ module Listen
|
|
36
38
|
msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
|
37
39
|
Kernel.warn "[Listen warning]:\n #{msg}" if msg
|
38
40
|
end
|
39
|
-
|
40
|
-
def _log(type, message)
|
41
|
-
Listen::Logger.send(type, message)
|
42
|
-
end
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
data/lib/listen/adapter/base.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'listen/options'
|
2
4
|
require 'listen/record'
|
3
5
|
require 'listen/change'
|
6
|
+
require 'listen/thread'
|
4
7
|
|
5
8
|
module Listen
|
6
9
|
module Adapter
|
@@ -30,7 +33,7 @@ module Listen
|
|
30
33
|
# TODO: it's a separate method as a temporary workaround for tests
|
31
34
|
def configure
|
32
35
|
if @configured
|
33
|
-
|
36
|
+
Listen.logger.warn('Adapter already configured!')
|
34
37
|
return
|
35
38
|
end
|
36
39
|
|
@@ -63,48 +66,39 @@ module Listen
|
|
63
66
|
configure
|
64
67
|
|
65
68
|
if started?
|
66
|
-
|
69
|
+
Listen.logger.warn('Adapter already started!')
|
67
70
|
return
|
68
71
|
end
|
69
72
|
|
70
73
|
@started = true
|
71
74
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
@snapshots.values.each do |snapshot|
|
76
|
-
_timed('Record.build()') { snapshot.record.build }
|
77
|
-
end
|
78
|
-
_run
|
79
|
-
rescue
|
80
|
-
msg = 'run() in thread failed: %s:\n'\
|
81
|
-
' %s\n\ncalled from:\n %s'
|
82
|
-
_log_exception(msg, calling_stack)
|
83
|
-
raise # for unit tests mostly
|
75
|
+
@run_thread = Listen::Thread.new("run_thread") do
|
76
|
+
@snapshots.values.each do |snapshot|
|
77
|
+
_timed('Record.build()') { snapshot.record.build }
|
84
78
|
end
|
79
|
+
_run
|
85
80
|
end
|
86
81
|
end
|
87
82
|
|
88
83
|
def stop
|
89
84
|
_stop
|
90
|
-
|
91
|
-
|
92
|
-
def self.usable?
|
93
|
-
const_get('OS_REGEXP') =~ RbConfig::CONFIG['target_os']
|
85
|
+
config.queue.close # this causes queue.pop to return `nil` to the front-end
|
94
86
|
end
|
95
87
|
|
96
88
|
private
|
97
89
|
|
98
90
|
def _stop
|
91
|
+
@run_thread&.kill
|
92
|
+
@run_thread = nil
|
99
93
|
end
|
100
94
|
|
101
95
|
def _timed(title)
|
102
96
|
start = Time.now.to_f
|
103
97
|
yield
|
104
98
|
diff = Time.now.to_f - start
|
105
|
-
Listen
|
99
|
+
Listen.logger.info format('%s: %.05f seconds', title, diff)
|
106
100
|
rescue
|
107
|
-
Listen
|
101
|
+
Listen.logger.warn "#{title} crashed: #{$ERROR_INFO.inspect}"
|
108
102
|
raise
|
109
103
|
end
|
110
104
|
|
@@ -114,10 +108,6 @@ module Listen
|
|
114
108
|
@snapshots[dir].invalidate(type, rel_path, options)
|
115
109
|
end
|
116
110
|
|
117
|
-
def _log(*args, &block)
|
118
|
-
self.class.send(:_log, *args, &block)
|
119
|
-
end
|
120
|
-
|
121
111
|
def _log_exception(msg, caller_stack)
|
122
112
|
formatted = format(
|
123
113
|
msg,
|
@@ -126,14 +116,12 @@ module Listen
|
|
126
116
|
caller_stack * "\n"
|
127
117
|
)
|
128
118
|
|
129
|
-
|
119
|
+
Listen.logger.error(formatted)
|
130
120
|
end
|
131
121
|
|
132
122
|
class << self
|
133
|
-
|
134
|
-
|
135
|
-
def _log(*args, &block)
|
136
|
-
Listen::Logger.send(*args, &block)
|
123
|
+
def usable?
|
124
|
+
const_get('OS_REGEXP') =~ RbConfig::CONFIG['target_os']
|
137
125
|
end
|
138
126
|
end
|
139
127
|
end
|