runify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/Rakefile +9 -0
- data/lib/runify.rb +33 -0
- data/lib/runify/version.rb +9 -0
- data/runify.gemspec +23 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2159c4ba2f63ddc75363f6c267768f71a2d396bb
|
4
|
+
data.tar.gz: 2159f9f1a0351e07b372d9afbe7d4099dbe283e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 118aca90ae253b5dad52c1a6502432e24f7bd701157c891db3df249a8f3147c204e0ba4ecd4885140987d36bcc396b8bee6e6f2df23cdf1b0bf82d47032013d4
|
7
|
+
data.tar.gz: ff276990c11ba2bf5ca3e535c5fe6ae791c06ef50ce5a6ab430cd354f2eaf3b31ecedee44eca047059459c3c8e20e5a928838caf312352c0b01764e4dc98be55
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Paul Duncan
|
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,101 @@
|
|
1
|
+
# Runify
|
2
|
+
|
3
|
+
Easily turn any object into a service
|
4
|
+
|
5
|
+
## Presentation
|
6
|
+
|
7
|
+
This library provides a very simple interface for any 'runnable' object (responds to _run_) to be used as a service.
|
8
|
+
When included into a class, the *Runify* module adds _startup_, _shutdown_ and _restart_ methods, which take care of wrapping execution of the _run_ method into a thread.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
### Gemfile
|
13
|
+
```ruby
|
14
|
+
gem 'runify'
|
15
|
+
```
|
16
|
+
|
17
|
+
### Terminal
|
18
|
+
```bash
|
19
|
+
gem install -V runify
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
For any object that features a _run_ method, simply include the *Runify* module into the class.
|
25
|
+
Instances of this class will respond to the following new methods:
|
26
|
+
|
27
|
+
#### *startup*
|
28
|
+
|
29
|
+
Calling this method will spawn a new thread around the _run_ method, effectively starting the service in the 'background'.
|
30
|
+
|
31
|
+
#### *shutdown*
|
32
|
+
|
33
|
+
Calling _shutdown_ will request the service to terminate by setting *@stop* to *true* and wait for the thread to complete (join).
|
34
|
+
Once this method returns, the service has completely terminated.
|
35
|
+
|
36
|
+
#### *restart*
|
37
|
+
|
38
|
+
The _restart_ method is nothing more than a shortcut to shutdown and startup:
|
39
|
+
```ruby
|
40
|
+
def restart
|
41
|
+
shutdown
|
42
|
+
startup
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
#### The *@stop* variable
|
47
|
+
|
48
|
+
Any class that includes the runify module should check the *@stop* instance variable in its _run_ method.
|
49
|
+
Runify will set @stop to _true_ to indicate execution should end and the _run_ method should return when a shutdown is requested.
|
50
|
+
|
51
|
+
A typical _run_ method will usually be implemented as such:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
def run
|
55
|
+
update until @stop # Run until shutdown is requested
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
## Examples
|
60
|
+
|
61
|
+
### Runifying a simple service class
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
require 'runify'
|
65
|
+
|
66
|
+
# Foo Service
|
67
|
+
class FooService
|
68
|
+
|
69
|
+
# Runify
|
70
|
+
include Runify
|
71
|
+
|
72
|
+
def run
|
73
|
+
update until @stop # Run until shutdown is requested
|
74
|
+
end
|
75
|
+
|
76
|
+
def update
|
77
|
+
# Business Logic goes here...
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Start Foo Service in the background
|
82
|
+
fs = FooService.new
|
83
|
+
fs.startup
|
84
|
+
puts 'Foo Service has started'
|
85
|
+
|
86
|
+
# Do something while service is running...
|
87
|
+
|
88
|
+
# Restart Foo Service
|
89
|
+
fs.restart
|
90
|
+
puts 'Foo Service has restarted'
|
91
|
+
|
92
|
+
# Do something while service is running...
|
93
|
+
|
94
|
+
# Shutdown Foo Service
|
95
|
+
fs.shutdown
|
96
|
+
puts 'Foo Service has terminated'
|
97
|
+
```
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/runify.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Runify
|
2
|
+
# by Eresse <eresse@eresse.net>
|
3
|
+
|
4
|
+
# External Includes
|
5
|
+
require 'thread'
|
6
|
+
|
7
|
+
# Runify Module
|
8
|
+
# Root Module for Runify
|
9
|
+
module Runify
|
10
|
+
|
11
|
+
# Startup
|
12
|
+
# Spawns a new Thread around the _run_ method
|
13
|
+
def startup
|
14
|
+
return if @thread
|
15
|
+
@stop = nil
|
16
|
+
@thread = Thread.new { run }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Shutdown
|
20
|
+
# Requests the _run_ method to complete and blocks until it returns
|
21
|
+
def shutdown
|
22
|
+
@stop = true
|
23
|
+
@thread.join if @thread
|
24
|
+
@thread = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# Restart
|
28
|
+
# Calls _shutdown_, immediately followed by _startup_
|
29
|
+
def restart
|
30
|
+
shutdown
|
31
|
+
startup
|
32
|
+
end
|
33
|
+
end
|
data/runify.gemspec
ADDED
@@ -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 'runify/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'runify'
|
8
|
+
spec.version = Runify::VERSION
|
9
|
+
spec.authors = ['Eresse']
|
10
|
+
spec.email = ['eresse@eresse.net']
|
11
|
+
|
12
|
+
spec.summary = 'Easily turn any object into a service'
|
13
|
+
spec.description = 'Provides a very simple interface for any \'runnable\' object (responds to _run_) to be used as a service'
|
14
|
+
spec.homepage = 'http://redmine.eresse.net/projects/runify'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_runtime_dependency 'minitest'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: runify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eresse
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-28 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Provides a very simple interface for any 'runnable' object (responds
|
56
|
+
to _run_) to be used as a service
|
57
|
+
email:
|
58
|
+
- eresse@eresse.net
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/runify.rb
|
69
|
+
- lib/runify/version.rb
|
70
|
+
- runify.gemspec
|
71
|
+
homepage: http://redmine.eresse.net/projects/runify
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.6.10
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Easily turn any object into a service
|
95
|
+
test_files: []
|