handling_queue 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d447fa9814068456364559b5259312d693b98bb409dfc1be0fb2b7767d313a01
4
+ data.tar.gz: 34ef9a975afb0aab5f20d2616f4a87b34acebae1e64dcad4d677b77efd319ba1
5
+ SHA512:
6
+ metadata.gz: acf5e6bbdc92c2e4cf740454da07710c130450e81488187578a63249725157623a24542d1056001fc2bfadb0c1684ee537a403129cec1dfc9c8f8d71391278fe
7
+ data.tar.gz: 2bd825ed166e77ebaa65267f9d4d019198e44e2b6d9332b94bb0dd32432276bcfb9d780a5ecd7eb378b1609ac8cfe98081fe315854a8f275f2baee7b0dfd6015
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
@@ -0,0 +1,41 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-performance
5
+
6
+ AllCops:
7
+ DisplayCopNames: true
8
+ DisplayStyleGuide: true
9
+ ExtraDetails: false
10
+ TargetRubyVersion: 2.6
11
+ Exclude:
12
+ - config/**/*
13
+ - tmp/**/*
14
+ - Capfile
15
+ - Gemfile
16
+ - Rakefile
17
+
18
+ # Follow RubyGuides on this one
19
+ Layout/EndOfLine:
20
+ EnforcedStyle: lf
21
+
22
+ # Just enough
23
+ Layout/LineLength:
24
+ Max: 100
25
+
26
+ # Allow long testing methods
27
+ Metrics/MethodLength:
28
+ Exclude:
29
+ - test/test_*.rb
30
+
31
+ # Follow RubyGuides on this one
32
+ Style/HashSyntax:
33
+ EnforcedStyle: ruby19_no_mixed_keys
34
+
35
+ # Prefer using Rubies methods
36
+ Style/HashEachMethods:
37
+ Enabled: true
38
+ Style/HashTransformKeys:
39
+ Enabled: true
40
+ Style/HashTransformValues:
41
+ Enabled: true
@@ -0,0 +1,7 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-06-12 22:08:35 +0300 using RuboCop version 0.85.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake', '~> 12.0'
4
+ gem 'minitest', '~> 5.0'
5
+
6
+ gem 'rubocop', '~> 0.85.0'
7
+ gem 'rubocop-performance', require: false
8
+
9
+ gem 'pry', '~> 0.13.1'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fizvlad
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.
@@ -0,0 +1,37 @@
1
+ # HandlingQueue
2
+
3
+ Timed handling of elements of queue in separate threads.
4
+
5
+ ## Installation
6
+
7
+ Add gem to your Gemfile using
8
+
9
+ $ bundle add handling_queue
10
+
11
+ ## Usage
12
+
13
+ Setting up handler:
14
+
15
+ handling_queue = HandlingQueue.new(slice: 1, interval: 1) do |a|
16
+ # Handle `a` which is array of `HandlingQueue::Request`
17
+ a.each { |e| e.re = e.obj * 2 } # It is necessar to assign `e.re`
18
+ end
19
+
20
+ Adding elements to queue:
21
+
22
+ handling_queue.handle(1) # Sleeps until handler called for this number, then => 2
23
+ handling_queue.handle(2) # => 4
24
+ handling_queue.handle(5) # => 10
25
+
26
+ ## Development
27
+
28
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fizvlad/handling_queue.
33
+
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,48 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rubocop/rake_task'
4
+ RuboCop::RakeTask.new(:rubocop) {}
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.test_files = FileList['test/test*.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ namespace 'rubocop' do
14
+ desc 'Generate rubocop TODO file.'
15
+ task 'todo' do
16
+ puts `rubocop --auto-gen-config`
17
+ end
18
+ end
19
+
20
+ namespace 'yardoc' do
21
+ desc 'Generate documentation'
22
+ task 'generate' do
23
+ puts `yardoc lib/*`
24
+ end
25
+
26
+ desc 'List undocumented elements'
27
+ task 'undoc' do
28
+ puts `yardoc stats --list-undoc lib/*`
29
+ end
30
+ end
31
+
32
+ namespace 'eol' do
33
+ desc 'Replace CRLF with LF.'
34
+ task :dos2unix, [:pattern] do |_t, args|
35
+ path_list = Dir.glob(args.pattern || '**/*.{rb,rake}', File::FNM_EXTGLOB)
36
+ counter = 0
37
+ path_list.each do |path|
38
+ next unless File.file?(path)
39
+
40
+ counter += 1
41
+ puts "Handling `#{path}`..."
42
+ content = File.read(path, mode: 'rb')
43
+ content.gsub!(/\r\n/, "\n")
44
+ File.write(path, content, mode: 'wb')
45
+ end
46
+ puts "Handled #{counter} files."
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'handling_queue'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+ Pry.start
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/handling_queue/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'handling_queue'
7
+ spec.version = HandlingQueue::VERSION
8
+ spec.authors = ['Fizvlad']
9
+ spec.email = ['fizvlad@mail.ru']
10
+
11
+ spec.summary = 'General purpose text bots'
12
+ spec.homepage = 'https://github.com/fizvlad/handling_queue'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.5')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/fizvlad/handling_queue'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'handling_queue/version'
4
+ require_relative 'handling_queue/request'
5
+
6
+ # Execute handling process from time to time.
7
+ class HandlingQueue
8
+ # @param slice [Integer] amount of handled objects in single +handler+ call.
9
+ # @param interval [Integer] interval between handler calls in seconds.
10
+ # @yieldparam obj [Array<Request>] array of handling requests.
11
+ def initialize(slice: 1, interval: 1, &handler)
12
+ @slice = slice
13
+ @interval = interval
14
+ @handler = handler
15
+
16
+ @requests_queue = [] # Use Array with #push and #shift methods to have queue.
17
+ # NOTE: effeciency is pretty good, btw. O(1) if no new memory required.
18
+
19
+ @mutex = Mutex.new # Queue and requests accessing mutex
20
+ @cv = ConditionVariable.new # Condition variable to signal when some of results are ready.
21
+
22
+ @working = false
23
+ @thread = nil
24
+ start_handler_thread
25
+ end
26
+
27
+ # Request handling of +obj+ and wait for it to be handled.
28
+ # @param obj [void] object to be handled.
29
+ # @return [void] result of handling.
30
+ def handle(obj)
31
+ request = Request.new(obj)
32
+ @mutex.synchronize do
33
+ @requests_queue.push(request)
34
+ @cv.wait(@mutex) while request.re.nil?
35
+ request.re
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def start_handler_thread
42
+ @working = true
43
+ @thread = Thread.new do
44
+ handler_iteration while @working
45
+ end
46
+ @thread.name = 'handling_queue'
47
+ end
48
+
49
+ def handler_iteration
50
+ sleep @interval
51
+ @mutex.synchronize do
52
+ arg = @requests_queue.shift(@slice)
53
+ @handler.call(arg) unless arg.empty?
54
+ @cv.broadcast
55
+ end
56
+ end
57
+
58
+ def stop_handler_thread
59
+ @working = false
60
+ @thread.join
61
+ @thread = nil
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HandlingQueue
4
+ # Represents single request to {HandlingQueue}.
5
+ class Request
6
+ # @return [void] object to be handled.
7
+ attr_accessor :obj
8
+
9
+ # @return [void] handled object or +nil+ if not handled yet.
10
+ attr_accessor :re
11
+
12
+ # @param obj [void] object to be handled.
13
+ def initialize(obj)
14
+ @obj = obj
15
+ @re = nil
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HandlingQueue
4
+ # Library version.
5
+ VERSION = '1.0.0'
6
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handling_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fizvlad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - fizvlad@mail.ru
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rubocop.yml"
22
+ - ".rubocop_todo.yml"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - handling_queue.gemspec
30
+ - lib/handling_queue.rb
31
+ - lib/handling_queue/request.rb
32
+ - lib/handling_queue/version.rb
33
+ homepage: https://github.com/fizvlad/handling_queue
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/fizvlad/handling_queue
38
+ source_code_uri: https://github.com/fizvlad/handling_queue
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.6.5
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.1.0.pre2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: General purpose text bots
58
+ test_files: []