Skip to content

Conversation

@Mr0grog
Copy link
Contributor

@Mr0grog Mr0grog commented Jun 26, 2024

This adds a new watchdog_interval method that returns the current watchdog interval in seconds (or 0 if no interval is set). Fixes #6.

A few notes here:

  • I tried to keep this simple by just adding a watchdog_interval method and not changing any other code. That said, it might be convenient if watchdog? returned false if disabled and the interval (instead of true) if enabled. That would work fine for most people (since floats are truthy), but might be subtly breaking if someone is literally comparing the return value to true.

  • Revised the docs for watchdog? a bit to hopefully make them clearer for users who aren’t already deeply familiar with sd_notify, instead of focusing on under-the-hood bits like environment variables. Happy to change this back if you don’t like that.

  • Added tests for watchdog? since there weren’t any.

  • Added an example of using watchdog to the README. It’s a little complex! Let me know if you have ideas for making it simpler or clearer, or if I should remove it.

  • When I ran Rubocop, I got a huge number of errors, mostly from cops that are probably new since this gem last received a major update. I went ahead and turned most of them off in the config, although I did add required_ruby_version to the gemspec file since that seemed like a good idea. I set the value (>= 2.3.0 >= 2.2.0) based on the GitHub Actions CI job. Let me know if I should change or remove it.

Mr0grog added 3 commits June 26, 2024 15:34
This adds a `watchdog_interval` method as well as tests for it and for `watchdog?` (which previously had no tests). I also took the liberty of revising the docs for `watchdog?` to (I hope) be a little clearer.

Fixes agis#6.
Running Rubocop printed out a huge number of new warnings and errors. For the most part, I just silenced them since this gem has been pretty stable and I assume you don't want to modify things just to match newer recommendations. I did add `required_ruby_version` to the gemspec, though, since that just seems like a good idea. It's set to 2.3.0, based on the GitHub Actions CI job.
@Mr0grog Mr0grog mentioned this pull request Jun 26, 2024
.rubocop.yml Outdated
Comment on lines 4 to 6
AllCops:
TargetRubyVersion: 2.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how many cops I had to turn off in this PR, it might be better to disable them all here and explicitly turn on the ones you want. I’m not sure which you want, though, so I just turned off all the new and/or error-triggering ones.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the issue is that rubocop was never pinned to a specific version. I'd suggest to just disregard all the offenses, given that it's not a requirement in the CI, and just revert this change here, since it's out of context of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, would it be good to at least remove this Rubocop config file, then? It seems a little confusing to have an unusable .rubocop.yml file here.

@Mr0grog
Copy link
Contributor Author

Mr0grog commented Sep 14, 2025

Just checking in on this, since it's been sitting for a year. Anything you need from me to land this, @agis?

Copy link
Owner

@agis agis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the radio silence. Left some comments, thanks for this!

lib/sd_notify.rb Outdated
wd_usec = Integer(ENV["WATCHDOG_USEC"])
wd_usec > 0 ? wd_usec / 1e6 : 0.0
rescue StandardError
0.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a clearer API if we returned nil when the value isn't set. Also I'd prefer to limit this rescue to the place it's intended for, i.e

def self.watchdog_interval
  wd_usec = Integer(ENV["WATCHDOG_USEC"]) rescue nil
  wd_usec && wd_usec / 1e6
end

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a clearer API if we returned nil when the value isn't set.

Sure. (Just for background, I usually try to avoid returning a different types as a general practice so the JIT can keep methods optimized. This probably isn’t a method that’s going to be on a hot path, though, so that’s not a big deal here.)

.rubocop.yml Outdated
Comment on lines 4 to 6
AllCops:
TargetRubyVersion: 2.2

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the issue is that rubocop was never pinned to a specific version. I'd suggest to just disregard all the offenses, given that it's not a requirement in the CI, and just revert this change here, since it's out of context of this PR.

s.files = ["lib/sd_notify.rb", "LICENSE", "README.md", "CHANGELOG.md"]
s.homepage = "https://github.com/agis/ruby-sdnotify"
s.license = "MIT"
s.required_ruby_version = ">= 2.2.0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale behind this requirement?

Copy link
Contributor Author

@Mr0grog Mr0grog Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the PR description:

When I ran Rubocop, I got a huge number of errors, mostly from cops that are probably new since this gem last received a major update. I went ahead and turned most of them off in the config, although I did add required_ruby_version to the gemspec file since that seemed like a good idea. I set the value (>= 2.3.0 >= 2.2.0) based on the GitHub Actions CI job. Let me know if I should change or remove it.

That is, Rubocop required that I add required_ruby_version or disable that cop. It seemed more reasonable in this case to add it to the gemspec and ask you if you wanted a different value.

From your other comments, it sounds like you'd rather just ignore Rubocop for this repo, though, so I can pull this back out if that’s what you’d like. (Is that what you’d like?)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for elaborating. In that case, yes, let's remove the rubocop config completely.

ENV["WATCHDOG_PID"] = $$.to_s
setup_socket

assert_equal(true, SdNotify.watchdog?)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified to `assert SdNotify.watchdog?

The existing Rubocop config is set up for an unknown version of Rubocop from several years ago that was never pinned. The current rules Rubocop suggests are undesired, so this just removes it instead.
The `ruby/setup-ruby` action does not support Ruby 2.2 on Ubuntu 22+ (see ruby/setup-ruby#496) and GitHub Actions no longer supports older versions of Ubuntu, so we can no longer test Ruby 2.2 (at least not without a lot of extra work!).
@Mr0grog Mr0grog force-pushed the 6-sdnotify-knows-your-watchdog-interval-but-will-it-tell-you branch 3 times, most recently from 98cada3 to dbf0dfd Compare October 20, 2025 05:47
@Mr0grog Mr0grog force-pushed the 6-sdnotify-knows-your-watchdog-interval-but-will-it-tell-you branch from dbf0dfd to 9ff9cdc Compare October 20, 2025 05:55
@Mr0grog Mr0grog force-pushed the 6-sdnotify-knows-your-watchdog-interval-but-will-it-tell-you branch from a40c59b to 20b1483 Compare October 20, 2025 06:08
@Mr0grog
Copy link
Contributor Author

Mr0grog commented Oct 20, 2025

@agis OK, I think I’ve made all the changes you asked for and this is ready for re-review.

Since this PR was originally written, GitHub dropped support for Ubuntu 20.04, so I had to update the CI config a bit — the “old” tests run in Ubuntu 22.04, and use Ruby 2.1 since Ruby 2.2 is no longer supported by the ruby/setup-ruby action on any Ubuntu versions supported on GitHub Actions. I’m assuming based on your previous comments about required_ruby_version that you do want to support Ruby 2.1, but I can also just remove the “old” tests entirely if this is not right. (Also: Ruby 2.1 should be installable just fine on Ubuntu 24.04, so we could move all this into the main test job instead having a separate “old” one. The main test job just needs to bundle exec so it gets the right minitest version.)

@agis agis merged commit c0a2d44 into agis:master Oct 26, 2025
11 checks passed
@agis
Copy link
Owner

agis commented Oct 26, 2025

Thanks!

@Mr0grog Mr0grog deleted the 6-sdnotify-knows-your-watchdog-interval-but-will-it-tell-you branch October 26, 2025 23:45
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

Successfully merging this pull request may close these issues.

Get watchdog interval

2 participants