black_company 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6578169a2c08c2121fa339b741c64d945ad09ef
4
+ data.tar.gz: d81d9e9d0e70bfefc38819f0ad6739c6f3972ebb
5
+ SHA512:
6
+ metadata.gz: 25c31e4765d45c157be4e66874c8fb3d89339a855fcdc30c8baab4f1236f37569d9a2f4ff194efaf1e47b2251e2b246e334b33ea96e96f8bf98bd7b53e5cd18c
7
+ data.tar.gz: 326f6d5cd8200fc9254fbe3d8c91148ad3b50039bbabd8533000c44a09894eba8195642d96865b28a48d792f5df380dec85f5df6b359416a2722ba0517c68f6d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in black_company.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 nownabe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # BlackCompany
2
+ BlackCompany provides workhorses. But there is a limit to the number of them. So it's a thread pool :yum:
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'black_company'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install black_company
19
+
20
+ ## Usage
21
+
22
+ `require "black_company"`
23
+
24
+ Simple usage:
25
+
26
+ ```ruby
27
+ black_company = BlackCompany.start
28
+ 100.times do |i|
29
+ black_company.receive(i) do |i|
30
+ puts "This is the #{i}th task..."
31
+ end
32
+ end
33
+ ```
34
+
35
+ You can use your original workhorse:
36
+
37
+ ```ruby
38
+ class MyWorkhorse < BlackCompany::Workhorse
39
+ def serve(task_content)
40
+ puts "This is the #{task_content}th task..."
41
+ end
42
+ end
43
+
44
+ black_company = BlackCompany.start(workhorse_class: MyWorkhorse)
45
+ 100.times do |task_content|
46
+ black_company.receive(task_content)
47
+ end
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nownabe/black_company.
59
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "black_company"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'black_company/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "black_company"
8
+ spec.version = BlackCompany::VERSION
9
+ spec.authors = ["nownabe"]
10
+ spec.email = ["nownabe@gmail.com"]
11
+
12
+ spec.summary = %q{BlackCompany provides workhorses. But there is a limit to the number of them. So it's a thread pool :yum:}
13
+ spec.homepage = "https://github.com/nownabe/black_company"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.10"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,10 @@
1
+ module BlackCompany
2
+ class Event
3
+ attr_reader :type, :data
4
+
5
+ def initialize(type, data)
6
+ @type = type
7
+ @data = data
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,84 @@
1
+ require "black_company/event"
2
+ require "black_company/task"
3
+ require "black_company/workhorse"
4
+
5
+ module BlackCompany
6
+ class Pool
7
+ DEFAULT_POOL_SIZE = 20
8
+
9
+ attr_reader :workhorse_class, :exeption_handlers
10
+
11
+ def initialize(
12
+ pool_size: DEFAULT_POOL_SIZE,
13
+ queue_size: nil,
14
+ workhorse_class: Workhorse,
15
+ exeption_handlers: []
16
+ )
17
+
18
+ @queue = queue_size ? SizedQueue.new(queue_size) : Queue.new
19
+ @fired_queue = Queue.new
20
+ @workhorse_class = workhorse_class
21
+ @exeption_handlers = exeption_handlers
22
+
23
+ @workhorses = []
24
+ hire(pool_size)
25
+ end
26
+
27
+ def count
28
+ @workhorses.count
29
+ end
30
+ alias_method :size, :count
31
+
32
+ def fire(count)
33
+ count.times { @queue.push(Event.new(:terminate, @fired_queue)) }
34
+ fired_count = 0
35
+ while fired_count != count
36
+ workhorse = @fired_queue.pop
37
+ workhorse.join
38
+ @workhorses.delete(workhorse)
39
+ fired_count += 1
40
+ end
41
+
42
+ true
43
+ end
44
+
45
+ def hire(count)
46
+ workhorses = Array.new(count) { hire_one }
47
+ @workhorses.concat(workhorses)
48
+
49
+ true
50
+ end
51
+
52
+ def inspect
53
+ "#<#{self.class}:0x%014x " \
54
+ "@count=#{count} " \
55
+ "@workhorse_class=#{workhorse_class} " \
56
+ "@queued_tasks_size=#{queued_tasks_size}>" % [object_id]
57
+ end
58
+
59
+ def on_exeption(&block)
60
+ @exeption_handlers << block
61
+ @workhorses.each { |w| w.on_exeption(&block) }
62
+
63
+ true
64
+ end
65
+
66
+ def receive(task_content = nil, &block)
67
+ @queue.push(Event.new(:perform, Task.new(task_content, &block)))
68
+ end
69
+
70
+ def queued_tasks_size
71
+ @queue.size
72
+ end
73
+
74
+ def quit
75
+ fire(count)
76
+ end
77
+
78
+ private
79
+
80
+ def hire_one
81
+ workhorse_class.new(@queue, exeption_handlers: exeption_handlers)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ module BlackCompany
2
+ class Task
3
+ attr_reader :content, :proc
4
+
5
+ def initialize(content, &block)
6
+ @content = content
7
+ @proc = block
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module BlackCompany
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,55 @@
1
+ module BlackCompany
2
+ class Workhorse
3
+ def initialize(queue, exeption_handlers: [])
4
+ @queue = queue
5
+ @active = true
6
+ @thread = Thread.new { work }
7
+ @exeption_handlers = [*exeption_handlers]
8
+ end
9
+
10
+ def alive?
11
+ @thread && @thread.alive?
12
+ end
13
+
14
+ def join
15
+ @thread.join
16
+ end
17
+
18
+ def on_exeption(&block)
19
+ @exeption_handlers << block
20
+ end
21
+
22
+ private
23
+
24
+ def handle(event)
25
+ case event.type
26
+ when :terminate
27
+ @active = false
28
+ event.data.push(self)
29
+ when :perform
30
+ perform(event.data)
31
+ end
32
+ end
33
+
34
+ def handle_exeption(e)
35
+ @exeption_handlers.each { |h| h.call(e) }
36
+ end
37
+
38
+ def perform(task)
39
+ serve(task.content, &task.proc)
40
+ rescue => e
41
+ handle_exeption(e)
42
+ end
43
+
44
+ def serve(content, &block)
45
+ block.call(content)
46
+ end
47
+
48
+ def work
49
+ while @active
50
+ event = @queue.pop
51
+ handle(event)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ require "black_company/pool"
2
+ require "black_company/version"
3
+
4
+ module BlackCompany
5
+ def self.start(*args)
6
+ Pool.new(*args)
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: black_company
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nownabe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - nownabe@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - black_company.gemspec
72
+ - lib/black_company.rb
73
+ - lib/black_company/event.rb
74
+ - lib/black_company/pool.rb
75
+ - lib/black_company/task.rb
76
+ - lib/black_company/version.rb
77
+ - lib/black_company/workhorse.rb
78
+ homepage: https://github.com/nownabe/black_company
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: 'BlackCompany provides workhorses. But there is a limit to the number of
101
+ them. So it''s a thread pool :yum:'
102
+ test_files: []
103
+ has_rdoc: