service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/Gemfile +3 -0
  2. data/Gemfile.lock +44 -0
  3. data/LICENSE +20 -0
  4. data/README.md +166 -0
  5. data/Rakefile +57 -0
  6. data/VERSION +1 -0
  7. data/lib/service.rb +81 -0
  8. data/service.gemspec +44 -0
  9. metadata +153 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ service (1.0.0)
5
+ version (~> 1.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ coderay (1.0.8)
11
+ github-markup (0.7.4)
12
+ guard (1.5.4)
13
+ listen (>= 0.4.2)
14
+ lumberjack (>= 1.0.2)
15
+ pry (>= 0.9.10)
16
+ thor (>= 0.14.6)
17
+ guard-yard (2.0.1)
18
+ guard (>= 1.1.0)
19
+ yard (>= 0.7.0)
20
+ listen (0.6.0)
21
+ lumberjack (1.0.2)
22
+ method_source (0.8.1)
23
+ pry (0.9.10)
24
+ coderay (~> 1.0.5)
25
+ method_source (~> 0.8)
26
+ slop (~> 3.3.1)
27
+ rake (10.0.3)
28
+ rb-fsevent (0.9.2)
29
+ redcarpet (2.2.2)
30
+ slop (3.3.3)
31
+ thor (0.16.0)
32
+ version (1.0.0)
33
+ yard (0.8.3)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ github-markup (~> 0.7)
40
+ guard-yard (~> 2.0)
41
+ rake (~> 10.0)
42
+ rb-fsevent (~> 0.9)
43
+ redcarpet (~> 2.2.2)
44
+ service!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ryan Scott Lewis <ryan@rynet.us>.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # Service
2
+
3
+ __serv·ice__ _noun_ _/ˈsɜr vɪs/_ __:__ The action of helping or doing work for someone.
4
+
5
+ Service encapsulates an object which executes a bit of code in a loop that can be started or
6
+ stopped and query whether it is running or not.
7
+
8
+ ## Install
9
+
10
+ ### Bundler: `gem 'service'`
11
+
12
+ ### RubyGems: `gem install service`
13
+
14
+ ## Usage
15
+
16
+ ### Defining
17
+
18
+ You can define an Object as a Service by subclassing `Service` or by including/extending
19
+ `Service::Base`.
20
+
21
+ ```ruby
22
+ class ServiceA < Service
23
+ end
24
+
25
+ class ServiceB
26
+ include Service::Base
27
+ end
28
+
29
+ class ServiceC
30
+ extend Service::Base
31
+ end
32
+ ```
33
+
34
+ A `Service` object stores it's state in the `@_service_state` instance variable as to be as
35
+ unobtrusive as possible when integrating with your custom objects.
36
+
37
+ The next thing to do is define a `execute` instance method on your object:
38
+
39
+ ```ruby
40
+ class MyService < Service
41
+ def execute
42
+ # ...
43
+ end
44
+ end
45
+ ```
46
+
47
+ ### Running
48
+
49
+ Use the `start`/`run` instance method to call the `execute` instance method in a loop.
50
+
51
+ ```ruby
52
+ class MyService < Service
53
+ def execute
54
+ puts "Hello"
55
+ end
56
+ end
57
+
58
+ MyService.new.run
59
+ # => Hello
60
+ # => Hello
61
+ # => ...
62
+ ```
63
+
64
+ Use `start!`/`run!` to call the `run` in a new Thread.
65
+
66
+ ```ruby
67
+ class MyService < Service
68
+ def execute
69
+ puts "Hello"
70
+ end
71
+ end
72
+
73
+ thread = MyService.new.run!
74
+ thread.join
75
+ # => Hello
76
+ # => Hello
77
+ # => ...
78
+ ```
79
+
80
+ ### Stopping
81
+
82
+ Use the `stop` instance method break the run loop.
83
+ This will also kill the Thread it is running in, if running in a Thread.
84
+
85
+ ```ruby
86
+ class MyService < Service
87
+ def execute
88
+ sleep 3
89
+ stop
90
+ end
91
+ end
92
+
93
+ print "Running MyService in a new Thread... "
94
+ thread = MyService.new.run!
95
+ thread.join
96
+ puts "Done!"
97
+ ```
98
+
99
+ ### Querying State
100
+
101
+ Use the `started?`/`running?` or `stopped?` instance methods to determine the current state of
102
+ the Service instance.
103
+
104
+ ```ruby
105
+ class MyService < Service
106
+ def execute
107
+ sleep 10
108
+ end
109
+ end
110
+
111
+ service = MyService.new
112
+ p service.running? # => false
113
+ service.run!
114
+ sleep 1
115
+ p service.running? # => true
116
+ ```
117
+
118
+ ## Example
119
+
120
+ ```ruby
121
+ require 'service'
122
+
123
+ class PingService < Service
124
+ def execute
125
+ sleep rand(5)
126
+ print 'ping'
127
+ stop
128
+ end
129
+ end
130
+
131
+ class PongService
132
+ include Service::Base
133
+
134
+ def execute
135
+ sleep rand(5)
136
+ print 'pong'
137
+ stop
138
+ end
139
+ end
140
+
141
+ class ExcitementService
142
+ extend Service::Base
143
+
144
+ def execute
145
+ sleep rand(5)
146
+ print '!'
147
+ stop
148
+ end
149
+ end
150
+
151
+ threads = []
152
+ threads << PingService.new.run!
153
+ threads << PongService.new.run!
154
+ threads << ExcitementService.new.run!
155
+
156
+ threads.each(&:join)
157
+ ```
158
+
159
+ ## Copyright
160
+
161
+ Copyright © 2012 Ryan Scott Lewis <ryan@rynet.us>.
162
+
163
+ The MIT License (MIT) - See LICENSE for further details.
164
+
165
+ [message_queue]: http://en.wikipedia.org/wiki/Message_queue
166
+ [queue]: http://rubydoc.info/stdlib/thread/Queue
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'pathname'
2
+
3
+ def require_task(path)
4
+ begin
5
+ require path
6
+
7
+ yield
8
+ rescue LoadError
9
+ puts '', "Could not load '#{path}'.", 'Try to `rake gem:spec` and `bundle install` and try again.', ''
10
+ end
11
+ end
12
+
13
+ spec = Gem::Specification.new do |s|
14
+
15
+ # Variables
16
+ s.name = 'service'
17
+ s.author = 'Ryan Scott Lewis'
18
+ s.email = 'ryan@rynet.us'
19
+ s.summary = 'A basic implementation of a Service, which has a run loop can be start and stopped or run an a new Thread.'
20
+ s.description = 'Service encapsulates an object which executes a bit of code in a loop that can be started or stopped and query whether it is running or not.'
21
+
22
+ # Dependencies
23
+ s.add_dependency 'version', '~> 1.0'
24
+ s.add_development_dependency 'rake', '~> 10.0'
25
+ s.add_development_dependency 'guard-yard', '~> 2.0'
26
+ s.add_development_dependency 'rb-fsevent', '~> 0.9'
27
+ s.add_development_dependency 'redcarpet', '~> 2.2.2'
28
+ s.add_development_dependency 'github-markup', '~> 0.7'
29
+
30
+ # Pragmatically set variables
31
+ s.homepage = "http://github.com/RyanScottLewis/#{s.name}"
32
+ s.version = Pathname.glob('VERSION*').first.read
33
+ s.require_paths = ['lib']
34
+ s.files = `git ls-files`.lines.to_a.collect { |s| s.strip }
35
+ s.executables = `git ls-files -- bin/*`.lines.to_a.collect { |s| File.basename(s.strip) }
36
+
37
+ end
38
+
39
+ desc 'Generate the gemspec defined in this Rakefile'
40
+ task :gemspec do
41
+ Pathname.new("#{spec.name}.gemspec").open('w') { |f| f.write(spec.to_ruby) }
42
+ end
43
+
44
+ require_task 'rake/version_task' do
45
+ Rake::VersionTask.new do |t|
46
+ t.with_git_tag = true
47
+ t.with_gemspec = spec
48
+ end
49
+ end
50
+
51
+ require 'rubygems/package_task'
52
+ Gem::PackageTask.new(spec) do |t|
53
+ t.need_zip = false
54
+ t.need_tar = false
55
+ end
56
+
57
+ task :default => :gemspec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/service.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'version'
2
+
3
+ # Service encapsulates an object which executes a bit of code in a loop
4
+ # that can be started or stopped and query whether it is running or not.
5
+ #
6
+ # Objects can subclass Service.
7
+ class Service
8
+ is_versioned
9
+
10
+ # The Base module for Services.
11
+ # Objects can include or extend Service::Base.
12
+ module Base
13
+
14
+ # The instance methods to be mixed into modules which include/extend Service::Base.
15
+ module InstanceMethods
16
+
17
+ # Query if the service is currently stopped.
18
+ #
19
+ # @returns [true, false]
20
+ def stopped?
21
+ @_service_state == :stopped
22
+ end
23
+
24
+ # Query if the service is currently started.
25
+ #
26
+ # @returns [true, false]
27
+ def started?
28
+ @_service_state == :started
29
+ end
30
+ alias_method :running?, :started?
31
+
32
+ # The code that will be executed within the run loop.
33
+ # @abstract Subclass and override {#run} to implement a custom Service.
34
+ def execute
35
+ raise NotImplementedError
36
+ end
37
+
38
+ # Stop the run loop.
39
+ def stop
40
+ @_service_state = :stopped
41
+ end
42
+
43
+ # Start the run loop.
44
+ def start
45
+ @_service_state = :started
46
+ loop do
47
+ break if stopped?
48
+ execute
49
+ end
50
+ end
51
+ alias_method :run, :start
52
+
53
+ # Start the run loop in a new Thread.
54
+ #
55
+ # @return [Thread]
56
+ def start!
57
+ Thread.new { start }
58
+ end
59
+ alias_method :run!, :start!
60
+
61
+ end
62
+
63
+ class << self
64
+
65
+ # Include Service::Base::InstanceMethods when Service::Base is included.
66
+ def included(base)
67
+ base.send(:include, InstanceMethods)
68
+ end
69
+
70
+ # Include Service::Base::InstanceMethods when Service::Base is extended.
71
+ def extended(base)
72
+ base.send(:include, InstanceMethods)
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ include Base
80
+
81
+ end
data/service.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "service"
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ryan Scott Lewis"]
9
+ s.date = "2012-12-13"
10
+ s.description = "Service encapsulates an object which executes a bit of code in a loop that can be started or stopped and query whether it is running or not."
11
+ s.email = "ryan@rynet.us"
12
+ s.files = ["Gemfile", "Gemfile.lock", "LICENSE", "README.md", "Rakefile", "VERSION", "lib/service.rb", "service.gemspec"]
13
+ s.homepage = "http://github.com/RyanScottLewis/service"
14
+ s.require_paths = ["lib"]
15
+ s.rubygems_version = "1.8.24"
16
+ s.summary = "A basic implementation of a Service, which has a run loop can be start and stopped or run an a new Thread."
17
+
18
+ if s.respond_to? :specification_version then
19
+ s.specification_version = 3
20
+
21
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
22
+ s.add_runtime_dependency(%q<version>, ["~> 1.0"])
23
+ s.add_development_dependency(%q<rake>, ["~> 10.0"])
24
+ s.add_development_dependency(%q<guard-yard>, ["~> 2.0"])
25
+ s.add_development_dependency(%q<rb-fsevent>, ["~> 0.9"])
26
+ s.add_development_dependency(%q<redcarpet>, ["~> 2.2.2"])
27
+ s.add_development_dependency(%q<github-markup>, ["~> 0.7"])
28
+ else
29
+ s.add_dependency(%q<version>, ["~> 1.0"])
30
+ s.add_dependency(%q<rake>, ["~> 10.0"])
31
+ s.add_dependency(%q<guard-yard>, ["~> 2.0"])
32
+ s.add_dependency(%q<rb-fsevent>, ["~> 0.9"])
33
+ s.add_dependency(%q<redcarpet>, ["~> 2.2.2"])
34
+ s.add_dependency(%q<github-markup>, ["~> 0.7"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<version>, ["~> 1.0"])
38
+ s.add_dependency(%q<rake>, ["~> 10.0"])
39
+ s.add_dependency(%q<guard-yard>, ["~> 2.0"])
40
+ s.add_dependency(%q<rb-fsevent>, ["~> 0.9"])
41
+ s.add_dependency(%q<redcarpet>, ["~> 2.2.2"])
42
+ s.add_dependency(%q<github-markup>, ["~> 0.7"])
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Scott Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: version
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rb-fsevent
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.9'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ - !ruby/object:Gem::Dependency
79
+ name: redcarpet
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.2.2
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.2.2
94
+ - !ruby/object:Gem::Dependency
95
+ name: github-markup
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '0.7'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '0.7'
110
+ description: Service encapsulates an object which executes a bit of code in a loop
111
+ that can be started or stopped and query whether it is running or not.
112
+ email: ryan@rynet.us
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - Gemfile
118
+ - Gemfile.lock
119
+ - LICENSE
120
+ - README.md
121
+ - Rakefile
122
+ - VERSION
123
+ - lib/service.rb
124
+ - service.gemspec
125
+ homepage: http://github.com/RyanScottLewis/service
126
+ licenses: []
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ segments:
138
+ - 0
139
+ hash: 619595835784170795
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.24
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: A basic implementation of a Service, which has a run loop can be start and
152
+ stopped or run an a new Thread.
153
+ test_files: []