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.



582
583
584
# File 'lib/rrrspec/redis_models.rb', line 582

def initialize(task_key)
  @key = task_key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



580
581
582
# File 'lib/rrrspec/redis_models.rb', line 580

def key
  @key
end

Class Method Details

.create(taskset, estimate_sec, spec_file) ⇒ Object



586
587
588
589
590
591
592
593
594
595
# File 'lib/rrrspec/redis_models.rb', line 586

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



597
598
599
# File 'lib/rrrspec/redis_models.rb', line 597

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

#add_trial(trial) ⇒ Object

Public: Add a trial of the task.



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

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



607
608
609
610
# File 'lib/rrrspec/redis_models.rb', line 607

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

#expire(sec) ⇒ Object

Persistence



684
685
686
687
688
# File 'lib/rrrspec/redis_models.rb', line 684

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



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

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

#statusObject

Public: Current status

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



651
652
653
# File 'lib/rrrspec/redis_models.rb', line 651

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

#tasksetObject

Public: Included taskset

Returns a Taskset



622
623
624
# File 'lib/rrrspec/redis_models.rb', line 622

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

#to_hObject

Serialize



668
669
670
671
672
673
674
675
# File 'lib/rrrspec/redis_models.rb', line 668

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



677
678
679
# File 'lib/rrrspec/redis_models.rb', line 677

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



633
634
635
636
637
# File 'lib/rrrspec/redis_models.rb', line 633

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”


657
658
659
660
661
662
663
# File 'lib/rrrspec/redis_models.rb', line 657

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