Class: Jober::AbstractTask

Inherits:
Object
  • Object
show all
Includes:
Exception, Logger
Defined in:
lib/jober/abstract_task.rb

Direct Known Subclasses

Task

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Exception

#catch, #exception

Methods included from Logger

#logger, #logger=, #logger_tag

Constructor Details

#initialize(opts = {}) ⇒ AbstractTask

opts:

:worker_id
:workers_count
:skip_delay


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jober/abstract_task.rb', line 46

def initialize(opts = {})
  @opts = opts
  @stopped = false
  trap("QUIT") { @stopped = true }
  trap("INT")  { @stopped = true }
  @worker_id = (opts[:worker_id] || 0).to_i
  @workers_count = (opts[:workers_count] || 1).to_i
  @skip_delay = opts[:skip_delay]
  @unique_id = opts[:unique_id]
  after_initialize
end

Class Attribute Details

.short_nameObject

Returns the value of attribute short_name.



32
33
34
# File 'lib/jober/abstract_task.rb', line 32

def short_name
  @short_name
end

Instance Attribute Details

#finishedObject (readonly)

Returns the value of attribute finished.



35
36
37
# File 'lib/jober/abstract_task.rb', line 35

def finished
  @finished
end

#stoppedObject (readonly)

Returns the value of attribute stopped.



35
36
37
# File 'lib/jober/abstract_task.rb', line 35

def stopped
  @stopped
end

#unique_idObject (readonly)

Returns the value of attribute unique_id.



35
36
37
# File 'lib/jober/abstract_task.rb', line 35

def unique_id
  @unique_id
end

#worker_idObject (readonly)

Returns the value of attribute worker_id.



35
36
37
# File 'lib/jober/abstract_task.rb', line 35

def worker_id
  @worker_id
end

#workers_countObject (readonly)

Returns the value of attribute workers_count.



35
36
37
# File 'lib/jober/abstract_task.rb', line 35

def workers_count
  @workers_count
end

Class Method Details

.get_intervalObject



12
13
14
# File 'lib/jober/abstract_task.rb', line 12

def get_interval
  @interval || Jober.default_interval
end

.get_workersObject



20
21
22
# File 'lib/jober/abstract_task.rb', line 20

def get_workers
  @workers || 1
end

.inherited(base) ⇒ Object



37
38
39
40
# File 'lib/jober/abstract_task.rb', line 37

def self.inherited(base)
  Jober.add_class(base)
  base.interval(self.get_interval)
end

.interval(interval) ⇒ Object



8
9
10
# File 'lib/jober/abstract_task.rb', line 8

def interval(interval)
  @interval = interval
end

.manual!Object



24
25
26
# File 'lib/jober/abstract_task.rb', line 24

def manual!
  @manual = true
end

.manual?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/jober/abstract_task.rb', line 28

def manual?
  @manual
end

.workers(n) ⇒ Object



16
17
18
# File 'lib/jober/abstract_task.rb', line 16

def workers(n)
  @workers = n
end

Instance Method Details

#after_executeObject



89
90
91
# File 'lib/jober/abstract_task.rb', line 89

def after_execute
  nil
end

#after_initializeObject



58
59
# File 'lib/jober/abstract_task.rb', line 58

def after_initialize
end

#before_executeObject



61
62
63
# File 'lib/jober/abstract_task.rb', line 61

def before_execute
  nil
end

#executeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jober/abstract_task.rb', line 65

def execute
  info "=> start"
  before_execute
  @start_at = Time.now
  @finished = false
  self.class.write_timestamp(:started)
  run
  self.class.del_timestamp(:crashed)
  if @stopped
    self.class.write_timestamp(:stopped)
  else
    self.class.write_timestamp(:finished)
    self.class.del_timestamp(:stopped)
  end
  info "<= end (in #{Time.now - @start_at})"
  @finished = true
  after_execute
  self
rescue Object
  self.class.write_timestamp(:crashed)
  on_crashed
  raise
end

#on_crashedObject



93
94
95
# File 'lib/jober/abstract_task.rb', line 93

def on_crashed
  nil
end

#run_loopObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jober/abstract_task.rb', line 97

def run_loop
  info { "running loop" }

  # wait until interval + last end
  if self.class.get_workers <= 1 &&
      (finished = self.class.read_timestamp(:finished)) &&
      (Time.now - finished < self.class.get_interval) &&
      !self.class.pop_skip_delay_flag! &&
      !@skip_delay &&
      !self.class.read_timestamp(:stopped)

    sleeping(self.class.get_interval - (Time.now - finished))
  end

  # main loop
  loop do
    break if stopped
    execute
    break if stopped
    sleeping
    break if stopped
  end

  info { "quit loop" }
end

#sleeping(int = self.class.get_interval) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/jober/abstract_task.rb', line 123

def sleeping(int = self.class.get_interval)
  info { "sleeping for %.1fm ..." % [int / 60.0] }
  Timeout.timeout(int.to_f) do
    loop do
      sleep 0.3
      return if stopped
    end
  end
rescue Timeout::Error
end

#stop!Object



134
135
136
# File 'lib/jober/abstract_task.rb', line 134

def stop!
  @stopped = true
end