safe_poller 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8eaa6bd230c29bffb5dbd45df7438fc40245d42288d29a0bea87dcee71fb9bf1
4
+ data.tar.gz: 92dd04bc2757d2f7237daa4be896afa4b5752754631f398c2da4a09e4c00b25c
5
+ SHA512:
6
+ metadata.gz: 264d72cc9b1844297c110b4304e2ba822f1da92357b8dcba7282cf66af01cd9b451767d0776e0c58acf020b22992a2593af052c712686254d6bfa63a53ca5888
7
+ data.tar.gz: f65afac92c576f0c468cbd85e918a8b98ba20984cfb3cfec7ae7679e06402aaed398e771a4612e67081831af5c9541120f87012c985b99bfead04b5d52b274a8
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,31 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: disable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: single_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: single_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Metrics/MethodLength:
21
+ Enabled: true
22
+ CountComments: false
23
+ Max: 30
24
+
25
+ Metrics/AbcSize:
26
+ Max: 30
27
+
28
+ Metrics/BlockLength:
29
+ Enabled: true
30
+ Exclude:
31
+ - 'spec/**/*'
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nejdetkadir.550@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in safe_poller.gemspec
6
+ gemspec
7
+
8
+ # Rake is a Make-like program implemented in Ruby [https://github.com/ruby/rake]
9
+ gem 'rake', '~> 13.0'
10
+
11
+ # Behaviour Driven Development for Ruby [https://github.com/rspec/rspec-metagem]
12
+ gem 'rspec', '~> 3.0'
13
+
14
+ # RuboCop is a Ruby code style checking and code formatting tool [https://github.com/rubocop/rubocop]
15
+ gem 'rubocop', '~> 1.21'
16
+
17
+ # Pretty print Ruby objects with proper indentation and colors [https://github.com/awesome-print/awesome_print]
18
+ gem 'awesome_print'
19
+
20
+ # Pry is a runtime developer console and IRB alternative with powerful introspection capabilities [https://github.com/pry/pry]
21
+ gem 'pry', '~> 0.14.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ safe_poller (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ awesome_print (1.9.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ json (2.6.3)
14
+ method_source (1.0.0)
15
+ parallel (1.22.1)
16
+ parser (3.2.2.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.2)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.8.0)
24
+ rexml (3.2.5)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.1)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.5)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.0)
38
+ rubocop (1.50.2)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.28.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.28.0)
49
+ parser (>= 3.2.1.0)
50
+ ruby-progressbar (1.13.0)
51
+ unicode-display_width (2.4.2)
52
+
53
+ PLATFORMS
54
+ arm64-darwin-22
55
+
56
+ DEPENDENCIES
57
+ awesome_print
58
+ pry (~> 0.14.1)
59
+ rake (~> 13.0)
60
+ rspec (~> 3.0)
61
+ rubocop (~> 1.21)
62
+ safe_poller!
63
+
64
+ BUNDLED WITH
65
+ 2.4.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 nejdetkadir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ [![Gem Version](https://badge.fury.io/rb/safe_poller.svg)](https://badge.fury.io/rb/safe_poller)
2
+ ![test](https://github.com/nejdetkadir/safe_poller/actions/workflows/test.yml/badge.svg?branch=main)
3
+ ![rubocop](https://github.com/nejdetkadir/safe_poller/actions/workflows/rubocop.yml/badge.svg?branch=main)
4
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
5
+ ![Ruby Version](https://img.shields.io/badge/ruby_version->=_2.7.0-blue.svg)
6
+
7
+ # Safe Poller
8
+
9
+ 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.
10
+
11
+
12
+ ## Installation
13
+
14
+ Install the gem and add to the application's Gemfile by executing:
15
+ ```bash
16
+ $ bundle add safe_poller
17
+ ```
18
+
19
+ Or add the following line to the application's Gemfile:
20
+ ```ruby
21
+ gem 'safe_poller', github: 'nejdetkadir/safe_poller', branch: 'main'
22
+ ```
23
+
24
+ If bundler is not being used to manage dependencies, install the gem by executing:
25
+ ```bash
26
+ gem install safe_poller
27
+ ```
28
+
29
+
30
+ ## Usage
31
+
32
+ To use SafePoller, simply call the `SafePoller.poll` method, passing in a block of code to be executed at the specified interval:
33
+ ```ruby
34
+ poller = SafePoller.poll(interval: 2.0) do
35
+ # Code to be executed at 2-second intervals
36
+ end
37
+ ```
38
+
39
+ 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:
40
+ ```ruby
41
+ poller = SafePoller.poll_for(30, interval: 2.0) do
42
+ # Code to be executed every 2 seconds for 30 seconds
43
+ end
44
+ ```
45
+
46
+ Or, you can specify the end time of the poll using the `SafePoller.poll_until` method:
47
+ ```ruby
48
+ end_time = Time.now + 60
49
+ poller = SafePoller.poll_until(end_time, interval: 2.0) do
50
+ # Code to be executed every 2 seconds until 1 minute from now
51
+ end
52
+ ```
53
+
54
+ You can pause and resume the poller using the `pause` and `resume` methods, respectively:
55
+ ```ruby
56
+ poller.pause # Pauses the poller
57
+ poller.resume # Resumes the poller
58
+ ```
59
+
60
+ You can check if the poller is currently running or paused using the `running?` and `paused?` methods, respectively:
61
+ ```ruby
62
+ poller.running? # Returns true if the poller is running, false otherwise
63
+ poller.paused? # Returns true if the poller is paused, false otherwise
64
+ ```
65
+
66
+ You can stop the poller using the `stop` method:
67
+ ```ruby
68
+ poller.stop # Stops the poller
69
+ ```
70
+
71
+
72
+ ## Contributing
73
+
74
+ 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).
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](LICENSE).
79
+
80
+ ## Code of Conduct
81
+
82
+ 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).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:rspec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[rspec rubocop]
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SafePoller
4
+ class Base
5
+ def initialize(**options, &block)
6
+ @block = block
7
+ @running = false
8
+ @paused = false
9
+ @thread = nil
10
+ @mutex = Mutex.new
11
+ @pause_cv = ConditionVariable.new
12
+ @interval = options.fetch(:interval, 1.0)
13
+ @until = options.fetch(:until, nil)
14
+ end
15
+
16
+ def start
17
+ return if @running
18
+
19
+ @running = true
20
+
21
+ @thread = Thread.new do
22
+ loop do
23
+ @mutex.synchronize do
24
+ @pause_cv.wait(@mutex) if @paused
25
+ end
26
+
27
+ break unless @running
28
+ break if @until && Time.now >= @until
29
+
30
+ @block.call
31
+
32
+ sleep @interval
33
+ end
34
+ end
35
+ end
36
+
37
+ def stop
38
+ return unless @running
39
+
40
+ @mutex.synchronize do
41
+ @running = false
42
+ @paused = false
43
+ @pause_cv.signal
44
+ end
45
+
46
+ @thread.join
47
+ @thread = nil
48
+ end
49
+
50
+ def pause
51
+ return unless @running
52
+
53
+ @mutex.synchronize do
54
+ @paused = true
55
+ end
56
+ end
57
+
58
+ def resume
59
+ return unless @running
60
+
61
+ @mutex.synchronize do
62
+ @paused = false
63
+ @pause_cv.signal
64
+ end
65
+ end
66
+
67
+ def running?
68
+ @running
69
+ end
70
+
71
+ def paused?
72
+ @paused
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SafePoller
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'safe_poller/base'
4
+ require_relative 'safe_poller/version'
5
+
6
+ module SafePoller
7
+ def self.poll(**options, &block)
8
+ poller = SafePoller::Base.new(**options, &block)
9
+ poller.start
10
+ poller
11
+ end
12
+
13
+ def self.poll_until(until_time, **options, &block)
14
+ options[:until] = until_time
15
+ poll(**options, &block)
16
+ end
17
+
18
+ def self.poll_for(duration, **options, &block)
19
+ options[:until] = Time.now + duration
20
+ poll(**options, &block)
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module SafePoller
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_poller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nejdetkadir
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ SafePoller is a Ruby gem that provides a safe and reliable way to perform periodic polling operations in multi-threaded environments.
15
+ 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.
16
+ SafePoller allows developers to focus on implementing their logic without worrying about thread safety issues.
17
+ email:
18
+ - nejdetkadir.550@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".rspec"
24
+ - ".rubocop.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - lib/safe_poller.rb
32
+ - lib/safe_poller/base.rb
33
+ - lib/safe_poller/version.rb
34
+ - sig/safe_poller.rbs
35
+ homepage: https://github.com/nejdetkadir/safe_poller
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/nejdetkadir/safe_poller
40
+ source_code_uri: https://github.com/nejdetkadir/safe_poller
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.7.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.4.10
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: SafePoller is a thread-safe Ruby gem that simplifies the process of periodic
60
+ polling. It provides a reliable and efficient way to run a block of code at a set
61
+ interval without the need for complex threading logic. With SafePoller, developers
62
+ can ensure their polling operations are safe and reliable in multi-threaded environments.
63
+ test_files: []