assistor 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: 076ce5c505b2401756f570d5cd6d7029620d5f8c
4
+ data.tar.gz: 8f0aa2a897cd547bb6d816809fdc1f57a8926ffb
5
+ SHA512:
6
+ metadata.gz: 00b36cf07fd1bec003667a0d2fd5e78d426eb1af3ffaa8b90410fda844b4baf1fb406c67e3a9a86f18ed21a9bf4478c72ccb441839a5f2d2ad629a8b8bce83ba
7
+ data.tar.gz: 96745595c9bb58ffe73c0a065213775ca501d5ccd3603c5010e5ff2b489833b1c11f5be9a0dabe4faff771e7ba0ec5d4af43dd8c1d8d7fb1c8982d0f22a32e83
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@tkowalewski.pl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tomasz Kowalewski
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,100 @@
1
+ # Assistor
2
+
3
+ Simple background processing framework for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'assistor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install assistor
20
+
21
+ ## Usage
22
+
23
+ ### 1. Signals
24
+ Assistor daemon respond to signals
25
+
26
+ * `QUIT` - Graceful shutdown, waits for assistants to finish their current request before finishing
27
+ * `TERM` / `INT` - Quick shutdown, kills all assistants immediately
28
+ * `USR1` - Increment the number of assistant processes by one
29
+ * `USR2` - Decrement the number of assistant processes by one
30
+
31
+ and assistants
32
+
33
+ * `QUIT` - Graceful shutdown, waits for assistant to finish their current request before finishing
34
+ * `TERM` / `INT` - Quick shutdown
35
+
36
+
37
+ ## Example
38
+ ```ruby
39
+ #!/usr/bin/env ruby
40
+
41
+ require 'assistor'
42
+
43
+ class ExampleJob < Assistor::AbstractJob
44
+ def initialize(i)
45
+ @i = i
46
+ end
47
+
48
+ def run
49
+ sleep @i
50
+ end
51
+
52
+ def fail(exception)
53
+ ExceptionHandler.handle exception
54
+ end
55
+ end
56
+
57
+ class ExampleQueue < Assistor::AbstractQueue
58
+ def initialize
59
+ @queue = []
60
+ end
61
+
62
+ def push(job)
63
+ @queue.push job
64
+ end
65
+
66
+ def pop
67
+ @queue.pop
68
+ end
69
+ end
70
+
71
+ queue = ExampleQueue.new
72
+ (1..100).each do |i|
73
+ queue.push ExampleJob.new i
74
+ end
75
+
76
+ configuration = Assistor::Configuration.new
77
+ configuration.id = 'example'
78
+ configuration.size = 3
79
+ configuration.delay = 0.2
80
+ configuration.log_file = File.join(File.expand_path('..', __FILE__), 'example.log')
81
+ configuration.pid_file = File.join(File.expand_path('..', __FILE__), 'example.pid')
82
+
83
+ Assistor::Daemon.new(configuration).run(queue)
84
+ ```
85
+
86
+ ## Development
87
+
88
+ 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.
89
+
90
+ 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).
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tkowalewski/assistor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
95
+
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
100
+
data/lib/assistor.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'logger'
2
+ require 'tmpdir'
3
+
4
+ require 'assistor/version'
5
+ require 'assistor/log_file'
6
+ require 'assistor/pid_file'
7
+ require 'assistor/configuration'
8
+ require 'assistor/abstract_job'
9
+ require 'assistor/abstract_queue'
10
+ require 'assistor/assistant'
11
+ require 'assistor/assistant_group'
12
+ require 'assistor/daemon'
13
+
14
+ module Assistor
15
+ end
@@ -0,0 +1,15 @@
1
+ module Assistor
2
+ class AbstractJob
3
+ def id
4
+ @id ||= SecureRandom.uuid
5
+ end
6
+
7
+ def run
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def fail(exception)
12
+ raise NotImplementedError
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Assistor
2
+ class AbstractQueue
3
+ def pop
4
+ raise NotImplementedError
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,112 @@
1
+ module Assistor
2
+ class Assistant
3
+ extend Forwardable
4
+
5
+ def_delegators :@configuration, :log_file
6
+
7
+ attr_reader :pid
8
+
9
+ def initialize(configuration, job)
10
+ @configuration = configuration
11
+ @job = job
12
+ end
13
+
14
+ def id
15
+ @id ||= [@configuration.id, @job.id].join ':'
16
+ end
17
+
18
+ def name
19
+ @name ||= [:assistor, id].join ':'
20
+ end
21
+
22
+ def assist
23
+ @pid = Process.fork do
24
+ start
25
+ process
26
+ stop
27
+ end
28
+ detach
29
+ rescue => e
30
+ log_file.error e
31
+ end
32
+
33
+ def assists?
34
+ Process.kill(0, pid)
35
+ rescue Errno::ESRCH
36
+ false
37
+ end
38
+
39
+ private
40
+
41
+ def start
42
+ log_file.reopen
43
+ handle_signals
44
+ set_process_title
45
+ end
46
+
47
+ def process
48
+ with_pid_file { @job.run }
49
+ rescue => e
50
+ log_file.error e
51
+ @job.fail e
52
+ end
53
+
54
+ def stop
55
+ return if pid_file.exist?
56
+ exit!(0)
57
+ rescue => e
58
+ log_file.error e
59
+ end
60
+
61
+ def stop!
62
+ pid_file.delete
63
+ exit!(1)
64
+ rescue => e
65
+ log_file.error e
66
+ end
67
+
68
+ def handle_signals
69
+ handle_term_signal
70
+ handle_int_signal
71
+ handle_quit_signal
72
+ end
73
+
74
+ def handle_term_signal
75
+ Signal.trap('TERM') do
76
+ Thread.new { stop! }.join
77
+ exit
78
+ end
79
+ end
80
+
81
+ def handle_int_signal
82
+ Signal.trap('INT') do
83
+ Thread.new { stop! }.join
84
+ exit
85
+ end
86
+ end
87
+
88
+ def handle_quit_signal
89
+ Signal.trap('QUIT') do
90
+ Thread.new { stop }.join
91
+ end
92
+ end
93
+
94
+ def set_process_title
95
+ $0 = name
96
+ end
97
+
98
+ def detach
99
+ Process.detach pid
100
+ end
101
+
102
+ def pid_file
103
+ @pid_file ||= PidFile.new File.join(Dir.tmpdir, format('%s.pid', Process.pid))
104
+ end
105
+
106
+ def with_pid_file
107
+ pid_file.create Process.pid
108
+ yield
109
+ pid_file.delete pid_file
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,50 @@
1
+ module Assistor
2
+ class AssistantGroup
3
+ attr_reader :size
4
+
5
+ def initialize(size)
6
+ @size = size
7
+ @assistants = []
8
+ end
9
+
10
+ def add(assistant)
11
+ @assistants << assistant
12
+ end
13
+
14
+ def flush
15
+ @assistants.delete_if { |assistant| !assistant.assists? }
16
+ end
17
+
18
+ def filled?
19
+ @assistants.size >= size
20
+ end
21
+
22
+ def wait
23
+ @assistants.each do |assistant|
24
+ begin
25
+ Process.wait(assistant.pid)
26
+ rescue Errno::ECHILD
27
+ next
28
+ end
29
+ end
30
+ end
31
+
32
+ def stop
33
+ @assistants.each do |assistant|
34
+ begin
35
+ Process.kill(:INT, assistant.pid)
36
+ rescue Errno::ESRCH
37
+ next
38
+ end
39
+ end
40
+ end
41
+
42
+ def increase
43
+ @size += 1
44
+ end
45
+
46
+ def decrease
47
+ @size -= 1
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ module Assistor
2
+ class Configuration
3
+ attr_accessor :id, :size, :delay
4
+ attr_reader :log_file, :pid_file
5
+
6
+ def log_file=(file)
7
+ @log_file = LogFile.new file
8
+ end
9
+
10
+ def pid_file=(file)
11
+ @pid_file = PidFile.new file
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,126 @@
1
+ module Assistor
2
+ class Daemon
3
+ extend Forwardable
4
+
5
+ def_delegators :@configuration, :id, :size, :delay, :log_file, :pid_file
6
+
7
+ def initialize(configuration)
8
+ @configuration = configuration
9
+ end
10
+
11
+ def name
12
+ [:assistor, id].join ':'
13
+ end
14
+
15
+ def run(queue)
16
+ start
17
+ loop do
18
+ sleep(delay) unless assist(queue)
19
+ end
20
+ stop
21
+ rescue => e
22
+ log_file.error e
23
+ end
24
+
25
+ private
26
+
27
+ def start
28
+ daemonize
29
+ log_file.reopen
30
+ handle_signals
31
+ set_process_title
32
+ pid_file.create pid
33
+ end
34
+
35
+ def stop
36
+ assistant_group.wait
37
+ assistant_group.flush
38
+ pid_file.delete
39
+ exit!(0)
40
+ rescue => e
41
+ log_file.error e
42
+ end
43
+
44
+ def stop!
45
+ assistant_group.stop
46
+ assistant_group.flush
47
+ pid_file.delete
48
+ exit!(1)
49
+ rescue => e
50
+ log_file.error e
51
+ end
52
+
53
+ def assist(queue)
54
+ assistant_group.flush
55
+ return if assistant_group.filled?
56
+ return unless job = queue.pop
57
+
58
+ assistant = Assistant.new(@configuration, job)
59
+ assistant.assist
60
+
61
+ assistant_group.add(assistant)
62
+ rescue => e
63
+ log_file.error e
64
+ end
65
+
66
+ def daemonize
67
+ Process.daemon(true, true)
68
+ end
69
+
70
+ def increase
71
+ assistant_group.increase
72
+ end
73
+
74
+ def decrease
75
+ assistant_group.decrease
76
+ end
77
+
78
+ def set_process_title
79
+ $0 = name
80
+ end
81
+
82
+ def pid
83
+ @pid ||= Process.pid
84
+ end
85
+
86
+ def handle_signals
87
+ handle_term_signal
88
+ handle_int_signal
89
+ handle_quit_signal
90
+ handle_usr1_signal
91
+ handle_usr2_signal
92
+ end
93
+
94
+ def handle_term_signal
95
+ Signal.trap('TERM') do
96
+ Thread.new { stop! }.join
97
+ exit
98
+ end
99
+ end
100
+
101
+ def handle_int_signal
102
+ Signal.trap('INT') do
103
+ Thread.new { stop! }.join
104
+ exit
105
+ end
106
+ end
107
+
108
+ def handle_quit_signal
109
+ Signal.trap('QUIT') do
110
+ Thread.new { stop }.join
111
+ end
112
+ end
113
+
114
+ def handle_usr1_signal
115
+ Signal.trap('USR1') { increase }
116
+ end
117
+
118
+ def handle_usr2_signal
119
+ Signal.trap('USR2') { decrease }
120
+ end
121
+
122
+ def assistant_group
123
+ @assistant_group ||= AssistantGroup.new(size)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,18 @@
1
+ module Assistor
2
+ class LogFile
3
+ extend Forwardable
4
+
5
+ def_delegators :@logger, :debug, :error, :fatal, :info, :unknown, :warn
6
+
7
+ def initialize(log_file)
8
+ @log_file = log_file
9
+ @logger = Logger.new(log_file)
10
+ end
11
+
12
+ def reopen
13
+ $stderr.reopen @log_file, 'a+'
14
+ $stdout.reopen $stderr
15
+ $stdout.sync = $stderr.sync = true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Assistor
2
+ class PidFile
3
+ def initialize(pid_file)
4
+ @pid_file = pid_file
5
+ end
6
+
7
+ def create(pid)
8
+ File.write @pid_file, pid
9
+ end
10
+
11
+ def delete
12
+ File.delete @pid_file
13
+ end
14
+
15
+ def exist?
16
+ File.exist? @pid_file
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Assistor
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assistor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Kowalewski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-31 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.44.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.44.1
69
+ description: Simple background processing framework for Ruby
70
+ email:
71
+ - me@tkowalewski.pl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CHANGELOG.md
77
+ - CODE_OF_CONDUCT.md
78
+ - LICENSE.txt
79
+ - README.md
80
+ - lib/assistor.rb
81
+ - lib/assistor/abstract_job.rb
82
+ - lib/assistor/abstract_queue.rb
83
+ - lib/assistor/assistant.rb
84
+ - lib/assistor/assistant_group.rb
85
+ - lib/assistor/configuration.rb
86
+ - lib/assistor/daemon.rb
87
+ - lib/assistor/log_file.rb
88
+ - lib/assistor/pid_file.rb
89
+ - lib/assistor/version.rb
90
+ homepage: https://github.com/tkowalewski/assistor
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Simple background processing framework for Ruby
114
+ test_files: []