Class: RRRSpec::Task

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(task_key) ⇒ Task

Returns a new instance of Task.



614
615
616
# File 'lib/rrrspec/redis_models.rb', line 614

def initialize(task_key)
  @key = task_key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



612
613
614
# File 'lib/rrrspec/redis_models.rb', line 612

def key
  @key
end

Class Method Details

.create(taskset, estimate_sec, spec_file) ⇒ Object



618
619
620
621
622
623
624
625
626
627
# File 'lib/rrrspec/redis_models.rb', line 618

def self.create(taskset, estimate_sec, spec_file)
  task_key = RRRSpec.make_key(taskset.key, 'task', spec_file)
  RRRSpec.redis.hmset(
    task_key,
    'taskset', taskset.key,
    'estimate_sec', estimate_sec,
    'spec_file', spec_file
  )
  return new(task_key)
end

Instance Method Details

#==(other) ⇒ Object



629
630
631
# File 'lib/rrrspec/redis_models.rb', line 629

def ==(other)
  @key == other.key
end

#add_trial(trial) ⇒ Object

Public: Add a trial of the task.



672
673
674
675
# File 'lib/rrrspec/redis_models.rb', line 672

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

#estimate_secObject

Public: Estimate time to finishe the task.

Returns seconds or nil if there is no estimation



639
640
641
642
# File 'lib/rrrspec/redis_models.rb', line 639

def estimate_sec
  v = RRRSpec.redis.hget(key, 'estimate_sec')
  v.present? ? v.to_i : nil
end

#expire(sec) ⇒ Object

Persistence



716
717
718
719
720
# File 'lib/rrrspec/redis_models.rb', line 716

def expire(sec)
  trials.each { |trial| trial.expire(sec) }
  RRRSpec.redis.expire(key, sec)
  RRRSpec.redis.expire(RRRSpec.make_key(key, 'trial'), sec)
end

#spec_fileObject

Public: Spec file to run.

Returns a path to the spec



647
648
649
# File 'lib/rrrspec/redis_models.rb', line 647

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

#statusObject

Public: Current status

Returns either nil, “running”, “passed”, “pending” or “failed”



683
684
685
# File 'lib/rrrspec/redis_models.rb', line 683

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

#tasksetObject

Public: Included taskset

Returns a Taskset



654
655
656
# File 'lib/rrrspec/redis_models.rb', line 654

def taskset
  Taskset.new(RRRSpec.redis.hget(key, 'taskset'))
end

#to_hObject

Serialize



700
701
702
703
704
705
706
707
# File 'lib/rrrspec/redis_models.rb', line 700

def to_h
  h = RRRSpec.redis.hgetall(key)
  h['key'] = key
  h['trials'] = trials.map { |trial| { 'key' => trial.key } }
  h['taskset'] = { 'key' => h['taskset'] }
  RRRSpec.convert_if_present(h, 'estimate_sec') { |v| v.to_i }
  h
end

#to_json(options = nil) ⇒ Object



709
710
711
# File 'lib/rrrspec/redis_models.rb', line 709

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

#trialsObject

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

Returns an array of the Trials



665
666
667
668
669
# File 'lib/rrrspec/redis_models.rb', line 665

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:

nil, “running”, “passed”, “pending”, “failed”


689
690
691
692
693
694
695
# File 'lib/rrrspec/redis_models.rb', line 689

def update_status(status)
  if status.present?
    RRRSpec.redis.hset(key, 'status', status)
  else
    RRRSpec.redis.hdel(key, 'status')
  end
end