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: HashWithIndifferentAccess, 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 runner



186
187
188
189
190
191
192
193
# File 'lib/web_task_runner.rb', line 186

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



139
140
141
# File 'lib/web_task_runner.rb', line 139

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

.current_statusObject

Get the status of the task



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/web_task_runner.rb', line 200

def self.current_status
  task_status = WebTaskRunner::RedisModule.redis.get('task:status')
  return {} unless task_status

  status = { status: task_status }
  status[:progress] = task_progress if task_progress && task_status == 'processing'
  status[:started_at] = task_started_at if task_started_at
  status[:finished_at] = task_finished_at if task_finished_at

  status
end

.job_ended(all: false) ⇒ Object

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



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

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:status', 'ok')
    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



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/web_task_runner.rb', line 120

def self.kill_task
  ps = Sidekiq::ProcessSet.new
  killed_count = 0
  ps.each do |p|
    p.stop! and killed_count += 1 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)
  WebTaskRunner::RedisModule.redis.set('task:status', 'error') if killed_count > 0
end

.start_task(params = nil) ⇒ Object

Starts (or kill and restart) the task



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/web_task_runner.rb', line 81

def self.start_task(params = nil)
  kill_task
  WebTaskRunner::RedisModule.redis.set('task:state', 'working')
  WebTaskRunner::RedisModule.redis.set('task:status', 'processing')
  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 do |job|
    job.perform_async(params)
  end

  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_idle(params = nil) ⇒ Object

Starts the task if it’s not running



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

def self.start_task_if_idle(params = nil)
  return unless current_state == 'idle'
  start_task(params)
end

.task_finished_atObject

Get the time when the task last finished



176
177
178
179
# File 'lib/web_task_runner.rb', line 176

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



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/web_task_runner.rb', line 148

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



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

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

Instance Method Details

#current_infoObject

:nodoc:



195
196
197
# File 'lib/web_task_runner.rb', line 195

def current_info  # :nodoc:
  WebTaskRunner.current_info
end

#current_stateObject

:nodoc:



143
144
145
# File 'lib/web_task_runner.rb', line 143

def current_state  # :nodoc:
  WebTaskRunner.current_state
end

#current_statusObject

:nodoc:



212
213
214
# File 'lib/web_task_runner.rb', line 212

def current_status  # :nodoc:
  WebTaskRunner.current_status
end

#kill_taskObject

:nodoc:



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

def kill_task  # :nodoc:
  WebTaskRunner.kill_task
end

#start_task(params = nil) ⇒ Object

:nodoc:



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

def start_task(params = nil)  # :nodoc:
  WebTaskRunner.start_task(params)
end

#start_task_if_idle(params = nil) ⇒ Object

:nodoc:



115
116
117
# File 'lib/web_task_runner.rb', line 115

def start_task_if_idle(params = nil)  # :nodoc:
  WebTaskRunner.start_task_if_idle(params)
end

#task_finished_atObject

:nodoc:



181
182
183
# File 'lib/web_task_runner.rb', line 181

def task_finished_at  # :nodoc:
  WebTaskRunner.task_finished_at
end

#task_progressObject

:nodoc:



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

def task_progress  # :nodoc:
  WebTaskRunner.task_progress
end

#task_started_atObject

:nodoc:



171
172
173
# File 'lib/web_task_runner.rb', line 171

def task_started_at  # :nodoc:
  WebTaskRunner.task_started_at
end