Class: Minicron::Transport::FayeJobHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/transport/faye/extensions/job_handler.rb

Overview

An extension to the Faye server to store some of the data it receives

TODO: A lot of this need more validation checks and error handling

currently it's just assumed the correct data is passed and the server
can crash if it isn't

Instance Method Summary collapse

Instance Method Details

#handle_exit(request, message, segments) ⇒ Object

Handle EXIT messages

Parameters:

  • request

    the rack request object

  • message (Hash)

    the decoded message sent with the request

  • segments (Hash)

    the message channel split by /


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 166

def handle_exit(request, message, segments)
  data = message['data']['message']

  # Update the execution and add the exit status
  Minicron::Hub::Execution.where(:id => segments[3]).update_all(
    :exit_status => data[5..-1]
  )

  # If the exit status was above 0 we need to trigger a failure alert
  if data[5..-1].to_i > 0
    alert = Minicron::Alert.new
    alert.send_all(
      :kind => 'fail',
      :execution_id => segments[3],
      :job_id => segments[2]
    )
  end
end

#handle_finish(request, message, segments) ⇒ Object

Handle FINISH messages

Parameters:

  • request

    the rack request object

  • message (Hash)

    the decoded message sent with the request

  • segments (Hash)

    the message channel split by /


152
153
154
155
156
157
158
159
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 152

def handle_finish(request, message, segments)
  data = message['data']['message']

  # Update the execution and add the finish time
  Minicron::Hub::Execution.where(:id => segments[3]).update_all(
    :finished_at => data[7..-1]
  )
end

#handle_output(request, message, segments) ⇒ Object

Handle job output

Parameters:

  • request

    the rack request object

  • message (Hash)

    the decoded message sent with the request

  • segments (Hash)

    the message channel split by /


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 127

def handle_output(request, message, segments)
  data = message['data']['message']
  ts = message['data']['ts']
  seq = message['data']['seq']

  # Store the job execution output
  output = Minicron::Hub::JobExecutionOutput.create(
    :execution_id => segments[3],
    :output => data,
    :timestamp => ts,
    :seq => seq
  )

  # Append the id to the message so we can use it on the frontend
  message['data']['job_execution_output_id'] = output.id

  # And finally return the message
  message
end

#handle_setup(request, message, segments) ⇒ Object

Handle SETUP messages

Parameters:

  • request

    the rack request object

  • message (Hash)

    the decoded message sent with the request

  • segments (Hash)

    the message channel split by /


62
63
64
65
66
67
68
69
70
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
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 62

def handle_setup(request, message, segments)
  data = message['data']['message']

  # Try and find the host
  host = Minicron::Hub::Host.where(:fqdn => data['fqdn']).first

  # Create it if it didn't exist!
  if !host
    host = Minicron::Hub::Host.create(
      :name => data['hostname'],
      :fqdn => data['fqdn'],
      :host => request.ip,
      :port => 22
    )

    # Generate a new SSH key - TODO: add passphrase
    key = Minicron.generate_ssh_key('host', host.id, host.fqdn)

    # And finally we store the public key in te db with the host for convenience
    Minicron::Hub::Host.where(:id => host.id).update_all(
      :public_key => key.ssh_public_key
    )
  end

  # Validate or create the job
  job = Minicron::Hub::Job.where(:job_hash => segments[2]).first_or_create do |j|
    j.job_hash = segments[2]
    j.user = data['user']
    j.command = data['command']
    j.host_id = host.id
  end

  # Create an execution for this job
  execution = Minicron::Hub::Execution.create(
    :job_id => job.id
  )

  # Alter the response channel to include the execution id for the
  # client to use in later requests
  segments[3] = "#{job.id}-#{execution.id}/status"
  message['channel'] = segments.join('/')

  # And finally return the message
  message
end

#handle_start(request, message, segments) ⇒ Object

Handle START messages

Parameters:

  • request

    the rack request object

  • message (Hash)

    the decoded message sent with the request

  • segments (Hash)

    the message channel split by /


113
114
115
116
117
118
119
120
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 113

def handle_start(request, message, segments)
  data = message['data']['message']

  # Update the execution and add the start time
  Minicron::Hub::Execution.where(:id => segments[3]).update_all(
    :started_at => data[6..-1]
  )
end

#incoming(message, request, callback) ⇒ Object

Called by Faye when a message is recieved

Parameters:

  • message (Hash)

    The message data

  • request

    the rack request object

  • callback

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/minicron/transport/faye/extensions/job_handler.rb', line 20

def incoming(message, request, callback)
  segments = message['channel'].split('/')

  # Is it a job messages
  if segments[1] == 'job'
    data = message['data']['message']

    # Is it a setup message?
    if segments[3] == 'status' && data['action'] == 'SETUP'
      message = handle_setup(request, message, segments)
    end

    # Is it a start message?
    if segments[4] == 'status' && data[0..4] == 'START'
      handle_start(request, message, segments)
    end

    # Is it job output?
    if segments[4] == 'output'
      message = handle_output(request, message, segments)
    end

    # Is it a finish message?
    if segments[4] == 'status' && data[0..5] == 'FINISH'
      handle_finish(request, message, segments)
    end

    # Is it an exit message?
    if segments[4] == 'status' && data[0..3] == 'EXIT'
      handle_exit(request, message, segments)
    end
  end

  # Return the message back to faye
  callback.call(message)
end