Safe Poller
SafePoller is a Ruby gem that provides a safe and reliable way to perform periodic polling operations in multi-threaded environments. It offers a simple and intuitive API for running a block of code at a defined interval, while ensuring thread safety and preventing potential race conditions. SafePoller allows developers to focus on implementing their logic without worrying about thread safety issues.
Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add safe_poller
Or add the following line to the application's Gemfile:
gem 'safe_poller', github: 'nejdetkadir/safe_poller', branch: 'main'
If bundler is not being used to manage dependencies, install the gem by executing:
gem install safe_poller
Usage
To use SafePoller, simply call the SafePoller.poll
method, passing in a block of code to be executed at the specified interval:
poller = SafePoller.poll(interval: 2.0) do
end
By default, the interval is set to 1 second. You can also specify the duration of the poll by using the SafePoller.poll_for
method:
poller = SafePoller.poll_for(30, interval: 2.0) do
end
Or, you can specify the end time of the poll using the SafePoller.poll_until
method:
end_time = Time.now + 60
poller = SafePoller.poll_until(end_time, interval: 2.0) do
end
You can pause and resume the poller using the pause
and resume
methods, respectively:
poller.pause
poller.resume
You can check if the poller is currently running or paused using the running?
and paused?
methods, respectively:
poller.running?
poller.paused?
You can stop the poller using the stop
method:
poller.stop
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nejdetkadir/safe_poller. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SafePoller project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.