All Projects → RyanScottLewis → Service

RyanScottLewis / Service

Licence: mit
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.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Service

Superboucle
Loop application synced with jack transport
Stars: ✭ 31 (-63.95%)
Mutual labels:  loop
Webfsd
A simple HTTP server for mostly static content written in C
Stars: ✭ 50 (-41.86%)
Mutual labels:  thread
Distube
A Discord.js v12 module to simplify your music commands and play songs with audio filters on Discord without any API key. Support YouTube, SoundCloud, Bandcamp, Facebook, and 700+ more sites
Stars: ✭ 73 (-15.12%)
Mutual labels:  loop
Enkits
A permissively licensed C and C++ Task Scheduler for creating parallel programs. Requires C++11 support.
Stars: ✭ 962 (+1018.6%)
Mutual labels:  thread
Bentools Etl
PHP ETL (Extract / Transform / Load) library with SOLID principles + almost no dependency.
Stars: ✭ 45 (-47.67%)
Mutual labels:  loop
Threadly
Type-safe thread-local storage in Swift
Stars: ✭ 58 (-32.56%)
Mutual labels:  thread
Thread Loader
Runs the following loaders in a worker pool
Stars: ✭ 945 (+998.84%)
Mutual labels:  thread
Easyble
Multi-devices process Bluetooth library for Android
Stars: ✭ 81 (-5.81%)
Mutual labels:  thread
Vsalert
An drop-in replacement for UIAlertController with more power and better looks.
Stars: ✭ 48 (-44.19%)
Mutual labels:  thread
Diskqueue
A thread-safe, multi-process(ish) persistent queue library for .Net and Mono
Stars: ✭ 66 (-23.26%)
Mutual labels:  thread
Rxjava Android Samples
Learning RxJava for Android by example
Stars: ✭ 7,520 (+8644.19%)
Mutual labels:  thread
Swiftysound
SwiftySound is a simple library that lets you play sounds with a single line of code.
Stars: ✭ 995 (+1056.98%)
Mutual labels:  loop
Pelagia
Automatic parallelization (lock-free multithreading thread) tool developed by Surparallel Open Source.Pelagia is embedded key value database that implements a small, fast, high-reliability on ANSI C.
Stars: ✭ 1,132 (+1216.28%)
Mutual labels:  thread
Threadpool
a dynamic threadpool
Stars: ✭ 32 (-62.79%)
Mutual labels:  thread
Laravel 5 Messenger
A Simple Laravel 5, 6, 7 & 8 Messenger with Pusher Capabilities
Stars: ✭ 75 (-12.79%)
Mutual labels:  thread
Autocrawler
Google, Naver multiprocess image web crawler (Selenium)
Stars: ✭ 957 (+1012.79%)
Mutual labels:  thread
Mainthreadguard
💂 Tracking UIKit access on main thread
Stars: ✭ 53 (-38.37%)
Mutual labels:  thread
Infinite Uicollectionview
Make a UICollectionView infinitely scrolling by looping through content
Stars: ✭ 82 (-4.65%)
Mutual labels:  loop
Rpmalloc
Public domain cross platform lock free thread caching 16-byte aligned memory allocator implemented in C
Stars: ✭ 1,218 (+1316.28%)
Mutual labels:  thread
Oscpy
An efficient OSC implementation compatible with python2.7 and 3.5+
Stars: ✭ 65 (-24.42%)
Mutual labels:  thread

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].