Class: EasyType::Daemon
- Inherits:
-
Object
- Object
- EasyType::Daemon
- 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
- SUCCESS_SYNC_STRING =
/~~~~COMMAND SUCCESFULL~~~~/
- FAILED_SYNC_STRING =
/~~~~COMMAND FAILED~~~~/
- TIMEOUT =
wait 60 seconds as default
60
- @@daemons =
{}
Class Method Summary collapse
-
.run(identity) ⇒ Object
Check if a daemon for this identity is running.
Instance Method Summary collapse
-
#execute_command(command) ⇒ Object
Pass a command to the daemon to execute.
-
#initialize(identifier, command, user) ⇒ Daemon
constructor
Initialize a command daemon.
-
#sync(timeout = TIMEOUT, &proc) ⇒ Object
Wait for the daemon process to return a valid sync string.
Constructor Details
#initialize(identifier, command, user) ⇒ Daemon
Initialize a command daemon. In the command daemon, the specified command is run in a daemon process. The specified command must readiths commands from stdi 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.
38 39 40 41 42 43 44 45 46 |
# File 'lib/easy_type/daemon.rb', line 38 def initialize(identifier, command, user) if @@daemons[identifier] return @@daemons[identifier] else initialize_daemon(user, command) @identifier = identifier @@daemons[identifier] = self end 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
20 21 22 |
# File 'lib/easy_type/daemon.rb', line 20 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
51 52 53 |
# File 'lib/easy_type/daemon.rb', line 51 def execute_command(command) @stdin.puts command 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~~~~’
60 61 62 63 64 65 66 67 68 |
# File 'lib/easy_type/daemon.rb', line 60 def sync( timeout = TIMEOUT, &proc) while true do line = timed_readline(timeout) Puppet.debug "#{line}" break if line =~ SUCCESS_SYNC_STRING fail 'command in deamon failed.' if line =~ FAILED_SYNC_STRING proc.call(line) if proc end end |