Module: Service::Base

Included in:
Service
Defined in:
lib/service.rb

Overview

The instance methods to be mixed into a Service.

Instance Method Summary collapse

Instance Method Details

#executeObject

This method is abstract.

Override #execute to implement a custom Service.

The code that will be executed within the run loop.

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/service.rb', line 28

def execute
  raise NotImplementedError
end

#execute!Thread

Call the #execute method within a new Thread.

Returns:

  • (Thread)


35
36
37
# File 'lib/service.rb', line 35

def execute!
  Thread.new { execute }
end

#startSymbol Also known as: run

Start the run loop.

Returns:

  • (Symbol)

    The current state (‘:started`).



49
50
51
52
53
54
55
56
57
# File 'lib/service.rb', line 49

def start
  @_service_state = :started

  loop do
    break if stopped?

    execute
  end
end

#start!Thread Also known as: run!

Start the run loop in a new Thread.

Returns:

  • (Thread)


64
65
66
# File 'lib/service.rb', line 64

def start!
  Thread.new { start }
end

#started?Boolean Also known as: running?

Query if the service is currently started.

Returns:

  • (Boolean)


20
21
22
# File 'lib/service.rb', line 20

def started?
  @_service_state == :started
end

#stopSymbol

Stop the run loop.

Returns:

  • (Symbol)

    The current state (‘:stopped`).



42
43
44
# File 'lib/service.rb', line 42

def stop
  @_service_state = :stopped
end

#stopped?Boolean

Query if the service is currently stopped.

Returns:

  • (Boolean)


13
14
15
# File 'lib/service.rb', line 13

def stopped?
  @_service_state == :stopped
end