New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to pause while responding to file changes? #549
Comments
@ColinDKelley I've tried that a few different ways, and I can't figure out how to have it pause while the listen job is running. (I just edited my second bullet to be more clear about that.) The main issue, as I see it, is that there is no way to access the listener from within the block. listener = Listen.to('/src') do |modified, added, remaining|
listener.pause # `listener` isn't defined, so I can't call this here.
maybe_make_changes_to_files(modified, added)
listener.start # same issue as 2 lines up
end Ideally I'd be able to write something like: my_listener = Listen.to('/src') do |event|
event.listener.pause
maybe_make_changes_to_files(event.modified, event.added)
event.listener.start
end |
Hi @beechnut, are you sure there is no way to access Can you double-check your assertion here? If you still get a failure, which version of Ruby are you using? I tested with 2.6.1.
|
Can you explain further what you mean by "test" here? Did you run a test or have a working code sample that can access |
@beechnut I just tried this code and it worked exactly as expected, using Ruby 2.6.1 and listen 3.4.0:
The expected behavior is that the block runs the first time anything is changed in /tmp. It then pauses. When I make more changes in /tmp, the block doesn't execute. Until I run Does this code work for you? If not, can you include the specific error you get? |
@beechnut Any update here? |
So, that particular code snippet does technically work for me, and I got another more complicated example to work: listener = Listen.to("/path/to") do |*args|
puts args.inspect
listener.pause
puts "doing some work:"
%x( touch /path/to/file ) ; puts "\tmade a file"
sleep 1
%x( rm /path/to/file ) ; puts "\tdeleted a file"
sleep 0.5
listener.start
end However, this has helped me realize that I'm trying to pause listening, not responding, and that's why I referred to listener = Listen.to("/path/to") do |*args|
puts args.inspect
listener.stop # <-- changed from `pause` to get it to stop listening to changes
puts "doing some work:"
%x( touch /path/to/file ) ; puts "\tmade a file"
sleep 1
%x( rm /path/to/file ) ; puts "\tdeleted a file"
sleep 0.5
listener.start
end This results in the following output, printing the changed files and then throwing a ThreadError:
I assume that the issue here is calling The options I'm seeing are: |
Can't you achieve what you want with a level of indirection, where the callback sometimes delegates to a proc/lambda and sometimes doesn't? As in:
But this example is inherently confusing because the state changes are inside the callback...which can run recursively. I think it would be simpler to demonstrate without recursive nesting. |
@beechnut Any objections if I close out this issue and you can file a fresh one with your refined concern? |
我已收到你的邮件,谢谢!
|
@ColinDKelley I'd like to keep this open for now, because reading back through the history, I think the refined concern is still essentially the same as it was in the first post:
I've been swamped and haven't had time to try the last solution you posted—thanks for posting that, and 'll give that a try early next week and reply with results. I'm still interested in the idea of:
Is that something that seems architecturally possible? I haven't dug into the internals of Listen much, you'll know better than me. |
That sounds achievable with a level of indirection. I posted about that above, on Jan 31. Will that approach work for you? |
我已收到你的邮件,谢谢!
|
I haven't been free to focus on this in a while, but I'm getting the impression that there's not much interest in fully understanding or supporting this use case, so I'm just going to close the issue. |
I'm using Listen to watch a directory for changes, and sometimes the response to a change modifies one of the files being watched. I don't want the listener to respond to the automatic file modification. (It is not possible, within my design, to exclude such files from being watched.)
Another way to say this: I want to pause the listener while the block that's responding to changes gets executed.
I'm looking for a way to do something like:
Attempted solutions
latency
and the like. None of these stops the listener from triggering when the files in question change, it just changes the timing.listener
tomaybe_make_changes_to_files
as a parameter and have that method calllistener.pause
. I get a message that says that's not allowed.The text was updated successfully, but these errors were encountered: