Class: PushyClient::JobRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/pushy_client/job_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ JobRunner

Returns a new instance of JobRunner.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pushy_client/job_runner.rb', line 29

def initialize(client)
  @client = client
  @on_job_state_change = []

  set_job_state(:idle)
  @pid = nil
  @process_thread = nil

  # Keep job state and process state in sync
  @state_lock = Mutex.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



41
42
43
# File 'lib/pushy_client/job_runner.rb', line 41

def client
  @client
end

#commandObject (readonly)

Returns the value of attribute command.



44
45
46
# File 'lib/pushy_client/job_runner.rb', line 44

def command
  @command
end

#job_idObject (readonly)

Returns the value of attribute job_id.



43
44
45
# File 'lib/pushy_client/job_runner.rb', line 43

def job_id
  @job_id
end

#lockfileObject (readonly)

Returns the value of attribute lockfile.



45
46
47
# File 'lib/pushy_client/job_runner.rb', line 45

def lockfile
  @lockfile
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#abortObject



142
143
144
145
146
147
148
149
# File 'lib/pushy_client/job_runner.rb', line 142

def abort
  Chef::Log.info("[#{node_name}] Received abort")
  @state_lock.synchronize do
    _job_id = job_id
    stop
    client.send_command(:aborted, _job_id)
  end
end

#commit(job_id, command, opts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pushy_client/job_runner.rb', line 71

def commit(job_id, command, opts)
  @opts = opts
  @state_lock.synchronize do
    if @state == :idle
      # If we're being asked to lock
      if client.whitelist[command] &&
         client.whitelist[command].is_a?(Hash) &&
         client.whitelist[command][:lock]
        # If the command is chef-client
        # We don't want to run if there is already another instance of chef-client going,
        # so we check to see if there is a runlock on chef-client before committing. This
        # currently only works in versions of chef where runlock has been implemented.

        # The location of our lockfile
        if client.whitelist[command][:lock] == true
          lockfile_location = Chef::Config[:lockfile] || "#{Chef::Config[:file_cache_path]}/chef-client-running.pid"
        else
          lockfile_location = client.whitelist[command][:lock]
        end
        # Open the Lockfile
        begin
          @lockfile = File.open(lockfile_location, 'w')
          locked = lockfile.flock(File::LOCK_EX|File::LOCK_NB)
          unless locked
            Chef::Log.info("[#{node_name}] Received commit #{job_id} but is already running '#{command}'")
            client.send_command(:nack_commit, job_id)
            return false
          end
        rescue Errno::ENOENT
        end
      elsif client.whitelist[command]
        user_ok = check_user(job_id)
        dir_ok = check_dir(job_id)
        file_ok = check_file(job_id)
        if user_ok && dir_ok && file_ok
          Chef::Log.info("[#{node_name}] Received commit #{job_id}")
          set_job_state(:committed, job_id, command)
          client.send_command(:ack_commit, job_id)
          true
        else
          client.send_command(:nack_commit, job_id)
        end
      else
        Chef::Log.error("[#{node_name}] Received commit #{job_id}, but command '#{command}' is not in the whitelist!")
        client.send_command(:nack_commit, job_id)
        false
      end
    else
      Chef::Log.warn("[#{node_name}] Received commit #{job_id} but current state is #{@state} #{@job_id}")
      client.send_command(:nack_commit, job_id)
      false
    end
  end
end

#job_stateObject



151
152
153
154
155
# File 'lib/pushy_client/job_runner.rb', line 151

def job_state
  @state_lock.synchronize do
    get_job_state
  end
end

#node_nameObject



53
54
55
# File 'lib/pushy_client/job_runner.rb', line 53

def node_name
  client.node_name
end

#on_job_state_change(&block) ⇒ Object



157
158
159
# File 'lib/pushy_client/job_runner.rb', line 157

def on_job_state_change(&block)
  @on_job_state_change << block
end

#reconfigureObject



67
68
69
# File 'lib/pushy_client/job_runner.rb', line 67

def reconfigure
  # We have no configuration, and keep state between reconfigures
end

#run(job_id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pushy_client/job_runner.rb', line 126

def run(job_id)
  @state_lock.synchronize do
    if @state == :committed && @job_id == job_id
      Chef::Log.info("[#{node_name}] Received run #{job_id}")
      pid, process_thread = start_process
      set_job_state(:running, job_id, @command, pid, process_thread)
      client.send_command(:ack_run, job_id)
      true
    else
      Chef::Log.warn("[#{node_name}] Received run #{job_id} but current state is #{@state} #{@job_id}")
      client.send_command(:nack_run, job_id)
      false
    end
  end
end

#safe_to_reconfigure?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/pushy_client/job_runner.rb', line 47

def safe_to_reconfigure?
  @state_lock.synchronize do
    @state == :idle
  end
end

#startObject



57
58
# File 'lib/pushy_client/job_runner.rb', line 57

def start
end

#stopObject



60
61
62
63
64
65
# File 'lib/pushy_client/job_runner.rb', line 60

def stop
  if @state == :running
    kill_process
  end
  set_job_state(:idle)
end