Class: WebTaskRunner
- Inherits:
-
Sinatra::Application
- Object
- Sinatra::Application
- WebTaskRunner
- 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
-
.current_info ⇒ Object
Get the info of the task runner.
-
.current_state ⇒ Object
Get the current state.
-
.current_status ⇒ Object
Get the status of the task.
-
.job_ended(all: false) ⇒ Object
Report that a job has been done, call this in each job after the work has done.
- .jobs ⇒ Object
-
.kill_task ⇒ Object
Kills the running task.
-
.start_task ⇒ Object
Starts (or kill and restart) the task.
-
.start_task_if_idle ⇒ Object
Starts the task if it’s not running.
-
.task_finished_at ⇒ Object
Get the time when the task last finished.
-
.task_progress ⇒ Object
Get the task progress.
-
.task_started_at ⇒ Object
Get the time when the task last started.
Instance Method Summary collapse
-
#current_info ⇒ Object
:nodoc:.
-
#current_state ⇒ Object
:nodoc:.
-
#current_status ⇒ Object
:nodoc:.
-
#kill_task ⇒ Object
:nodoc:.
-
#start_task ⇒ Object
:nodoc:.
-
#start_task_if_idle ⇒ Object
:nodoc:.
-
#task_finished_at ⇒ Object
:nodoc:.
-
#task_progress ⇒ Object
:nodoc:.
-
#task_started_at ⇒ Object
:nodoc:.
Class Method Details
.current_info ⇒ Object
Get the info of the task runner
194 195 196 197 198 199 200 201 |
# File 'lib/web_task_runner.rb', line 194 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_state ⇒ Object
Get the current state
147 148 149 |
# File 'lib/web_task_runner.rb', line 147 def self.current_state WebTaskRunner::RedisModule.redis.get('task:state') || 'idle' end |
.current_status ⇒ Object
Get the status of the task
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/web_task_runner.rb', line 208 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/web_task_runner.rb', line 65 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 |
.jobs ⇒ Object
16 17 18 |
# File 'lib/web_task_runner.rb', line 16 def self.jobs @@jobs end |
.kill_task ⇒ Object
Kills the running task
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/web_task_runner.rb', line 128 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 ⇒ Object
Starts (or kill and restart) the task
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/web_task_runner.rb', line 91 def self.start_task 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(&: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_idle ⇒ Object
Starts the task if it’s not running
118 119 120 121 |
# File 'lib/web_task_runner.rb', line 118 def self.start_task_if_idle return unless current_state == 'idle' start_task end |
.task_finished_at ⇒ Object
Get the time when the task last finished
184 185 186 187 |
# File 'lib/web_task_runner.rb', line 184 def self.task_finished_at return nil if current_state != 'idle' try_to_parse_date_from_redis('task:finished_at') end |
.task_progress ⇒ Object
Get the task progress
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/web_task_runner.rb', line 156 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_at ⇒ Object
Get the time when the task last started
175 176 177 |
# File 'lib/web_task_runner.rb', line 175 def self.task_started_at try_to_parse_date_from_redis('task:started_at') end |
Instance Method Details
#current_info ⇒ Object
:nodoc:
203 204 205 |
# File 'lib/web_task_runner.rb', line 203 def current_info # :nodoc: WebTaskRunner.current_info end |
#current_state ⇒ Object
:nodoc:
151 152 153 |
# File 'lib/web_task_runner.rb', line 151 def current_state # :nodoc: WebTaskRunner.current_state end |
#current_status ⇒ Object
:nodoc:
220 221 222 |
# File 'lib/web_task_runner.rb', line 220 def current_status # :nodoc: WebTaskRunner.current_status end |
#kill_task ⇒ Object
:nodoc:
142 143 144 |
# File 'lib/web_task_runner.rb', line 142 def kill_task # :nodoc: WebTaskRunner.kill_task end |
#start_task ⇒ Object
:nodoc:
113 114 115 |
# File 'lib/web_task_runner.rb', line 113 def start_task # :nodoc: WebTaskRunner.start_task end |
#start_task_if_idle ⇒ Object
:nodoc:
123 124 125 |
# File 'lib/web_task_runner.rb', line 123 def start_task_if_idle # :nodoc: WebTaskRunner.start_task_if_idle end |
#task_finished_at ⇒ Object
:nodoc:
189 190 191 |
# File 'lib/web_task_runner.rb', line 189 def task_finished_at # :nodoc: WebTaskRunner.task_finished_at end |
#task_progress ⇒ Object
:nodoc:
170 171 172 |
# File 'lib/web_task_runner.rb', line 170 def task_progress # :nodoc: WebTaskRunner.task_progress end |
#task_started_at ⇒ Object
:nodoc:
179 180 181 |
# File 'lib/web_task_runner.rb', line 179 def task_started_at # :nodoc: WebTaskRunner.task_started_at end |