Skip to content
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

Managing listener lifecycle #570

Open
christopher-b opened this issue Aug 2, 2023 · 1 comment
Open

Managing listener lifecycle #570

christopher-b opened this issue Aug 2, 2023 · 1 comment

Comments

@christopher-b
Copy link

Hi Folks, I have a class that includes a listener as a rails "class attribute":

module MyModule
  class MyClass
    cattr_accessor(:file_listener)

    def initialize
      initialize_file_listener
    end

    def initialize_file_listener
      self.file_listener&.stop

      self.file_listener = Listen.to(config[:local_path]) do |modified, added, _|
        # ...
      end

      self.file_listener.start
    end
  end
end

I periodically get this exception when calling start:

ArgumentError: Listen::Listener can't change state from 'stopped' to 'processing_events', only to: backend_started

I'm not sure why this is happening. I believe I'm setting the file_listener variable to a new Listener instance, so the state of that instance should be the default state, :initializing.

Unfortunately, there's nothing in my logs to shed light on this (I have set LISTEN_GEM_DEBUGGING=debug).

Is there additional lifecycle management I need to do on the listeners? Is there a better approach to be taking?

Thanks!

@guard guard deleted a comment from LouisaNikita Feb 28, 2024
@ColinDKelley
Copy link
Collaborator

@christopher-b One approach you could use would be to nil out the file listener once you stop it. That's reasonable since it won't be useful after that point. Then you'd need to create a new one when needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants