sync met werkbranch
This commit is contained in:
parent
3305750de2
commit
7d36791118
4 changed files with 90 additions and 71 deletions
145
pbatch/pb.rb
145
pbatch/pb.rb
|
|
@ -4,6 +4,8 @@
|
|||
# $URL$
|
||||
|
||||
require 'fileutils'
|
||||
require 'thread'
|
||||
require 'open3'
|
||||
|
||||
JOBSDIR = "jobs"
|
||||
PARKDIR = "park"
|
||||
|
|
@ -14,6 +16,8 @@ MONITORDIR = "monitor"
|
|||
RUNMODE = 0755
|
||||
CONFIG = "config"
|
||||
@config = { "waittime" => 3, "maxproc" => 3 }
|
||||
@processor_threads = []
|
||||
@jobclaimlock = Mutex.new
|
||||
|
||||
COUNTERFILE="jobscounter"
|
||||
RUNNINGFILE="runningcounter"
|
||||
|
|
@ -22,19 +26,19 @@ def usage
|
|||
puts <<ENDTXT
|
||||
Usage: pb <option>
|
||||
|
||||
add <command>>-->-------Add the given command as a job.
|
||||
addfile|af <file>>------Add the given file as a job.
|
||||
createconfing>-->-------Write a default configuration file.
|
||||
run>---->------->-------Start the master scheduler process.
|
||||
del <jobnr>>---->-------Delete the given job from the queue.
|
||||
help|-h>>------->-------Show this help.
|
||||
park>--->------->-------Place all queued jobs in the parkinglot.
|
||||
unpark>->------->-------Place all jobs from the parkinglot back in the queue.
|
||||
retry <jobnr>>-->-------Reschedule a failed job. Destroys all output.
|
||||
prio <h|n|l> <jobnr>>---Change the priority of the given job.>--[unimplemented]
|
||||
status|st>------>-------Show the status of the current jobs.
|
||||
stop <jobnr>>--->-------Stop the given job.>----[unimplemented]
|
||||
stopall>>------->-------Stop all running jobs.>-[unimplemented]
|
||||
add <command> Add the given command as a job.
|
||||
addfile|af <file> Add the given file as a job.
|
||||
createconfing Write a default configuration file.
|
||||
run Start the master scheduler process.
|
||||
del <jobnr> Delete the given job from the queue.
|
||||
help|-h Show this help.
|
||||
park Place all queued jobs in the parkinglot.
|
||||
unpark Place all jobs from the parkinglot back in the queue.
|
||||
retry <jobnr> Reschedule a failed job. Destroys all output.
|
||||
prio <h|n|l> <jobnr> Change the priority of the given job. [unimplemented]
|
||||
status|st Show the status of the current jobs.
|
||||
stop <jobnr> Stop the given job. [unimplemented]
|
||||
stopall Stop all running jobs. [unimplemented]
|
||||
ENDTXT
|
||||
exit
|
||||
end
|
||||
|
|
@ -136,7 +140,7 @@ def addjobfromfile
|
|||
end
|
||||
jobnumber = sprintf("%06d", increasejobscounter)
|
||||
puts "New job: #{jobnumber}"
|
||||
File.copy(filename, "#{@basedir}/#{JOBSDIR}/#{jobnumber}")
|
||||
FileUtils.cp(filename, "#{@basedir}/#{JOBSDIR}/#{jobnumber}")
|
||||
File.chmod(RUNMODE, "#{@basedir}/#{JOBSDIR}/#{jobnumber}")
|
||||
end
|
||||
|
||||
|
|
@ -209,35 +213,6 @@ def changeprio
|
|||
puts "unimplemented"
|
||||
end
|
||||
|
||||
def incrementrunning
|
||||
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig)
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_EX)
|
||||
File.open(RUNNINGFILE, "r+"){|file|
|
||||
running = file.read
|
||||
running = running.to_i + 1
|
||||
file.seek(0)
|
||||
file.trunc
|
||||
file.puts running
|
||||
}
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_UN)
|
||||
end
|
||||
|
||||
def decrementrunning
|
||||
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig)
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_EX)
|
||||
File.open(RUNNINGFILE, "r+"){|file|
|
||||
running = file.read
|
||||
running = running.to_i - 1
|
||||
if running < 0
|
||||
running = 0
|
||||
end
|
||||
file.seek(0)
|
||||
file.trunc
|
||||
file.puts running
|
||||
}
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_UN)
|
||||
end
|
||||
|
||||
def getjob
|
||||
Dir.entries("#{@basedir}/#{JOBSDIR}").each{|file|
|
||||
if file == '.' or file == '..'
|
||||
|
|
@ -250,6 +225,7 @@ def getjob
|
|||
end
|
||||
end
|
||||
}
|
||||
return nil
|
||||
end
|
||||
|
||||
def failjob(jobnumber)
|
||||
|
|
@ -261,24 +237,46 @@ def finishjob(jobnumber)
|
|||
end
|
||||
|
||||
def runprocessor
|
||||
incrementrunning
|
||||
sleep 1 # XXX niet nodig als locking echt werkt (of bij threading)
|
||||
runjob = getjob
|
||||
if ! runjob.nil?
|
||||
puts "Starting job: #{runjob}"
|
||||
owd = Dir.pwd
|
||||
Dir.cd("#{@basedir}/#{PROCDIR}/#{runjob}")
|
||||
# XXX IPC nog even uitzoeken... popen3 ?
|
||||
# if "${basedir}/processors/${runjob}/job" 2>&1 > "${basedir}/processors/${runjob}/output" ;then
|
||||
# finishjob ${runjob}
|
||||
puts "Finished job: #{runjob}"
|
||||
# else
|
||||
# failjob ${runjob}
|
||||
puts "Failed job: #{runjob}"
|
||||
# fi
|
||||
Dir.cd(owd)
|
||||
@processor_threads << Thread.new do
|
||||
begin
|
||||
# gebruik een mutex voor het claimen van een job!
|
||||
runjob = nil
|
||||
@jobclaimlock.synchronize {
|
||||
runjob = getjob
|
||||
}
|
||||
|
||||
if ! runjob.nil?
|
||||
starttime = Time.now
|
||||
puts "Starting job: #{runjob}"
|
||||
owd = Dir.pwd
|
||||
# puts "#{@basedir}/#{PROCDIR}/#{runjob}"
|
||||
Dir.chdir("#{@basedir}/#{PROCDIR}/#{runjob}")
|
||||
begin
|
||||
output = File.open("#{@basedir}/#{PROCDIR}/#{runjob}/output", "w")
|
||||
Open3.popen3("#{@basedir}/#{PROCDIR}/#{runjob}/job", "r+"){ |stdin, stdout, stderr|
|
||||
# dit moet netter kunnen, nu wordt de volgorde van de diverse outputs veranderd
|
||||
while !stdout.eof and ! stderr.eof do
|
||||
#output.print "XXX ERR #{stderr.gets}"
|
||||
#output.print "XXX OUT #{stdout.gets}"
|
||||
output.print stderr.gets
|
||||
output.print stdout.gets
|
||||
end
|
||||
}
|
||||
output.close
|
||||
finishjob runjob
|
||||
puts "Finished job: #{runjob} [runtime: #{Time.now-starttime}]"
|
||||
rescue
|
||||
failjob runjob
|
||||
puts "Failed job: #{runjob} [runtime: #{Time.now-starttime}]"
|
||||
puts $!
|
||||
end
|
||||
Dir.chdir(owd)
|
||||
end
|
||||
rescue
|
||||
puts "failure in thread"
|
||||
puts $!
|
||||
end
|
||||
end
|
||||
decrementrunning
|
||||
end
|
||||
|
||||
def stop
|
||||
|
|
@ -291,18 +289,27 @@ end
|
|||
|
||||
def master
|
||||
if Process.euid != 0
|
||||
puts "If your jobs need root priviledges (XXX spelling) you want to run this as root"
|
||||
puts "If your jobs need root privileges you want to run this as root"
|
||||
end
|
||||
|
||||
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig)
|
||||
if ! FileTest.file?(RUNNINGFILE)
|
||||
FileUtils.touch(RUNNINGFILE)
|
||||
while true
|
||||
sleep @config["waittime"]
|
||||
# puts "Number of threads: #{@processor_threads.length}"
|
||||
# dode threads moeten opgeruimd worden
|
||||
(0...@processor_threads.length).to_a.reverse.each{|thr|
|
||||
# puts "thr: #{thr}"
|
||||
# puts @processor_threads[thr].status
|
||||
if @processor_threads[thr].status == false
|
||||
# puts "deleting thread #{thr}"
|
||||
@processor_threads.delete_at(thr)
|
||||
end
|
||||
}
|
||||
if @processor_threads.length < @config["maxproc"]
|
||||
# nog testen op aanwezig zijn van nieuwe jobs in queue?
|
||||
runprocessor
|
||||
end
|
||||
readconfig
|
||||
end
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_EX)
|
||||
File.open(RUNNINGFILE, "w"){|file|
|
||||
file.puts "0"
|
||||
}
|
||||
File.new(RUNNINGFILE).flock(File::LOCK_UN)
|
||||
|
||||
# while sleep $waittime; do
|
||||
# if [ $(cat "${basedir}/${runningfile}") -lt $maxproc ]; then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue