Class: Omega::Server::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/omega/server/command.rb

Overview

Omega Command, tracks and invokes commands and hooks on various conditions.

Should be subclassed to perform actual command and hook operations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Command

Omega::Server::Command initializer

Options Hash (args):

  • id (Symbol)

    id of the command

  • exec_rate (Integer)

    optional rate at which command should be executed

  • hooks (Hash<String, Array<Callable>>)

    hooks to register w/ the command


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omega/server/command.rb', line 52

def initialize(args = {})
  attr_from_args args, :id              => nil,
                       :exec_rate       => nil,
                       :added_at        => Time.now.to_s, # XXX .to_s needed so time comparisons are acurate
                       :last_ran_at     => nil,
                       :ran_first_hooks => false,
                       :hooks =>  {:first  => [proc { self.first_hook  }],
                                   :before => [proc { self.before_hook }],
                                   :after  => [proc { self.after_hook  }],
                                   :last   => [proc { self.last_hook   }]}

  @added_at    = Time.parse(@added_at)    if @added_at.is_a?(String)
  @last_ran_at = Time.parse(@last_ran_at) if @last_ran_at.is_a?(String)
  #\@id = \@id.intern if \@id.is_a?(String)
end

Instance Attribute Details

#added_atObject

Time command was added to the registry


28
29
30
# File 'lib/omega/server/command.rb', line 28

def added_at
  @added_at
end

#exec_rateObject

Optional rate at which the command should be executed


19
20
21
# File 'lib/omega/server/command.rb', line 19

def exec_rate
  @exec_rate
end

#hooksObject

Hooks to be invoked during various stages of the command


22
23
24
# File 'lib/omega/server/command.rb', line 22

def hooks
  @hooks
end

#idObject

Id of the event


16
17
18
# File 'lib/omega/server/command.rb', line 16

def id
  @id
end

#last_ran_atObject

Time of last time command was run


31
32
33
# File 'lib/omega/server/command.rb', line 31

def last_ran_at
  @last_ran_at
end

#nodeObject

Node which to use to perform additional queries


37
38
39
# File 'lib/omega/server/command.rb', line 37

def node
  @node
end

#ran_first_hooksObject

Special flag, indicates if 'first' hooks was called


25
26
27
# File 'lib/omega/server/command.rb', line 25

def ran_first_hooks
  @ran_first_hooks
end

#registryObject

Registry which command is running in


34
35
36
# File 'lib/omega/server/command.rb', line 34

def registry
  @registry
end

Class Method Details

.json_create(o) ⇒ Object

Create new command from json representation


136
137
138
139
# File 'lib/omega/server/command.rb', line 136

def self.json_create(o)
  cmd = new(o['data'])
  return cmd
end

Instance Method Details

#after_hookObject

'after' hook definition


82
83
# File 'lib/omega/server/command.rb', line 82

def after_hook
end

#before_hookObject

'before' hook definition


78
79
# File 'lib/omega/server/command.rb', line 78

def before_hook
end

#cmd_jsonObject


119
120
121
122
123
124
125
# File 'lib/omega/server/command.rb', line 119

def cmd_json
 {:id              => id,
  :exec_rate       => exec_rate,
  :ran_first_hooks => ran_first_hooks,
  :added_at        => added_at,
  :last_ran_at     => last_ran_at}
end

#first_hookObject

'first' hook definition


74
75
# File 'lib/omega/server/command.rb', line 74

def first_hook
end

#last_hookObject

'last' hook definition


86
87
# File 'lib/omega/server/command.rb', line 86

def last_hook
end

#processes?(entity) ⇒ Boolean

Return bool indicating if command processes the specified entity. By default returns false but may be overridden by specific commands in other subsystems


42
43
44
# File 'lib/omega/server/command.rb', line 42

def processes?(entity)
  false
end

#remove?Boolean

Return bool indicating if cmd should be removed


110
111
112
# File 'lib/omega/server/command.rb', line 110

def remove?
  false
end

#run!Object

Actually run command


105
106
107
# File 'lib/omega/server/command.rb', line 105

def run!
  @last_ran_at = Time.now
end

#run_hooks(hook) ⇒ Object

Run hooks of the specified type


90
91
92
93
94
95
96
# File 'lib/omega/server/command.rb', line 90

def run_hooks(hook)
  @ran_first_hooks = true if hook == :first

  @hooks[hook].each { |h|
    instance_exec &h
  } if @hooks[hook]
end

#should_run?Boolean

Return boolean indicating if command should be run


99
100
101
102
# File 'lib/omega/server/command.rb', line 99

def should_run?
  (@last_ran_at.nil? || @exec_rate.nil? ||
   ((Time.now - @last_ran_at) > 1 / @exec_rate))
end

#to_json(*a) ⇒ Object

Convert command to json representation and return it


128
129
130
131
132
133
# File 'lib/omega/server/command.rb', line 128

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => cmd_json
  }.to_json(*a)
end

#to_sObject

Convert command to human readable string and return it


115
116
117
# File 'lib/omega/server/command.rb', line 115

def to_s
  "command-#{@id}"
end

#update(cmd) ⇒ Object

TODO this needed?


69
70
71
# File 'lib/omega/server/command.rb', line 69

def update(cmd)
  update_from(cmd, :ran_first_hooks, :added_at, :last_ran_at, :exec_rate)
end