Class: EasyType::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_type/daemon.rb

Overview

The EasyType:Daemon class, allows you to easy write a daemon for your application utility. To get it working, subclass from

rubocop:disable ClassVars

Constant Summary collapse

@@daemons =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, command, user, filters = [], errors = []) ⇒ Daemon

Initialize a command daemon. In the command daemon, the specified command is run in a daemon process. The specified command must read its commands from stdin and output any results from stdout. A daemon proces must be identified by an identifier string. If you want to run multiple daemon processes, say for connecting to an other, you can use a different name.

If you want to run the daemon as an other user, you can specify a user name, the process will run under. This must be an existing user.

Checkout sync on how to sync the output. You can specify a timeout value to have the daemon read’s timed out if it dosen’t get an expected answer within that time.

rubocop: disable Lint/ReturnInVoidContext



42
43
44
45
46
47
48
# File 'lib/easy_type/daemon.rb', line 42

def initialize(identifier, command, user, filters = [], errors = [])
  return @@daemons[identifier] if @@daemons[identifier]
  initialize_daemon(user, command, filters)
  @identifier = identifier
  @@daemons[identifier] = self
  @errors = Regexp.union(errors << FAILED_SYNC_STRING)
end

Class Method Details

.run(identity) ⇒ Object

Check if a daemon for this identity is running. Use this to determin if you need to start the daemon



25
26
27
# File 'lib/easy_type/daemon.rb', line 25

def self.run(identity)
  daemon_for(identity) if daemonized?(identity)
end

Instance Method Details

#execute_command(command) ⇒ Object

Pass a command to the daemon to execute



65
66
67
# File 'lib/easy_type/daemon.rb', line 65

def execute_command(command)
  @stdin.puts command
end

#killObject

Kill the daemon and reset the entry



54
55
56
57
58
59
60
61
# File 'lib/easy_type/daemon.rb', line 54

def kill
  Thread.kill(@error_reader)
  Puppet.debug "Quiting daemon #{@identifier}..."
  @stdin.close
  @stdout.close
  @stderr.close
  @@daemons[@identifier] = nil
end

#sync(timeout = TIMEOUT, &proc) ⇒ Object

Wait for the daemon process to return a valid sync string. YIf your command passed ,return the string ‘~~~~COMMAND SUCCESFULL~~~~’. If it failed, return the string ‘~~~~COMMAND FAILED~~~~’



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/easy_type/daemon.rb', line 74

def sync(timeout = TIMEOUT, &proc)
  Puppet.debug "Daemon syncing with timeout of #{timeout} seconds..."
  @output = ''
  loop do
    line = timed_readline(timeout)
    @output += line.gsub(@filter, '*** Filtered ***')
    break if line =~ SUCCESS_SYNC_STRING
    fail "command in deamon failed.\n #{@output}" if line =~ @errors
    yield(line) if proc
  end
  Puppet.debug @output.to_s
  @output
end