Class: RRRSpec::Worker
- Inherits:
-
Object
- Object
- RRRSpec::Worker
- Defined in:
- lib/rrrspec/redis_models.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.create(worker_type, hostname = RRRSpec.hostname) ⇒ Object
Public: Create a new worker.
-
.list ⇒ Object
Public: A list of the workers which are possibly available.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#current_taskset ⇒ Object
Public: Current taskset.
-
#dequeue_taskset ⇒ Object
Public: Dequeue the taskset from the taskset_queue.
-
#enqueue_taskset(taskset) ⇒ Object
Public: Enqueue the taskset to the taskset_queue.
-
#evict ⇒ Object
Public: Remove myself from the worker list.
-
#exist? ⇒ Boolean
Public: Check its existence with heartbeat key.
-
#heartbeat(time) ⇒ Object
Public: Maintain heartbeat.
-
#initialize(worker_key) ⇒ Worker
constructor
A new instance of Worker.
-
#queue_empty? ⇒ Boolean
Public: Checks whether the taskset_queue is empty.
-
#update_current_taskset(taskset) ⇒ Object
Public: Update the current taskset.
-
#worker_type ⇒ Object
Public: The worker_type.
Constructor Details
#initialize(worker_key) ⇒ Worker
Returns a new instance of Worker.
868 869 870 |
# File 'lib/rrrspec/redis_models.rb', line 868 def initialize(worker_key) @key = worker_key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
866 867 868 |
# File 'lib/rrrspec/redis_models.rb', line 866 def key @key end |
Class Method Details
.create(worker_type, hostname = RRRSpec.hostname) ⇒ Object
Public: Create a new worker. The worker returned is NOT appeared in Worker.list.
874 875 876 877 878 879 880 |
# File 'lib/rrrspec/redis_models.rb', line 874 def self.create(worker_type, hostname=RRRSpec.hostname) worker_key = RRRSpec.make_key('rrrspec', 'worker', hostname) RRRSpec.redis.hset(worker_key, 'worker_type', worker_type) worker = new(worker_key) return worker end |
Instance Method Details
#==(other) ⇒ Object
896 897 898 |
# File 'lib/rrrspec/redis_models.rb', line 896 def ==(other) @key == other.key end |
#current_taskset ⇒ Object
Public: Current taskset
Returns a taskset or nil
914 915 916 917 918 919 920 921 |
# File 'lib/rrrspec/redis_models.rb', line 914 def current_taskset taskset_key = RRRSpec.redis.hget(key, 'taskset') if taskset_key.present? return Taskset.new(taskset_key) else nil end end |
#dequeue_taskset ⇒ Object
Public: Dequeue the taskset from the taskset_queue
938 939 940 941 |
# File 'lib/rrrspec/redis_models.rb', line 938 def dequeue_taskset _, taskset_key = RRRSpec.redis.blpop(RRRSpec.make_key(key, 'worker_queue'), 0) return Taskset.new(taskset_key) end |
#enqueue_taskset(taskset) ⇒ Object
Public: Enqueue the taskset to the taskset_queue
933 934 935 |
# File 'lib/rrrspec/redis_models.rb', line 933 def enqueue_taskset(taskset) RRRSpec.redis.rpush(RRRSpec.make_key(key, 'worker_queue'), taskset.key) end |
#evict ⇒ Object
Public: Remove myself from the worker list.
892 893 894 |
# File 'lib/rrrspec/redis_models.rb', line 892 def evict RRRSpec.redis.srem(RRRSpec.make_key('rrrspec', 'worker'), key) end |
#exist? ⇒ Boolean
Public: Check its existence with heartbeat key.
Returns bool
954 955 956 |
# File 'lib/rrrspec/redis_models.rb', line 954 def exist? RRRSpec.redis.exists(RRRSpec.make_key(key, 'heartbeat')) end |
#heartbeat(time) ⇒ Object
Public: Maintain heartbeat
959 960 961 962 |
# File 'lib/rrrspec/redis_models.rb', line 959 def heartbeat(time) RRRSpec.redis.setex(RRRSpec.make_key(key, 'heartbeat'), time, "alive") RRRSpec.redis.sadd(RRRSpec.make_key('rrrspec', 'worker'), key) end |
#queue_empty? ⇒ Boolean
Public: Checks whether the taskset_queue is empty.
944 945 946 |
# File 'lib/rrrspec/redis_models.rb', line 944 def queue_empty? RRRSpec.redis.llen(RRRSpec.make_key(key, 'worker_queue')) == 0 end |
#update_current_taskset(taskset) ⇒ Object
Public: Update the current taskset
924 925 926 927 928 929 930 |
# File 'lib/rrrspec/redis_models.rb', line 924 def update_current_taskset(taskset) if taskset.present? RRRSpec.redis.hset(key, 'taskset', taskset.key) else RRRSpec.redis.hset(key, 'taskset', nil) end end |