RyanScottLewis/service
{ "createdAt": "2012-12-13T09:17:55Z", "defaultBranch": "master", "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.", "fullName": "RyanScottLewis/service", "homepage": null, "language": "Ruby", "name": "service", "pushedAt": "2018-01-16T15:51:22Z", "stargazersCount": 84, "topics": [ "loop", "ruby", "thread" ], "updatedAt": "2022-08-03T14:16:19Z", "url": "https://github.com/RyanScottLewis/service"}Service
Section titled “Service”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.
Install
Section titled “Install”Bundler: gem 'service'
Section titled “Bundler: gem 'service'”RubyGems: gem install service
Section titled “RubyGems: gem install service”See the
examples/directory for more usage examples.
Requiring
Section titled “Requiring”| Class/Module | File |
|---|---|
Service | service |
Service::Base | service/base |
Defining
Section titled “Defining”You can define an Object as a Service by subclassing Service or by including Service::Base:
class ServiceA < Serviceend
class ServiceB
include Service::Base
endA
Serviceobject stores it’s state in the@_service_stateinstance variable as to be as unobtrusive as possible when integrating with your custom objects.
The next thing to do is to define an #execute instance method on your object:
class MyService < Service
def execute # ... end
endRunning
Section titled “Running”Service simply allows you to run your code that is within the #execute instance method in four different ways:
- Once (
#execute) - Once, within a new Thread (
#execute!) - Looping (
#start/#run) - Looping, within a new Thread (
#start!/#run!)
Use the #start/#run instance method to call the #execute instance method in a loop.
class MyService < Service
def execute puts "Hello" end
end
MyService.new.run# => Hello# => Hello# => ...Use #start!/#run! to call the #execute instance method in a new Thread.
class MyService < Service
def execute puts "Hello" end
end
thread = MyService.new.run!thread.join# => Hello# => Hello# => ...Stopping
Section titled “Stopping”Use the #stop instance method break the run loop.
This will also kill the Thread it is running in, if running within a Thread.
class CountingService < Service
def initialize @count = 0 end
def execute puts @count sleep 1
@count += 1 end
end
service = CountingService.new
service.run! # Run the #execute method in a loop within a new Threadsleep 5service.stopQuerying State
Section titled “Querying State”Use the started?/running? or stopped? instance methods to determine the current state of
the Service instance.
class MyService < Service
def execute sleep 10 end
end
service = MyService.new
p service.running? # => false
service.run!sleep 1
p service.running? # => trueCopyright
Section titled “Copyright”Copyright © 2012 Ryan Scott Lewis ryanscottlewis@gmail.com.
The MIT License (MIT) - See LICENSE for further details.