Class: AllQ::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/allq/actions/base.rb

Overview

Base class for handling allq actions

Direct Known Subclasses

AddServer, Bury, Clear, Delete, Done, Drain, Get, Kick, ParentJob, Peek, Put, Release, Stats, Throttle, Touch

Constant Summary collapse

ALLQ_DEBUG_DATA =
ENV["ALLQ_DEBUG_DATA"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, client) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
# File 'lib/allq/actions/base.rb', line 10

def initialize(connection, client)
  @connection = connection
  @client = client
  setup
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/allq/actions/base.rb', line 6

def client
  @client
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/allq/actions/base.rb', line 6

def connection
  @connection
end

Instance Method Details

#base_send(_data) ⇒ Object

– Abstract

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/allq/actions/base.rb', line 62

def base_send(_data)
  raise NotImplementedError
end

#build_job(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/allq/actions/base.rb', line 45

def build_job(result)
  result_hash = JSON.parse(result)
  job_info = result_hash["job"]
  job_id = job_info["job_id"]

  job = Job.new(job_id, @client)
  # -- Optional fields
  job.body = job_info["body"] if job_info["body"]
  job.expireds = job_info["expireds"] if job_info["expireds"]
  job.releases = job_info["releases"] if job_info["releases"]
  return job
end

#rcv(data) ⇒ Object



22
23
24
25
# File 'lib/allq/actions/base.rb', line 22

def rcv(data)
  return nil if data.to_s == '' || data.to_s.strip == '{}'
  data
end

#send_hash_as_json(data_hash, ignore_failure = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/allq/actions/base.rb', line 27

def send_hash_as_json(data_hash, ignore_failure = false)
  transmit_data = data_hash.to_json
  result = nil
  puts "transmitting data: #{transmit_data}" if ALLQ_DEBUG_DATA
  if ignore_failure
    @connection.socat(transmit_data) do |response|
      result = response
    end
    results = "{}" if results.to_s == ""
  else
    @connection.transmit(transmit_data) do |response|
      result = response
    end
  end

  result
end

#setupObject



58
59
# File 'lib/allq/actions/base.rb', line 58

def setup
end

#snd(data) ⇒ Object



16
17
18
19
20
# File 'lib/allq/actions/base.rb', line 16

def snd(data)
  send_data = base_send(data)
  response = send_hash_as_json(send_data)
  rcv(response)
end