Class: FireAndForget::Command::Fire
- Inherits:
-
CommandBase
- Object
- CommandBase
- FireAndForget::Command::Fire
- Defined in:
- lib/fire_and_forget/command/fire.rb
Instance Attribute Summary collapse
-
#niceness ⇒ Object
readonly
Returns the value of attribute niceness.
-
#task_gid ⇒ Object
readonly
Returns the value of attribute task_gid.
-
#task_uid ⇒ Object
readonly
Returns the value of attribute task_uid.
Attributes inherited from CommandBase
Instance Method Summary collapse
- #binary ⇒ Object
- #binary_file ⇒ Object
- #cmd ⇒ Object
- #debug ⇒ Object
- #env ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(task, params = {}) ⇒ Fire
constructor
A new instance of Fire.
- #permitted? ⇒ Boolean
- #run ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from CommandBase
Constructor Details
#initialize(task, params = {}) ⇒ Fire
Returns a new instance of Fire.
8 9 10 11 12 |
# File 'lib/fire_and_forget/command/fire.rb', line 8 def initialize(task, params={}) super @task_uid = Process.euid @task_gid = Process.egid end |
Instance Attribute Details
#niceness ⇒ Object (readonly)
Returns the value of attribute niceness.
6 7 8 |
# File 'lib/fire_and_forget/command/fire.rb', line 6 def niceness @niceness end |
#task_gid ⇒ Object (readonly)
Returns the value of attribute task_gid.
6 7 8 |
# File 'lib/fire_and_forget/command/fire.rb', line 6 def task_gid @task_gid end |
#task_uid ⇒ Object (readonly)
Returns the value of attribute task_uid.
6 7 8 |
# File 'lib/fire_and_forget/command/fire.rb', line 6 def task_uid @task_uid end |
Instance Method Details
#binary ⇒ Object
18 19 20 |
# File 'lib/fire_and_forget/command/fire.rb', line 18 def binary @task.binary end |
#binary_file ⇒ Object
22 23 24 |
# File 'lib/fire_and_forget/command/fire.rb', line 22 def binary_file @task.binary.split(" ").first end |
#cmd ⇒ Object
26 27 28 |
# File 'lib/fire_and_forget/command/fire.rb', line 26 def cmd %(#{binary} #{FireAndForget.to_arguments(@params)}) end |
#debug ⇒ Object
71 72 73 |
# File 'lib/fire_and_forget/command/fire.rb', line 71 def debug "Fire :#{@task.name}: #{cmd}\n" end |
#env ⇒ Object
44 45 46 47 48 49 |
# File 'lib/fire_and_forget/command/fire.rb', line 44 def env @task.env.merge({ FireAndForget::ENV_SOCKET => FireAndForget.socket, FireAndForget::ENV_TASK_NAME => @task.name.to_s }) end |
#exists? ⇒ Boolean
39 40 41 42 |
# File 'lib/fire_and_forget/command/fire.rb', line 39 def exists? raise FileNotFoundError, "'#{binary_file}'" unless File.exists?(binary_file) true end |
#permitted? ⇒ Boolean
34 35 36 37 |
# File 'lib/fire_and_forget/command/fire.rb', line 34 def permitted? raise PermissionsError, "'#{binary_file}' does not belong to user '#{ENV["USER"]}'" unless File.stat(binary_file).uid == task_uid true end |
#run ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fire_and_forget/command/fire.rb', line 51 def run if valid? pid = fork do # set up the environment so that the task can access the F&F server env.each { | k, v | ENV[k] = v } # TODO: figure out how to pass a logfile path to this daemonize Process.setpriority(Process::PRIO_PROCESS, 0, niceness) if niceness > 0 # change to the UID of the originating thread if necessary Process::UID.change_privilege(task_uid) unless Process.euid == task_uid File.umask(0022) exec(cmd) end Process.detach(pid) if pid # don't return the PID as it's actually wrong (the daemonize call forks again so our original # PID is at least 1 out) "OK" end end |
#valid? ⇒ Boolean
30 31 32 |
# File 'lib/fire_and_forget/command/fire.rb', line 30 def valid? exists? && permitted? end |