Class: BeanCounter::Strategy::GemeraldBeanstalkStrategy::Job

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb

Constant Summary collapse

STATS_METHODS =

Attributes that should be retrieved via the gemerald job's stats. This list is used to dynamically create the appropriate accessor methods

%w[
  age buries delay id kicks pri releases reserves
  state time-left timeouts ttr tube
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gemerald_job, connection) ⇒ Job

Initialize a new GemeraldBeanstalkStrategy::Job wrapping the provided gemerald_job in the context of the provided connection.



54
55
56
57
# File 'lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb', line 54

def initialize(gemerald_job, connection)
  @gemerald_job = gemerald_job
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb', line 9

def connection
  @connection
end

Instance Method Details

#deleteBoolean

Attempts to delete the job. Returns true if deletion succeeds or if job does not exist. Returns false if job could not be deleted (typically due to it being reserved by another connection).

Returns:

  • (Boolean)

    If the given job was successfully deleted or does not exist, returns true. Otherwise returns false.



32
33
34
35
36
37
38
39
# File 'lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb', line 32

def delete
  response = connection.transmit("delete #{id}")
  if response == "DELETED\r\n" || !exists?
    return true
  else
    return false
  end
end

#exists?Boolean

Returns a Boolean indicating whether or not the job still exists.

Returns:

  • (Boolean)

    If job state is deleted or the job does not exist on the beanstalk server, returns false. If job state is not deleted, returns true if the job exists on the beanstalk server.



46
47
48
49
# File 'lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb', line 46

def exists?
  return false if state == 'deleted'
  return connection.transmit("stats-job #{id}") != "NOT_FOUND\r\n"
end

#to_hashHash

Augment job stats to provide a Hash representation of the job.

Returns:

  • (Hash)

    Hash representation of the job



62
63
64
65
66
67
68
69
70
71
# File 'lib/bean_counter/strategies/gemerald_beanstalk_strategy/job.rb', line 62

def to_hash
  stats_pairs = stats.to_a
  stats_pairs << ['body', body]
  stats_pairs << ['connection', connection]

  hash = Hash[stats_pairs.sort_by!(&:first)]
  hash.delete('file')

  return hash
end