Class: FakeGoUp::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_go_up/task.rb

Constant Summary collapse

MAX_INTERVAL =
24 * 60 * 60
@@subclass =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTask

Returns a new instance of Task.



11
12
13
# File 'lib/fake_go_up/task.rb', line 11

def initialize
  @cur_step = 0
end

Instance Attribute Details

#cur_stepObject (readonly)

Returns the value of attribute cur_step.



4
5
6
# File 'lib/fake_go_up/task.rb', line 4

def cur_step
  @cur_step
end

Class Method Details

.field_to_item(field) ⇒ Object



92
93
94
95
# File 'lib/fake_go_up/task.rb', line 92

def field_to_item(field)
  item_class_name, item_id = field.split("#")
  eval(item_class_name).find_by_id(item_id)
end

.hash_keyObject



97
98
99
# File 'lib/fake_go_up/task.rb', line 97

def hash_key
  "fake_go_up:#{self.name}"
end

.inherited(subclass) ⇒ Object



83
84
85
86
# File 'lib/fake_go_up/task.rb', line 83

def inherited(subclass)
  @@subclass << subclass
  super
end

.intervalObject



66
67
68
# File 'lib/fake_go_up/task.rb', line 66

def interval
  @interval
end

.item_to_field(item) ⇒ Object



88
89
90
# File 'lib/fake_go_up/task.rb', line 88

def item_to_field(item)
  "#{item.class}##{item.id}"
end

.max_fake_countObject



70
71
72
# File 'lib/fake_go_up/task.rb', line 70

def max_fake_count
  @max_fake_count
end

.queue_up(item, count) ⇒ Object



56
57
58
59
# File 'lib/fake_go_up/task.rb', line 56

def queue_up(item, count)
  field = item_to_field(item)
  FakeGoUp.redis.hset(self.hash_key, field, count)
end

.remain(item) ⇒ Object



61
62
63
64
# File 'lib/fake_go_up/task.rb', line 61

def remain(item)
  field = item_to_field(item)
  FakeGoUp.redis.hget(self.hash_key, field).to_i
end

.running?(item) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/fake_go_up/task.rb', line 74

def running?(item)
  field = item_to_field(item)
  FakeGoUp.redis.hkeys(self.hash_key).include?(field)
end

.subclassesObject



79
80
81
# File 'lib/fake_go_up/task.rb', line 79

def subclasses
  @@subclass
end

Instance Method Details

#all_finish?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fake_go_up/task.rb', line 19

def all_finish?
  FakeGoUp.redis.hkeys(self.class.hash_key).length == 0
end

#go_upObject



23
24
25
26
27
28
29
# File 'lib/fake_go_up/task.rb', line 23

def go_up
  return unless self.cur_step % self.class.interval == 0
  FakeGoUp.redis.hkeys(self.class.hash_key).each do |field|
    go_up_one_field(field)
  end
  self.next_step
end

#go_up_one_field(field) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fake_go_up/task.rb', line 31

def go_up_one_field(field)
  remain_count = FakeGoUp.redis.hget(self.class.hash_key, field).to_i
  fake_count = rand(self.class.max_fake_count)
  if remain_count < fake_count
    fake_count = remain_count
    FakeGoUp.redis.hdel(self.class.hash_key, field)
  else
    remain_count = remain_count - fake_count
    FakeGoUp.redis.hset(self.class.hash_key, field, remain_count)
  end

  return unless fake_count > 0
  item = self.class.field_to_item(field)
  if item.nil?
    FakeGoUp.redis.hdel(self.class.hash_key, field)
  else
    process(item, fake_count)
  end
end

#next_stepObject



51
52
53
# File 'lib/fake_go_up/task.rb', line 51

def next_step
  @cur_step = (@cur_step + 1) % MAX_INTERVAL
end

#process(item, fake_count) ⇒ Object



15
16
17
# File 'lib/fake_go_up/task.rb', line 15

def process(item, fake_count)
  raise "you need overwrite this func"
end