https://github.com/nejdetkadir/safe_poller
SafePoller is a Ruby gem that provides a safe and reliable way to perform periodic polling operations in multi-threaded environments
https://github.com/nejdetkadir/safe_poller
poll polling ruby thread
Last synced: 3 months ago
JSON representation
SafePoller is a Ruby gem that provides a safe and reliable way to perform periodic polling operations in multi-threaded environments
- Host: GitHub
- URL: https://github.com/nejdetkadir/safe_poller
- Owner: nejdetkadir
- License: mit
- Created: 2023-04-18T15:20:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-18T17:30:12.000Z (over 2 years ago)
- Last Synced: 2025-07-11T03:22:08.778Z (3 months ago)
- Topics: poll, polling, ruby, thread
- Language: Ruby
- Homepage:
- Size: 11.7 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://badge.fury.io/rb/safe_poller)


[](https://github.com/rubocop/rubocop)
# 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:
```bash
$ bundle add safe_poller
```Or add the following line to the application's Gemfile:
```ruby
gem 'safe_poller', github: 'nejdetkadir/safe_poller', branch: 'main'
```If bundler is not being used to manage dependencies, install the gem by executing:
```bash
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:
```ruby
poller = SafePoller.poll(interval: 2.0) do
# Code to be executed at 2-second intervals
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:
```ruby
poller = SafePoller.poll_for(30, interval: 2.0) do
# Code to be executed every 2 seconds for 30 seconds
end
```Or, you can specify the end time of the poll using the `SafePoller.poll_until` method:
```ruby
end_time = Time.now + 60
poller = SafePoller.poll_until(end_time, interval: 2.0) do
# Code to be executed every 2 seconds until 1 minute from now
end
```You can pause and resume the poller using the `pause` and `resume` methods, respectively:
```ruby
poller.pause # Pauses the poller
poller.resume # Resumes the poller
```You can check if the poller is currently running or paused using the `running?` and `paused?` methods, respectively:
```ruby
poller.running? # Returns true if the poller is running, false otherwise
poller.paused? # Returns true if the poller is paused, false otherwise
```You can stop the poller using the `stop` method:
```ruby
poller.stop # Stops the poller
```## 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](https://github.com/nejdetkadir/safe_poller/blob/main/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](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](https://github.com/nejdetkadir/safe_poller/blob/main/CODE_OF_CONDUCT.md).