tricky_signals 0.1.0

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
+ SHA1:
3
+ metadata.gz: 5bae01d6def11c743e015c04e98c535fcedffc7a
4
+ data.tar.gz: 6566524445724705862fd73938ac86ce5c17fe3c
5
+ SHA512:
6
+ metadata.gz: a0819793f214f9ebf5462447d138621afe780fb4ad4009caf0f5dcd49a0dba779961fc3a22f791d288e0d1310cea944b7288a29e2595fdccbed1402d56b5106c
7
+ data.tar.gz: 0c61f0c2c319c6c5bdc2e45ccba1f69f5452c6565750ac01c2c0fca70a4a492f8291acac25f0e74fc92dd325d8c88b33d8a09e1087a27d3f5c87fb5084ee8e5b
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,8 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Vladimir Kochnev
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,111 @@
1
+ tricky_signals
2
+ ==============
3
+
4
+ [![Build Status](https://travis-ci.org/marshall-lee/tricky_signals.svg)](https://travis-ci.org/marshall-lee/tricky_signals)
5
+
6
+ ![](https://pbs.twimg.com/media/CO4aDLlUAAAjMuA.png)
7
+
8
+ This gem aims to solve the problem with...
9
+
10
+ ```ruby
11
+ logger = Logger.new(STDOUT)
12
+ trap('USR1') do
13
+ logger.info 'hello!'
14
+ end
15
+ ```
16
+
17
+ And then:
18
+
19
+ ```
20
+ kill -USR1 <pid>
21
+ ```
22
+
23
+ What we get:
24
+
25
+ ```
26
+ log writing failed. can't be called from trap context
27
+ ```
28
+
29
+ Looks familiar? Then `tricky_signals` is your friend!
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ ```ruby
36
+ gem 'tricky_signals'
37
+ ```
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install tricky_signals
46
+
47
+ ## Usage
48
+
49
+ ### Global trap handlers
50
+
51
+ ```ruby
52
+ logger = Logger.new(STDOUT)
53
+
54
+ # ...
55
+
56
+ TrickySignals.global.trap(:USR1) do
57
+ Thread.list.each do |thread|
58
+ logger.debug { "Thread object_id=#{thread.object_id}" }
59
+ if thread.backtrace
60
+ logger.debug { thread.backtrace.join("\n") }
61
+ else
62
+ logger.debug '<no backtrace available>'
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ ### Global untrap
69
+
70
+ ```ruby
71
+ TrickySignals.global.untrap(:USR1)
72
+ ```
73
+
74
+ ### Manual starting of service
75
+
76
+ ```ruby
77
+ TrickySignals.start! do |signals|
78
+ signals.trap(:USR1) { }
79
+ # subscribed to USR1
80
+ signals.trap(:TTIN) { }
81
+ # subscribed to TTIN
82
+ # ...
83
+ end
84
+ # unsubscribed from USR1 and TTIN
85
+ ```
86
+
87
+ #### Manual stop
88
+ ```ruby
89
+ signals = TrickySignals.start!
90
+ signals.trap(:USR1) { }
91
+ signals.trap(:TTIN) { }
92
+ # ...
93
+ signals.stop!
94
+ # unsubscribed from USR1 and TTIN
95
+ ```
96
+
97
+ ## Development
98
+
99
+ 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.
100
+
101
+ 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).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marshall-lee/tricky_signals.
106
+
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
111
+
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 "tricky_signals"
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,34 @@
1
+ require 'singleton'
2
+ require 'forwardable'
3
+
4
+ class TrickySignals
5
+ class << self
6
+ def global
7
+ Global.instance
8
+ end
9
+ end
10
+
11
+ class Global < TrickySignals
12
+ include Singleton
13
+
14
+ class << self
15
+ extend Forwardable
16
+
17
+ def_delegators :instance,
18
+ :start!,
19
+ :stop!,
20
+ :trap,
21
+ :untrap,
22
+ :untrap_all,
23
+ :ignore_on,
24
+ :default_on,
25
+ :exit_on,
26
+ :system_default_on
27
+ end
28
+
29
+ def initialize
30
+ super
31
+ start!
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ class TrickySignals
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,202 @@
1
+ require 'thread'
2
+ require 'monitor.rb'
3
+
4
+ class TrickySignals
5
+ class Handlers
6
+ end
7
+
8
+ class << self
9
+ def start!
10
+ obj = new.start!
11
+ if block_given?
12
+ yield obj
13
+ obj.stop!
14
+ else
15
+ obj
16
+ end
17
+ end
18
+
19
+ def handlers_class
20
+ const_get(:Handlers)
21
+ end
22
+ end
23
+
24
+ include MonitorMixin
25
+
26
+ def initialize
27
+ super
28
+ @started = false
29
+ end
30
+
31
+ def start!
32
+ synchronize do
33
+ fail "cannot start #{self}: already started!" if @started
34
+ @started = true
35
+
36
+ @handlers = self.class.handlers_class.new
37
+ @previous = {}
38
+ @io_thread = start_io
39
+ self
40
+ end
41
+ end
42
+
43
+ def stop!
44
+ synchronize do
45
+ fail "cannot stop #{self}: not started or stopped" unless @started
46
+
47
+ untrap_all
48
+
49
+ stop_io
50
+ @previous = nil
51
+ @handlers = nil
52
+
53
+ @started = false
54
+ nil
55
+ end
56
+ end
57
+
58
+ def started?
59
+ @started
60
+ end
61
+
62
+ def handlers
63
+ synchronize do
64
+ check_started!
65
+ @handlers
66
+ end
67
+ end
68
+
69
+ def setup_handlers
70
+ fail 'you should pass a block to `setup_handlers`' unless block_given?
71
+
72
+ synchronize do
73
+ check_started!
74
+ @handlers.instance_eval(&proc)
75
+ end
76
+ end
77
+
78
+ def trap(signal, command = nil)
79
+ synchronize do
80
+ check_started!
81
+ signal = stringify_signal(signal)
82
+ prev_handler =
83
+ if block_given?
84
+ trap_with_block(signal, &proc)
85
+ else
86
+ trap_with_command(signal, command)
87
+ end
88
+ @previous[signal] = prev_handler unless @previous.key? signal
89
+ prev_handler
90
+ end
91
+ end
92
+
93
+ def untrap(signal)
94
+ synchronize do
95
+ check_started!
96
+ signal = stringify_signal(signal)
97
+ if @previous.key? signal
98
+ trap_with_command(signal, @previous[signal])
99
+ @previous.delete signal
100
+ end
101
+ end
102
+ end
103
+
104
+ def untrap_all
105
+ synchronize do
106
+ check_started!
107
+ @previous.keys.each { |signal| untrap signal }
108
+ end
109
+ end
110
+
111
+ {
112
+ ignore: 'IGNORE',
113
+ default: 'DEFAULT',
114
+ exit: 'EXIT',
115
+ system_default: 'SYSTEM_DEFAULT'
116
+ }.each do |name, command|
117
+ name = "#{name}_on"
118
+
119
+ define_method(name) do |signal, &block|
120
+ trap(signal, command)
121
+ end
122
+ end
123
+
124
+ private
125
+
126
+ def check_started!
127
+ fail "#{self} is not started yet!" unless @started
128
+ end
129
+
130
+ def start_io
131
+ @io_read, @io_write = IO.pipe
132
+
133
+ Thread.new do
134
+ while ios = IO.select([@io_read])
135
+ input = ios.first.first.gets
136
+ break unless input
137
+ signal = input.chomp
138
+
139
+ synchronize do
140
+ handler = @handlers.public_method("handle_#{signal}")
141
+ case handler.arity
142
+ when 0 then handler.call
143
+ when 1 then handler.call signal
144
+ when 2 then handler.call signal, @previous[signal]
145
+ end
146
+ end
147
+ end
148
+
149
+ @io_read.close
150
+ end
151
+ end
152
+
153
+ def stop_io
154
+ @io_write.close
155
+ @io_thread.join
156
+ @io_thread = nil
157
+ @io_read = nil
158
+ @io_write = nil
159
+ end
160
+
161
+ def trap_with_block(signal, &block)
162
+ @handlers.define_singleton_method("handle_#{signal}", &block)
163
+ Signal.trap(signal) do
164
+ @io_write.puts(signal)
165
+ end
166
+ end
167
+
168
+ def trap_with_command(signal, command)
169
+ sclass = @handlers.singleton_class
170
+ if sclass.method_defined?(method_name_for(signal))
171
+ sclass.send(:remove_method, method_name_for(signal))
172
+ end
173
+ Signal.trap(signal, command)
174
+ end
175
+
176
+ if Signal.respond_to? :signame
177
+ def signame(signal)
178
+ Signal.signame(signal).to_s
179
+ end
180
+ else
181
+ def signame(signal)
182
+ found = Signal.list.find { |name, i| i == signal }
183
+ fail ArgumentError, "unknow signal #{signal}" unless found
184
+ found.first.to_s
185
+ end
186
+ end
187
+
188
+ def stringify_signal(signal)
189
+ if signal.is_a? Fixnum
190
+ signame(signal)
191
+ else
192
+ signal.to_s
193
+ end
194
+ end
195
+
196
+ def method_name_for(signal)
197
+ "handle_#{signal}"
198
+ end
199
+ end
200
+
201
+ require 'tricky_signals/global'
202
+ require 'tricky_signals/version'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tricky_signals/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tricky_signals"
8
+ spec.version = TrickySignals::VERSION
9
+ spec.authors = ["Vladimir Kochnev"]
10
+ spec.email = ["hashtable@yandex.ru"]
11
+
12
+ spec.summary = "Escaping Ruby's Signal.trap!"
13
+ spec.homepage = "https://github.com/marshall-lee/tricky_signals"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tricky_signals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Kochnev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-14 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
+ - hashtable@yandex.ru
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
+ - lib/tricky_signals.rb
72
+ - lib/tricky_signals/global.rb
73
+ - lib/tricky_signals/version.rb
74
+ - tricky_signals.gemspec
75
+ homepage: https://github.com/marshall-lee/tricky_signals
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.8
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Escaping Ruby's Signal.trap!
99
+ test_files: []