Class: WebTaskRunner

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/web_task_runner.rb,
lib/web_task_runner/task_worker.rb,
lib/web_task_runner/redis_module.rb

Defined Under Namespace

Modules: RedisModule Classes: TaskWorker

Constant Summary collapse

VERSION =
WebTaskRunnerVersion::VERSION
@@jobs =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_infoObject

Get the info of the task



178
179
180
181
182
183
184
185
# File 'lib/web_task_runner.rb', line 178

def self.current_info
  info = { state: current_state }
  info[:task_progress] = task_progress if task_progress
  info[:task_started_at] = task_started_at if task_started_at
  info[:task_finished_at] = task_finished_at if task_finished_at

  info
end

.current_stateObject

Get the current state



131
132
133
# File 'lib/web_task_runner.rb', line 131

def self.current_state
  WebTaskRunner::RedisModule.redis.get('task:state') || 'idle'
end

.current_statusObject

Get the status of the task runner



192
193
194
195
196
# File 'lib/web_task_runner.rb', line 192

def self.current_status
  info = { state: current_state }

  info
end

.job_ended(all: false) ⇒ Object

Report that a job has been done, call this in each job after the work has done



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/web_task_runner.rb', line 53

def self.job_ended(all: false)
  if all
    WebTaskRunner::RedisModule.redis.set('task:working_jobs', 0)
  else
    # decrease the working jobss count
    WebTaskRunner::RedisModule.redis.decr('task:working_jobs')
  end

  # set the state to idle if all the works has been done
  if WebTaskRunner::RedisModule.redis.get('task:working_jobs').to_i < 1
    WebTaskRunner::RedisModule.redis.set('task:state', 'idle')
    WebTaskRunner::RedisModule.redis.set('task:finished_at', Time.now)
  end
end

.jobsObject



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

def self.jobs
  @@jobs
end

.kill_taskObject

Kills the running task



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/web_task_runner.rb', line 114

def self.kill_task
  ps = Sidekiq::ProcessSet.new
  ps.each do |p|
    p.stop! if p['busy'] > 0
  end
  sleep(0.5)
  Sidekiq::Queue.new.clear
  Sidekiq::ScheduledSet.new.clear
  Sidekiq::RetrySet.new.clear
  WebTaskRunner.job_ended(all: true)
end

.start_taskObject

Starts (or kill and restart) the task



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/web_task_runner.rb', line 78

def self.start_task
  kill_task
  WebTaskRunner::RedisModule.redis.set('task:state', 'working')
  WebTaskRunner::RedisModule.redis.set('task:started_at', Time.now)

  # Set the count of jobs that should be started
  jobs_count = @@jobs.count

  # Start the worker here
  @@jobs.each(&:perform_async)

  WebTaskRunner::RedisModule.redis.set('task:task_jobs', jobs_count)
  WebTaskRunner::RedisModule.redis.set('task:working_jobs', jobs_count)

  # Reset the progress of each job
  jobs_count.times do |i|
    i -= 1
    WebTaskRunner::RedisModule.redis.set("task:job_#{i}_progress", 0)
  end
end

.start_task_if_idleObject

Starts the task if it’s not running



104
105
106
107
# File 'lib/web_task_runner.rb', line 104

def self.start_task_if_idle
  return unless current_state == 'idle'
  start_task
end

.task_finished_atObject

Get the time when the task last finished



168
169
170
171
# File 'lib/web_task_runner.rb', line 168

def self.task_finished_at
  return nil if current_state != 'idle'
  try_to_parse_date_from_redis('task:finished_at')
end

.task_progressObject

Get the task progress



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/web_task_runner.rb', line 140

def self.task_progress
  return nil if current_state == 'idle'
  task_jobs = WebTaskRunner::RedisModule.redis.get('task:task_jobs').to_i
  return nil if task_jobs < 1
  total_progress = 0.0

  task_jobs.times do |i|
    i += 1
    total_progress += WebTaskRunner::RedisModule.redis.get("task:job_#{i}_progress").to_f
  end

  total_progress / task_jobs.to_f
end

.task_started_atObject

Get the time when the task last started



159
160
161
# File 'lib/web_task_runner.rb', line 159

def self.task_started_at
  try_to_parse_date_from_redis('task:started_at')
end

Instance Method Details

#current_infoObject

:nodoc:



187
188
189
# File 'lib/web_task_runner.rb', line 187

def current_info  # :nodoc:
  WebTaskRunner.current_info
end

#current_stateObject

:nodoc:



135
136
137
# File 'lib/web_task_runner.rb', line 135

def current_state  # :nodoc:
  WebTaskRunner.current_state
end

#current_statusObject

:nodoc:



198
199
200
# File 'lib/web_task_runner.rb', line 198

def current_status  # :nodoc:
  WebTaskRunner.current_status
end

#kill_taskObject

:nodoc:



126
127
128
# File 'lib/web_task_runner.rb', line 126

def kill_task  # :nodoc:
  WebTaskRunner.kill_task
end

#start_taskObject

:nodoc:



99
100
101
# File 'lib/web_task_runner.rb', line 99

def start_task  # :nodoc:
  WebTaskRunner.start_task
end

#start_task_if_idleObject

:nodoc:



109
110
111
# File 'lib/web_task_runner.rb', line 109

def start_task_if_idle  # :nodoc:
  WebTaskRunner.start_task_if_idle
end

#task_finished_atObject

:nodoc:



173
174
175
# File 'lib/web_task_runner.rb', line 173

def task_finished_at  # :nodoc:
  WebTaskRunner.task_finished_at
end

#task_progressObject

:nodoc:



154
155
156
# File 'lib/web_task_runner.rb', line 154

def task_progress  # :nodoc:
  WebTaskRunner.task_progress
end

#task_started_atObject

:nodoc:



163
164
165
# File 'lib/web_task_runner.rb', line 163

def task_started_at  # :nodoc:
  WebTaskRunner.task_started_at
end