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

Looking for a good way to break out of Journal#watch #56

Closed
kiyoto opened this issue Dec 22, 2014 · 2 comments
Closed

Looking for a good way to break out of Journal#watch #56

kiyoto opened this issue Dec 22, 2014 · 2 comments

Comments

@kiyoto
Copy link

kiyoto commented Dec 22, 2014

I am currently writing a plugin for Fluentd using this gem. The issue that I ran into is, I couldn't find a good way to break out of Journal#watch. In other words:

def start
  @journal = Systemd::Journal.new
  @journal.watch do |entry|
    # do stuff ehre
  end
end

and this "start" method is run in a different event loop (using Cool.io). What I want to do is, in the corresponding "shutdown" method, stop the watch loop like this (shutdown is invoked when there is a INT/KILL signal.

def shutdown
  # somehow stop @journal.watch
end

Is this something possible? If there is a better way to go about it, pointers is hugely appreciated.

@ledbettj
Copy link
Owner

Sorry for the late response.

Yes, watch does not return and in particular uses a systemd journal API that blocks inside C code, and so cannot be interrupted from ruby land. In particular watch is implemented something like this:

loop do
    if journal.wait # something happened - wake up!
      yield journal.current_entry while journal.move_next
    end
end

You can do something similar and pass a timeout value to wait so that it will wake up even if nothing changed every so often, so you can check if you need to exit (untested example below):

while should_i_keep_running?
    if journal.wait(3_000_000) # wake up at least every 3 seconds
      yield journal.current_entry while journal.move_next
    end
end

Additionally, if you need to handle signals interrupting wait, you can pass select: true as a second parameter. This uses IO.select to wait for things to change from the ruby side, so it can be interrupted, unlike normal wait. Note that this won't work on JRuby.

@kiyoto
Copy link
Author

kiyoto commented Dec 29, 2014

Thanks! I will examine this further. For now, consider this to be closed.

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