Class: Minicron::Transport::Client

Inherits:
FayeClient show all
Defined in:
lib/minicron/transport/client.rb

Instance Attribute Summary

Attributes inherited from FayeClient

#queue, #responses, #url

Instance Method Summary collapse

Methods inherited from FayeClient

#ensure_delivery, #ensure_em_running, #request, #tidy_up

Constructor Details

#initialize(scheme, host, port, path) ⇒ Client

Instantiate a new instance of the client

Parameters:

  • host (String)

    The host to be communicated with


9
10
11
12
13
14
15
16
# File 'lib/minicron/transport/client.rb', line 9

def initialize(scheme, host, port, path)
  @scheme = scheme
  @host = host
  @path = path == '/' ? '/faye' : "#{path}/faye"
  @port = port
  @seq = 1
  super(@scheme, @host, @port, @path)
end

Instance Method Details

#publish(channel, message) ⇒ Object

Publishes a message on the given channel to the server

Parameters:

  • channel (String)
  • message (String, Hash)

68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/minicron/transport/client.rb', line 68

def publish(channel, message)
  # Set up the data to send to faye
  data = { :channel => channel, :data => {
    :ts => Time.now.utc.strftime('%Y-%m-%d %H:%M:%S'),
    :message => message,
    :seq => @seq
  } }

  # Increment the sequence id
  @seq += 1

  request(:message => data.to_json)
end

#send(options = {}) ⇒ Object

Helper that wraps the publish function making it quicker to use

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • job_id (String)
  • execution_id (Integer)
  • type (String, Symbol)

    status or output

  • (String, Hash)

59
60
61
62
# File 'lib/minicron/transport/client.rb', line 59

def send(options = {})
  # Publish the message to the correct channel
  publish("/job/#{options[:job_id]}/#{options[:execution_id]}/#{options[:type]}", options[:message])
end

#setup(options = {}) ⇒ Hash

Used to set up a job on the server

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • job_hash (String)
  • user (String)
  • command (Integer)
  • fqdn (String)
  • hostname (String)

Returns:

  • (Hash)

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
# File 'lib/minicron/transport/client.rb', line 26

def setup(options = {})
  # Send a request to set up the job
  publish("/job/#{options[:job_hash]}/status",
    :action => 'SETUP',
    :user => options[:user],
    :command => options[:command],
    :fqdn => options[:fqdn],
    :hostname => options[:hostname]
  )

  # Wait for the response..
  ensure_delivery

  # TODO: Handle errors here!
  # Get the job and execution id from the response
  ids = JSON.parse(@responses.first[:body]).first['channel'].split('/')[3]

  # Split them up
  ids = ids.split('-')

  # Return them as a hash
  {
    :job_id => ids[0],
    :execution_id => ids[1]
  }
end