From 7d57f59dd5114cf13e308f4392c1637648eccfa8 Mon Sep 17 00:00:00 2001 From: Vincent Riquer Date: Fri, 15 Mar 2013 19:57:06 +0100 Subject: [PATCH] worker() --- atom | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 3 deletions(-) diff --git a/atom b/atom index 999596a..f80b999 100755 --- a/atom +++ b/atom @@ -1001,13 +1001,13 @@ decodeFile() { source_file $fileid command_line $commandline status 0 - cleanup $tempdir/$tmpfile.wav EOInsert ) progressSpin fi if (( sox_needed )) then + cleanup="$tempdir/$tmpfile" decodeSox "$tempdir/$tmpfile.wav" if ! soxtaskid=$( Select tasks id <<<"key = $tmpfile" @@ -1019,8 +1019,9 @@ decodeFile() { source_file $fileid command_line $commandline requires $decodetaskid + required $decodetaskid status 0 - cleanup $tempdir/$tmpfile.wav + cleanup $cleanup EOInsert ) progressSpin @@ -1128,7 +1129,11 @@ encodeFile::mp3() { Insert tasks <<-EOInsert key ${fileid}lame$destination requires ${soxtaskid:-$decodetaskid} + required ${soxtaskid:-$decodetaskid} + fileid $destfileid + filename $destdir/$destfile.mp3 command_line lame $lameopts "$tempdir/$tmpfile.wav" "$destdir/$destfile.mp3" + cleanup "$tempdir/$tmpfile.wav" source_file $fileid status 0 EOInsert @@ -1152,7 +1157,11 @@ encodeFile::vorbis() { Insert tasks <<-EOInsert key ${fileid}oggenc$destination requires ${soxtaskid:-$decodetaskid} + required ${soxtaskid:-$decodetaskid} + fileid $destfileid + filename $destdir/$destfile.ogg command_line oggenc $oggencopts -o "$destdir/$destfile.ogg" "$tempdir/$tmpfile.wav" + cleanup "$tempdir/$tmpfile.wav" source_file $fileid status 0 EOInsert @@ -1194,6 +1203,103 @@ transcodeLauncher() { #update counter / metadata } +worker() { + while : + do + if [ -f "$tempdir/worker-lock" ] + then + sleep 0.001 + continue + else + touch "$tempdir/worker-lock" + concurrency=$(Select worker_comm value <<<"parameter = concurrency") + active_workers=$(Select tasks 'count(*)' <<<"status = 1") + if (( active_workers < concurrency )) + then + echo ' +SELECT COUNT(*) +FROM tasks +WHERE status = 0 +AND requires is NULL; + +SELECT + id, + source_file, + required, + command_line, + cleanup, + fileid, + filename +FROM tasks +WHERE status = 0 +AND requires is NULL +ORDER BY source_file +LIMIT 1; +' >&3 + read -u4 count + if (( count == 0 )) + then + rm "$tempdir/worker-lock" + exit + fi + read -u4 line + taskid=${line%%|*} + rest="${line#*|}|" + sourcefileid=${rest%%|*} + rest=${rest#*|} + required=${rest%%|*} + rest=${rest#*|} + commandline=${rest%%|*} + rest=${rest#*|} + cleanup=${rest%%|*} + rest=${rest#*|} + destfileid=${rest%%|*} + rest=${rest#*|} + destfilename=${rest%%|*} + rest=${rest#*|} + Update tasks status 1 <<<"id = $taskid" + rm "$tempdir/worker-lock" + if eval $commandline 2>>"$tempdir/errors.log" + then + Delete tasks <<<"id = $taskid" + if [ -n "$destfilename" ] + then + echo \ + "UPDATE destination_files" \ + "SET filename=\"$destfilename\"," \ + " last_change=(" \ + " SELECT last_change" \ + " FROM source_files" \ + " WHERE id=$sourcefileid"\ + " )" \ + "WHERE id=$destfileid;" \ + >&3 + fi + else + Update tasks status 2 <<<"id = $taskid" + [ -n "$filename" ] && rm -f "$filename" + [ -n "$cleanup" ] && rm -f "$cleanup" + fi + if [ -n "$cleanup" -a -n "$required" ] + then + echo "SELECT COUNT(*) + FROM tasks + WHERE ( status = 0 OR status = 1 ) + AND required = $required;">&3 + read -u4 count + if (( count == 0 )) + then + eval rm $cleanup + fi + fi + else + rm "$tempdir/worker-lock" + exit + fi + fi + done +} + #UI if [ ! -f ~/.atom/atom.cfg ] @@ -1373,14 +1479,25 @@ echo ' id INTEGER PRIMARY KEY, key TEXT UNIQUE, source_file INTEGER, + fileid INTEGER, + filename TEXT, command_line TEXT, requires INTEGER, + required INTEGER, status INTEGER NOT NULL, cleanup TEXT, FOREIGN KEY(requires) REFERENCES tasks(id) ON DELETE SET NULL ); - CREATE INDEX tasks_by_key ON tasks ( key );' >&3 + CREATE INDEX tasks_by_key ON tasks ( key ); + CREATE INDEX tasks_by_sourcefile ON tasks ( source_file ); + + CREATE TEMPORARY TABLE worker_comm( + parameter TEXT UNIQUE NOT NULL, + value TEXT + ); + CREATE INDEX wrkcomm_by_param ON worker_comm ( parameter ); +' >&3 echo ' SELECT COUNT(source_files.id) @@ -1571,6 +1688,9 @@ echo 'COMMIT;' >&3 echo -e "\rCreated ${count:-0} tasks for $filecount files" unset count +InsertOrUpdate worker_comm value 1 <<<"parameter concurrency" +worker + closeDatabase # vim:set ts=8 sw=8: