Class: Job
Overview
This class manages the job execution and its status on a job handler.
Constant Summary collapse
- @@debug =
false- @@log =
Logger.new(STDOUT)
Class Method Summary collapse
-
.debug(mode) ⇒ Object
Sets debug mode mode: boolean.
Instance Method Summary collapse
-
#error!(instance = 0) ⇒ Object
Sets a job in ERROR status.
-
#finish ⇒ Object
Change instance counter If workflow is in suspend status, suspend the job at the end of its treatment.
-
#isover? ⇒ Boolean
Compares intance counter to max instances to determine if job is over.
-
#logMessage(operation, msg) ⇒ Object
If debug, log the message in the database.
-
#resume ⇒ Object
Resume a suspended job Update its status and run next jobs.
-
#run(curhandler, instance = 0) ⇒ Object
Update job status and launch the command locally Sends a finish message or an error message according to the job status.
-
#runcommand(workdir, instance) ⇒ Object
Execute locally the command, creating directories and setting environment variables to empty string for security.
-
#runnext(skip = false) ⇒ Object
Run next jobs.
-
#sendmessage(operation, msg, jobqueue = '') ⇒ Object
Sends a message.
-
#skip(curhandler) ⇒ Object
Skip treatment, just answer, for debug.
Class Method Details
.debug(mode) ⇒ Object
Sets debug mode mode: boolean
32 33 34 |
# File 'lib/manband/job.rb', line 32 def self.debug(mode) @@debug = mode end |
Instance Method Details
#error!(instance = 0) ⇒ Object
Sets a job in ERROR status. instance: job instance number in fault
206 207 208 209 210 211 212 213 214 |
# File 'lib/manband/job.rb', line 206 def error!(instance = 0) @status= STATUS_ERROR curjob = Job.get(@id) err = JSON.parse(curjob.error) err.push(instance) curjob.update(:status => STATUS_ERROR, :error => err.to_json) workflow = WorkFlow.get(@wid) workflow.update(:status => STATUS_ERROR) end |
#finish ⇒ Object
Change instance counter If workflow is in suspend status, suspend the job at the end of its treatment
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/manband/job.rb', line 127 def finish if @status == STATUS_SKIP return end workflow = WorkFlow.get(@wid) if workflow.status == STATUS_SUSPEND @status= STATUS_SUSPEND DataMapper.repository(:default).adapter.execute('UPDATE jobs SET instances = instances + 1, status = '+STATUS_SUSPEND+' WHERE id='+@id.to_s); curjob = Job.get(@id) #curjob = Job.get(@id) #instancesplusone = curjob.instances + 1 #curjob.update(:status => STATUS_SUSPEND, :instances => instancesplusone) elsif @status == STATUS_SUSPEND # Job set in suspend mode (breakpoint), keep in this status #instancesplusone = curjob.instances + 1 #curjob.update(:instances => instancesplusone) DataMapper.repository(:default).adapter.execute('UPDATE jobs SET instances = instances + 1 WHERE id='+@id.to_s); curjob = Job.get(@id) else @status= STATUS_OVER DataMapper.repository(:default).adapter.execute('UPDATE jobs SET instances = instances + 1 WHERE id='+@id.to_s); # TODO add a lock for update curjob = Job.get(@id) if curjob.instances >= curjob.maxinstances curjob.update(:status => STATUS_OVER) else end end end |
#isover? ⇒ Boolean
Compares intance counter to max instances to determine if job is over.
158 159 160 161 162 163 164 165 166 |
# File 'lib/manband/job.rb', line 158 def isover? if @status == STATUS_OVER return true elsif @status == STATUS_SKIP return true else return false end end |
#logMessage(operation, msg) ⇒ Object
If debug, log the message in the database
302 303 304 305 306 307 |
# File 'lib/manband/job.rb', line 302 def logMessage(operation,msg) if @@debug == true bmsg = BandMessage.new(:wid => @wid, :message => '{ "operation" => '+operation+', "msg" => '+msg+' }' ) bmsg.save end end |
#resume ⇒ Object
Resume a suspended job Update its status and run next jobs
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/manband/job.rb', line 170 def resume workflow = WorkFlow.get(@wid) # if status = suspend, continue to next # if status = error, restart job if @status == STATUS_SUSPEND @@log.debug "Resume from suspended job "+@id.to_s+" for workflow "+@wid.to_s @status= STATUS_OVER curjob = Job.get(@id) curjob.update(:status => STATUS_OVER) runnext elsif @status == STATUS_ERROR @@log.debug "Resume from error job "+@id.to_s+" for workflow "+@wid.to_s @status = STATUS_RUNNING curjob = Job.get(@id) commands = workflow.getnodecommand(curjob.node) if commands == nil @@log.error "Could not get command for node "+curjob.node return end # Restart whole job, use new tmp dir, reset errors curjob.update(:status => STATUS_RUNNING, :command => commands.to_json, :instances => 0, :workdir => FlowConfig.getjobdir(workflow.workdir), :error => "[]", :maxinstances => commands.length) i = 0 if commands.length > 1 i = 1 end commands.each do |command| jobmsg = '{ "workflow" : "'+@wid.to_s+'" , "node" : "'+@node+'", "id" : "'+@id.to_s+'", "instance" : "'+i.to_s+'" }' i += 1 # send message (OP_RUN,jobmsg,curjob.queue) end end end |
#run(curhandler, instance = 0) ⇒ Object
Update job status and launch the command locally Sends a finish message or an error message according to the job status. if storage is needed, job will send a finish AND a store message. Storage will occur in parallel of the rest of the workflow.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/manband/job.rb', line 56 def run(curhandler,instance=0) # Send run command, possibly multiple ones according to pattern # Command would send a message when over curjob = Job.get(@id) if @status != STATUS_SUSPEND @status=STATUS_RUNNING curjob.update(:status => STATUS_RUNNING, :handler => curhandler.to_s) else curjob.update(:handler => curhandler.to_s) end workdir = curjob.workdir if instance>0 workdir = workdir + "/node" + instance.to_s end EventMachine.defer do err = runcommand(workdir,instance) if err == nil || err == false # An error occured jobmsg = '{ "workflow" : "'+@wid.to_s+'" , "node" : "'+@node+'", "id" : "'+@id.to_s+'", "handler" : "'+curhandler.to_s+'", "instance" : "'+instance.to_s+'" }' (OP_ERROR,jobmsg) else if curjob.store == STORE_DO || curjob.store == STORE_ERROR workflow = WorkFlow.get(curjob.wid) jobmsg = '{ "id" : "'+curjob.id.to_s+'", "bucket" : "'+workflow.bucket+'" }' (OP_STORE,jobmsg) end jobmsg = '{ "workflow" : "'+@wid.to_s+'" , "node" : "'+@node+'", "id" : "'+@id.to_s+'", "handler" : "'+curhandler.to_s+'", "instance" : "'+instance.to_s+'" }' (OP_FINISH,jobmsg) end end # End EventMachine end |
#runcommand(workdir, instance) ⇒ Object
Execute locally the command, creating directories and setting environment variables to empty string for security. wordir: job working directory instance: instance number in the list of commands
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/manband/job.rb', line 100 def runcommand(workdir,instance) initcmd = "AMQP_URL="" && MYSQL_URL="" && mkdir -p "+workdir+" && cd "+workdir+" && WORKDIR="+workdir+" && " curjob = Job.get(@id) command = JSON.parse(curjob.command) if command.length > 1 cmd = initcmd+command[0] else cmd = initcmd+command[instance-1] end if FlowConfig.sudo() == true wflow = WorkFlow.get(@wid) script = wflow.workdir+"/"+self.id.to_s+"-"+instance.to_s+".sh" if !File.exists?(wflow.workdir) Dir.mkdir(wflow.workdir) system("chown -R "+wflow.uid+" "+wflow.workdir) end File.open(script, 'w') {|f| f.write("#!/bin/bash\n"+cmd) } File.chmod(0755,script) cmd = "sudo -u "+wflow.uid+" '"+script+"'" end @@log.debug cmd system(cmd) end |
#runnext(skip = false) ⇒ Object
Run next jobs
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/manband/job.rb', line 217 def runnext(skip=false) workflow = WorkFlow.get(@wid) # Are all previous jobs over? previous = JobLink.all(:to => @id) #nexts = workflow.getnextjobs(@node) if previous!=nil @@log.debug "Checking previous jobs: "+previous.length.to_s end if previous == nil || previous.length==0 # This is fine, must be a root node elsif previous.length==1 # Only 1 parent. As we got the message, previous is over else # Check each job previous.each do |link| pjob = Job.get(link.from) if pjob.status != STATUS_OVER @@log.debug "At least one previous job is not over ("+pjob.id.to_s+"), wait for next message" return false end end @@log.debug "All previous jobs are over, continue...." end # Look at next jobs nexts = JobLink.all(:from => @id) #nexts = workflow.getnextjobs(@node) if nexts == nil || nexts.length==0 workflow.isover? return true end nexts.each do |link| job = Job.get(link.to) #nexts.each do |nextnode| #nextnode.strip! #queue = workflow.getnodequeue(nextnode) if skip==true || @status == STATUS_SKIP jobstatus = STATUS_SKIP operation = OP_SKIP commands = [ "skipcommand"] else jobstatus = STATUS_NEW operation = OP_RUN commands = workflow.getnodecommand(job.node) end if commands == nil @@log.error "Could not get command for node "+job.id.to_s job.update(:command => "", :status => STATUS_ERROR, :instances => 0, :maxinstances => 0, :workdir => FlowConfig.getjobdir(workflow.workdir)) workflow.update(:status => STATUS_ERROR) return end @@log.debug "New job command: "+commands.to_s job.update(:command => commands.to_json, :status => jobstatus, :instances => 0, :maxinstances => commands.length, :workdir => FlowConfig.getjobdir(workflow.workdir)) #err = job.save i=0 # If multiple instances, differenciate and start at 1 if commands.length > 1 i = 1 end commands.each do |command| jobmsg = '{ "workflow" : "'+job.wid.to_s+'" , "node" : "'+job.node+'", "id" : "'+job.id.to_s+'", "instance" : "'+i.to_s+'" }' i += 1 # send message (operation,jobmsg,job.queue) end end end |
#sendmessage(operation, msg, jobqueue = '') ⇒ Object
Sends a message. According to the operation, message will be sent to master or node queues. operation: kind of message msg: message to send jobqueue: optional specific queue
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/manband/job.rb', line 290 def (operation,msg,jobqueue='') queue = "manband.master" if operation == OP_RUN || operation == OP_SKIP || operation == OP_DESTROY || operation == OP_CLEAN|| operation == OP_STORE queue = "manband.node"+jobqueue end if queue != nil Utils.enqueue(queue, { "operation" => operation, "msg" => msg }) logMessage(operation,msg) end end |
#skip(curhandler) ⇒ Object
Skip treatment, just answer, for debug
89 90 91 92 93 94 |
# File 'lib/manband/job.rb', line 89 def skip(curhandler) curjob = Job.get(@id) curjob.update(:handler => curhandler.to_s) jobmsg = '{ "workflow" : "'+@wid.to_s+'" , "node" : "'+@node+'", "id" : "'+@id.to_s+'", "handler" : "'+curhandler.to_s+'" }' (OP_FINISH,jobmsg) end |