Class: RRRSpec::Slave

Inherits:
Object
  • Object
show all
Defined in:
lib/rrrspec/redis_models.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slave_key) ⇒ Slave

Returns a new instance of Slave.



936
937
938
# File 'lib/rrrspec/redis_models.rb', line 936

def initialize(slave_key)
  @key = slave_key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



934
935
936
# File 'lib/rrrspec/redis_models.rb', line 934

def key
  @key
end

Class Method Details

.build_from_pid(pid) ⇒ Object



946
947
948
949
# File 'lib/rrrspec/redis_models.rb', line 946

def self.build_from_pid(pid)
  slave_key = RRRSpec.make_key('rrrspec', 'worker', RRRSpec.hostname, 'slave', pid)
  return new(slave_key)
end

.createObject



940
941
942
943
944
# File 'lib/rrrspec/redis_models.rb', line 940

def self.create
  slave_key = RRRSpec.make_key('rrrspec', 'worker', RRRSpec.hostname, 'slave', Process.getpgrp)
  slave = new(slave_key)
  return slave
end

Instance Method Details

#add_trial(trial) ⇒ Object

Public: Add trial to the list of the trials that the slave worked for.



965
966
967
968
# File 'lib/rrrspec/redis_models.rb', line 965

def add_trial(trial)
  RRRSpec.redis.rpush(RRRSpec.make_key(key, 'trial'),
                      trial.key)
end

#append_log(string) ⇒ Object

Public: Append a line to the worker_log



992
993
994
# File 'lib/rrrspec/redis_models.rb', line 992

def append_log(string)
  RRRSpec.redis.append(RRRSpec.make_key(key, 'log'), string)
end

#exist?Boolean

Public: Check its existence with heartbeat key.

Returns bool

Returns:

  • (Boolean)


1002
1003
1004
# File 'lib/rrrspec/redis_models.rb', line 1002

def exist?
  RRRSpec.redis.exists(RRRSpec.make_key(key, 'heartbeat'))
end

#expire(sec) ⇒ Object

Persistence



1029
1030
1031
1032
1033
1034
# File 'lib/rrrspec/redis_models.rb', line 1029

def expire(sec)
  RRRSpec.redis.expire(key, sec)
  RRRSpec.redis.expire(RRRSpec.make_key(key, 'trial'), sec)
  RRRSpec.redis.expire(RRRSpec.make_key(key, 'log'), sec)
  RRRSpec.redis.expire(RRRSpec.make_key(key, 'heartbeat'), sec)
end

#heartbeat(time) ⇒ Object

Public: Maintain heartbeat



1007
1008
1009
# File 'lib/rrrspec/redis_models.rb', line 1007

def heartbeat(time)
  RRRSpec.redis.setex(RRRSpec.make_key(key, 'heartbeat'), time, "alive")
end

#logObject

Public: Execution log of the slave



987
988
989
# File 'lib/rrrspec/redis_models.rb', line 987

def log
  RRRSpec.redis.get(RRRSpec.make_key(key, 'log')) || ""
end

#statusObject

Public: Current status

Returns either nil, “normal_exit”, “timeout_exit” or “failure_exit”



976
977
978
# File 'lib/rrrspec/redis_models.rb', line 976

def status
  RRRSpec.redis.hget(key, 'status')
end

#to_hObject

Serialize



1014
1015
1016
1017
1018
1019
1020
# File 'lib/rrrspec/redis_models.rb', line 1014

def to_h
  h = RRRSpec.redis.hgetall(key)
  h['trials'] = trials.map { |trial| { 'key' => trial.key } }
  h['key'] = key
  h['log'] = log
  h
end

#to_json(options = nil) ⇒ Object



1022
1023
1024
# File 'lib/rrrspec/redis_models.rb', line 1022

def to_json(options=nil)
  to_h.to_json(options)
end

#trialsObject

Public: Returns the trials of the slave. The return value should be sorted in the order added.

Returns an array of the Trials



958
959
960
961
962
# File 'lib/rrrspec/redis_models.rb', line 958

def trials
  RRRSpec.redis.lrange(RRRSpec.make_key(key, 'trial'), 0, -1).map do |key|
    Trial.new(key)
  end
end

#update_status(status) ⇒ Object

Public: Update the status. It should be one of:

“normal_exit”, “timeout_exit”, “failure_exit”


982
983
984
# File 'lib/rrrspec/redis_models.rb', line 982

def update_status(status)
  RRRSpec.redis.hset(key, 'status', status)
end