thread_watcher 0.4.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: a61335ce0dc5a9aaf5671d1c4628d6de2a4a0cc7
4
+ data.tar.gz: 824bb5d6f27b8c9e753ab50ebbefe270ba2c2620
5
+ SHA512:
6
+ metadata.gz: a4ce646a5ad5f0733f9591c07131cb898549c1cdf83cbac7e94c17ee025f8116af5a505fd9450e2e5401d03d1840300151df33c915bf28a2814cc033fa9eb32b
7
+ data.tar.gz: 5d0d622c6599299d2341f3b914d0bfdd8bfa615f650fdd9cd82a8e12a8eec5c6c41fd128be76490e96f812d375c82e3b4a419636d036df24287033dbec2abcf5
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,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thread_watcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Robert Starke (robertst81 (at) gmail com )
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,105 @@
1
+ # ThreadWatcher
2
+
3
+ You need to monitor your threads and kill one specific thread in another part of your application? Then this Gem could be usefull for you. Use it with any ruby version greater or equal than 1.8.7
4
+
5
+
6
+ ## Installation
7
+
8
+ hm, yeah. just add this to your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'thread_watcher', '~> 0.4.0'
12
+ ```
13
+
14
+ And then execute:
15
+ ```
16
+ $ bundle
17
+ ```
18
+ Or install it yourself as:
19
+ ```
20
+ $ gem install thread_watcher
21
+ ```
22
+
23
+ Huh, ready to use!
24
+
25
+ ## Usage
26
+
27
+ Ok, let's say you want to run a big process separatly in a thread.
28
+ Something big like sleep(10) ;).
29
+
30
+ ```ruby
31
+ ThreadWatcher::Monitor.run { sleep 10 }
32
+ ```
33
+
34
+ Run needs a block to work and return the internal process id.
35
+ This could be something like `1452333019`
36
+
37
+ If your thread is ok, so let them work.
38
+ ThreadWatcher starts automaticly a cleaning task to kill dead threads every minute.
39
+
40
+ Let's say you type something like
41
+
42
+ ```ruby
43
+ ThreadWatcher::Monitor.run { sleep 10000000 }
44
+ ```
45
+
46
+ And you want to kill this worker. So just use the process id to kill the thread
47
+
48
+ ```ruby
49
+ ThreadWatcher::Monitor.kill 1452333224
50
+ ```
51
+
52
+ Your thread is now killed.
53
+
54
+
55
+ If you need more information about these threads so use
56
+
57
+ ```ruby
58
+ ThreadWatcher::Monitor.status
59
+ ```
60
+
61
+ And you get a simple overview like
62
+
63
+ ```
64
+ |ID |Running? |Runtime in Seconds |
65
+ |1452405225 |true | 120 |
66
+ |1452405227 |true | 118 |
67
+ |1452405228 |true | 117 |
68
+ |1452405334 |false | 11 |
69
+
70
+ ```
71
+
72
+
73
+ ## The MIT License (MIT)
74
+
75
+ Copyright (c) 2015 [Robert Starke](robertst81+github@gmail.com)
76
+
77
+ Permission is hereby granted, free of charge, to any person obtaining a copy
78
+ of this software and associated documentation files (the "Software"), to deal
79
+ in the Software without restriction, including without limitation the rights
80
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
81
+ copies of the Software, and to permit persons to whom the Software is
82
+ furnished to do so, subject to the following conditions:
83
+
84
+ The above copyright notice and this permission notice shall be included in
85
+ all copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
88
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
89
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
90
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
91
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
92
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
93
+ THE SOFTWARE.
94
+
95
+ ## Questions?
96
+
97
+ If you have further questions, code smells, hints or a beer, just contact me :)
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it ( https://github.com/robst/thread_watcher/fork )
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "thread_watcher"
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,64 @@
1
+ module ThreadWatcher
2
+ class ProcessWatch
3
+ attr_accessor :threads
4
+ class ThreadHolder
5
+ attr_accessor :thread, :id
6
+ def initialize thread
7
+ @thread = thread
8
+ @id = time_to_i
9
+ end
10
+
11
+ def stop!
12
+ @thread.kill
13
+ end
14
+
15
+ def alive?
16
+ @thread.alive?
17
+ end
18
+
19
+ def to_s
20
+ "|#{id}\t|#{alive?}\t\t|\t#{runtime}\t\t|"
21
+ end
22
+
23
+ def runtime
24
+ time_to_i - @id
25
+ end
26
+
27
+ def time_to_i
28
+ Time.now.to_i
29
+ end
30
+ end
31
+
32
+ def initialize
33
+ @threads = {}
34
+ run { while true; self.clear!; sleep(60); end; }
35
+ end
36
+
37
+ def run &block
38
+ thread_holder = ThreadHolder.new(Thread.new { block.call })
39
+ @threads[thread_holder.id] = thread_holder
40
+ thread_holder.id
41
+ end
42
+
43
+ def kill id
44
+ @threads[id].stop!
45
+ @threads.delete id
46
+ end
47
+
48
+ def clear!
49
+ @threads.each do |key, thread|
50
+ next if thread.alive?
51
+ kill key
52
+ end
53
+ end
54
+
55
+ def status
56
+ puts "|ID\t\t|Running?\t|Runtime in Seconds\t|"
57
+ @threads.each do |key, thread|
58
+ puts thread.to_s
59
+ end
60
+ ''
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module ThreadWatcher
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ require "thread_watcher/version"
2
+ require "thread_watcher/process_watch"
3
+ module ThreadWatcher
4
+ require 'singleton'
5
+ class Monitor
6
+ include Singleton
7
+
8
+ def initialize
9
+ @process_watch = ThreadWatcher::ProcessWatch.new
10
+ end
11
+
12
+ def run &block
13
+ @process_watch.run &block
14
+ end
15
+
16
+ def self.run &block
17
+ instance.run &block
18
+ end
19
+
20
+ def kill id
21
+ @process_watch.kill id
22
+ end
23
+
24
+ def self.kill id
25
+ instance.kill id
26
+ end
27
+
28
+ def status
29
+ @process_watch.status
30
+ end
31
+
32
+ def self.status
33
+ instance.status
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thread_watcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thread_watcher"
8
+ spec.version = ThreadWatcher::VERSION
9
+ spec.authors = ["Robert Starke"]
10
+ spec.email = ["robertst81@gmail.com"]
11
+
12
+
13
+ spec.summary = %q{Monitor your Threads exactly and kill them from each Point of your Application.}
14
+ spec.description = %q{You need to monitor your threads and kill one specific thread in another part of your application? Then this Gem could be usefull for you. Use it with any ruby version greater or equal than 1.8.7. See detailed information at the github Page.}
15
+ spec.homepage = "https://github.com/robst/thread_watcher"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.required_ruby_version = '>= 1.8.7'
27
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_watcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Starke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-10 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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
+ description: You need to monitor your threads and kill one specific thread in another
42
+ part of your application? Then this Gem could be usefull for you. Use it with any
43
+ ruby version greater or equal than 1.8.7. See detailed information at the github
44
+ Page.
45
+ email:
46
+ - robertst81@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - ".rspec"
53
+ - ".travis.yml"
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - lib/thread_watcher.rb
61
+ - lib/thread_watcher/process_watch.rb
62
+ - lib/thread_watcher/version.rb
63
+ - thread_watcher.gemspec
64
+ homepage: https://github.com/robst/thread_watcher
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.7
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Monitor your Threads exactly and kill them from each Point of your Application.
88
+ test_files: []