GithubHelp home page GithubHelp logo

ryanscottlewis / service Goto Github PK

View Code? Open in Web Editor NEW
84.0 6.0 0.0 10 KB

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.

License: MIT License

Ruby 100.00%
thread ruby loop

service's Introduction

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

Bundler: gem 'service'

RubyGems: gem install service

Usage

See the examples/ directory for more usage examples.

Requiring

Class/Module File
Service service
Service::Base service/base

Defining

You can define an Object as a Service by subclassing Service or by including Service::Base:

class ServiceA < Service
end

class ServiceB

  include Service::Base

end

A Service object stores it's state in the @_service_state instance 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

end

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

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 Thread
sleep 5
service.stop

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? # => true

Copyright

Copyright © 2012 Ryan Scott Lewis [email protected].

The MIT License (MIT) - See LICENSE for further details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.