From d0cb1429527224d9a37821ec0d5f56144b6e46a8 Mon Sep 17 00:00:00 2001 From: hummypkg Date: Wed, 21 Dec 2016 12:58:25 +0000 Subject: [PATCH] auto now runs every minute, checking for time to run git-svn-id: file:///root/webif/svn/pkg/webif/trunk@3442 2a923420-c742-0410-a762-8d5b09965624 --- CONTROL/control | 2 +- CONTROL/postinst | 3 +- webif/html/browse/epgtitle.jim | 2 +- webif/html/browse/mp3.jim | 2 +- webif/html/browse/newdir.jim | 2 +- webif/html/browse/shrunk.jim | 2 +- webif/html/browse/sizes.jim | 4 +- webif/html/diag/queue/fetch.jim | 2 +- webif/html/settings/modules/auto/init.hook | 6 +- .../html/settings/modules/auto/settings.hook | 11 +- webif/lib/bin/auto | 100 ++++++++++++------ webif/lib/escape | 14 --- webif/lib/json | 44 ++++---- webif/lib/queue.class | 14 +++ webif/lib/settings.class | 10 ++ 15 files changed, 139 insertions(+), 79 deletions(-) delete mode 100644 webif/lib/escape diff --git a/CONTROL/control b/CONTROL/control index e710b1b..f2491b0 100644 --- a/CONTROL/control +++ b/CONTROL/control @@ -1,7 +1,7 @@ Package: webif Priority: optional Section: web -Version: 1.3.4-5 +Version: 1.3.4-6 Architecture: mipsel Maintainer: af123@hpkg.tv Depends: tcpfix,webif-channelicons(>=1.1.24),lighttpd(>=1.4.39-1),jim(>=0.77),jim-oo(>=0.77),jim-sqlite3(>=0.76),jim-cgi(>=0.7-1),jim-binary(>=0.76),service-control(>=2.3),busybox(>=1.20.2-1),lsof(>=4.87),epg(>=1.2.3),hmt(>=2.0.10),ssmtp,cron-daemon(>=1.18.3-3),at(>=3.1.18),anacron,trm(>=1.1),openssl-command,nicesplice,id3v2,file,rsvsync(>=1.1.9),webif-charts(>=1.2-1),stripts(>=1.2.5-3),tmenu(>=1.08),ffmpeg,id3v2,multienv(>=1.6),tcpping(>=1.1),e2fsprogs,wireless-tools(>=29-1),dbupdate,recmon(>=2.0.7),hwctl,nugget(>=0.95),sqlite3(>=3.15.1) diff --git a/CONTROL/postinst b/CONTROL/postinst index d3d6917..6d92a5c 100755 --- a/CONTROL/postinst +++ b/CONTROL/postinst @@ -20,8 +20,7 @@ crontab=$PKG_ROOT/bin/crontab if [ -x $crontab ]; then $crontab -l | grep -v webif/lib/bin/auto > $tmpf cat $tmpf - << EOM | $crontab - -5,25,45 * * * * /mod/webif/lib/bin/auto >/dev/null 2>&1 -*/3 * * * * /mod/webif/lib/bin/auto -queue >/dev/null 2>&1 +* * * * * /mod/webif/lib/bin/auto >/dev/null 2>&1 EOM fi diff --git a/webif/html/browse/epgtitle.jim b/webif/html/browse/epgtitle.jim index ff5616c..9601faf 100755 --- a/webif/html/browse/epgtitle.jim +++ b/webif/html/browse/epgtitle.jim @@ -2,7 +2,7 @@ package require cgi source /mod/webif/lib/setup -require ts.class escape +require ts.class json httpheader "application/json" diff --git a/webif/html/browse/mp3.jim b/webif/html/browse/mp3.jim index 416e11b..aadb43b 100755 --- a/webif/html/browse/mp3.jim +++ b/webif/html/browse/mp3.jim @@ -2,7 +2,7 @@ package require cgi source /mod/webif/lib/setup -require escape +require json httpheader "application/json" diff --git a/webif/html/browse/newdir.jim b/webif/html/browse/newdir.jim index 5839ef8..a4f2d8c 100755 --- a/webif/html/browse/newdir.jim +++ b/webif/html/browse/newdir.jim @@ -3,7 +3,7 @@ package require cgi package require pack source /mod/webif/lib/setup -require escape +require json httpheader "application/json" diff --git a/webif/html/browse/shrunk.jim b/webif/html/browse/shrunk.jim index cb72c9d..ebe799a 100755 --- a/webif/html/browse/shrunk.jim +++ b/webif/html/browse/shrunk.jim @@ -2,7 +2,7 @@ package require cgi source /mod/webif/lib/setup -require ts.class escape +require ts.class json httpheader "application/json" diff --git a/webif/html/browse/sizes.jim b/webif/html/browse/sizes.jim index 7f0086a..16bbb49 100755 --- a/webif/html/browse/sizes.jim +++ b/webif/html/browse/sizes.jim @@ -2,7 +2,7 @@ package require cgi source /mod/webif/lib/setup -require escape +require json httpheader "application/json" @@ -16,7 +16,7 @@ foreach line [split [exec /mod/bin/busybox/du -h -l -d 1 "$dir/"] "\n"] { lassign [split $line "\t"] size node set node [string range $node $dlen end] if {[string length $node]} { - puts "\"$node\": \"$size\"," + puts "\"[jescape $node]\": \"$size\"," } } diff --git a/webif/html/diag/queue/fetch.jim b/webif/html/diag/queue/fetch.jim index cf29cf3..733c0d5 100755 --- a/webif/html/diag/queue/fetch.jim +++ b/webif/html/diag/queue/fetch.jim @@ -20,7 +20,7 @@ foreach q [queue all] { puts "{" puts " \"qid\": [$q get id]," puts " \"dat\": \"$dat\"," - puts " \"file\": \"$name\"," + puts " \"file\": \"[::json::escape $name]\"," puts " \"action\": \"[$q get action]\"," puts " \"status\": \"[$q get status]\"," puts " \"log\": \"[::json::escape [$q get log]]\"," diff --git a/webif/html/settings/modules/auto/init.hook b/webif/html/settings/modules/auto/init.hook index ddfd76a..00620ab 100644 --- a/webif/html/settings/modules/auto/init.hook +++ b/webif/html/settings/modules/auto/init.hook @@ -7,13 +7,15 @@ if {$autorecperiod == 0} { set autorecperiod 10 } set autokeep [$settings autokeep] if {$autokeep == 0} { set autokeep 7 } set noautohours [$settings noautohours] +set autofreq [$settings autofreq] +if {$autofreq == 0} { set autofreq 20 } handle_int_update autolog $autolog "Auto-processing log level" handle_int_update noautorec $noautorec "Auto-processing during recording" handle_int_update noautorecimm $noautorecimm "Auto-processing before recording" handle_int_update autorecperiod $autorecperiod \ "Auto-processing recording wait period" 1 1 $(60 * 24) -handle_int_update autokeep $autokeep \ - "Auto-processing queue period" 1 1 365 +handle_int_update autokeep $autokeep "Auto-processing queue period" 1 1 365 +handle_int_update autofreq $autofreq "Auto-processing frequency" 1 5 60 handle_str_update noautohours $noautohours "Auto processing hours" ascii diff --git a/webif/html/settings/modules/auto/settings.hook b/webif/html/settings/modules/auto/settings.hook index cf5075b..30573a2 100755 --- a/webif/html/settings/modules/auto/settings.hook +++ b/webif/html/settings/modules/auto/settings.hook @@ -38,6 +38,9 @@ puts " " +setting_number autofreq "How often should auto-processing run (in minutes)?" \ + $autofreq 5 60 + setting_toggle "Suspend automatic processing whilst recording?" \ "noautorec" $noautorec setting_toggle "Suspend automatic processing if will record soon?" \ @@ -46,10 +49,6 @@ setting_toggle "Suspend automatic processing if will record soon?" \ setting_number autorecperiod "...how many minutes is soon?" $autorecperiod \ 1 $(60 * 24) -setting_number autokeep \ - "How many days should completed entries stay in the queue?" $autokeep \ - 1 365 - puts -nonewline "
@@ -78,6 +77,10 @@ puts " " +setting_number autokeep \ + "How many days should completed entries stay in the queue?" $autokeep \ + 1 365 + puts { diff --git a/webif/lib/bin/auto b/webif/lib/bin/auto index 59849b0..9b107f3 100755 --- a/webif/lib/bin/auto +++ b/webif/lib/bin/auto @@ -45,19 +45,27 @@ while {[llength $argv]} { set earlyexit 1 } default { - # Unknown option, pass to rest of script. + # Pass to rest of script. break } } set argv [lrange $argv 1 end] } +set manualrun $([llength $argv] > 0) +if {$loglevel > 1} { + if {$logfd ne "unset"} { + puts $logfd "Manual run: $manualrun" + } +} + ######################################################################### +# Acquire lock if {$logfd ne "unset"} { puts $logfd "Acquiring lock..." } -if {!$prelocked && ![acquire_lock webif_auto 10 1]} { +if {!$prelocked && ![acquire_lock webif_auto]} { if {$loglevel > 1} { system plog auto "Could not acquire lock." } @@ -71,10 +79,13 @@ if {[file exists $logfile] && [file size $logfile] > 2097152} { file delete $logfile } +# Open log file if {$logfd eq "unset"} { set logfd [open "/mod/tmp/auto.log" "a+"] } +######################################################################### + proc log {msg {level 1}} { if {$level > $::loglevel} return puts $::logfd "[system logtimestamp] - $::logprefix$msg" @@ -83,6 +94,7 @@ proc log {msg {level 1}} { proc oktorun {} { global settings + set now [clock seconds] # Ongoing scheduled recording @@ -125,6 +137,43 @@ proc oktorun {} { } } +proc dsc {{size 0}} { + set free [system diskfree] + + # Required disk space is 1GiB + 3 times the file size. + set req $($size * 3 + 1073741824) + + if {$free < $req} { + log "Insufficient disk space. Require=$req, Free=$free" 0 + exit + } +} + +# Determine if it's time to run + +dsc +oktorun +if {!$manualrun} { + set autofreq [$settings autofreq] + + set timesincelast $(([clock seconds] - [$settings autolast]) / 60) + if {![queue size] && $timesincelast < $autofreq} { + log "Aborting, not yet time to run." 2 + log " elapsed (minutes): $timesincelast (<$autofreq)" 2 + exit + } +} + +if {$earlyexit} { + if {!$manualrun} { + $settings autolast [clock seconds] + } + puts "Early exit." + exit +} + +######################################################################### + proc inuse {ts} { set retries 5 while {$retries > 0 && [$ts inuse]} { @@ -195,13 +244,7 @@ proc runplugin {name {ts 0}} { } } -# Check if it's ok to run now. -oktorun - -if {$earlyexit} { - puts "Early exit." - exit -} +######################################################################### eval_plugins auto 1 @@ -222,19 +265,7 @@ if {[system dlnastatus]} { log "DLNA Server is NOT running." 2 } -log "Media scan starting, DLNA server status: $dlnaok" - -proc dsc {{size 0}} { - set free [system diskfree] - - # Required disk space is 1GiB + 3 times the file size. - set req $($size * 3 + 1073741824) - - if {$free < $req} { - log "Insufficient disk space. Require=$req, Free=$free" 0 - exit - } -} +log "Auto processing starting, DLNA server status: $dlnaok" dsc @@ -873,13 +904,8 @@ proc scansingle {dirs} { } } -file stat "$root/" rootstat -set rootdev $rootstat(dev) -log "Root device: $rootdev" 2 - -if {[lindex $argv 0] eq "-singledir"} { - scansingle [lrange $argv 1 end] -} elseif {[lindex $argv 0] eq "-queue"} { +proc process_queue {} { + global settings log "Processing queue..." 2 queue startup [$settings autokeep] while {[llength [set q [queue pop]]]} { @@ -910,6 +936,18 @@ if {[lindex $argv 0] eq "-singledir"} { $q update COMPLETE \ "Completed at [clock format [clock seconds]]" 0 $elapsed } +} + +file stat "$root/" rootstat +set rootdev $rootstat(dev) +log "Root device: $rootdev" 2 + +###################################################################### + +if {[lindex $argv 0] eq "-singledir"} { + scansingle [lrange $argv 1 end] +} elseif {[lindex $argv 0] eq "-queue"} { + process_queue } elseif {[llength $argv] > 0} { set loglevel 2 foreach arg $argv { @@ -926,6 +964,7 @@ if {[lindex $argv 0] eq "-singledir"} { log "$arg scan completed in [elapsed $st] seconds." } } else { + process_queue foreach arg $modules { set st [clock milliseconds] log "$arg scan starting." @@ -935,9 +974,10 @@ if {[lindex $argv 0] eq "-singledir"} { runplugin "post${arg}scan" processed_files log "$arg scan completed in [elapsed $st] seconds." } + $settings autolast [clock seconds] } if {!$prelocked} { release_lock webif_auto } -log "Media scan completed in [elapsed $scanstart] seconds." +log "Auto processing completed in [elapsed $scanstart] seconds." diff --git a/webif/lib/escape b/webif/lib/escape deleted file mode 100644 index 76d3f19..0000000 --- a/webif/lib/escape +++ /dev/null @@ -1,14 +0,0 @@ - -if {![exists -proc jescape]} { - proc jescape {str} { - return [string map { - "\"" "\\\"" - "\n" " " - } $str] - #regsub -all -- {"} "$str" "\\\"" str - #return $str - } - - alias escape jescape -} - diff --git a/webif/lib/json b/webif/lib/json index 42c028d..984e912 100644 --- a/webif/lib/json +++ b/webif/lib/json @@ -1,22 +1,28 @@ -# Initialise a map from control characters to JSON escaped characters. -# Initially all non-null control characters to \u00xx sequences. -for {set i 1} {$i < 32} {incr i} { - set ::json::escape_map([format %c $i]) \\u[format %04x $i] -} - -# Then overwrite certain well known control characters with shorter versions. -set ::json::escape_map([format %c 8]) \\b; # backspace -set ::json::escape_map([format %c 9]) \\t; # tab -set ::json::escape_map([format %c 10]) \\n; # lf -set ::json::escape_map([format %c 12]) \\f; # ff -set ::json::escape_map([format %c 13]) \\r; # cr -# Other special sequences -set ::json::escape_map(\") {\"} -set ::json::escape_map(\\) {\\} -set ::json::escape_map(/) {\/} - -proc ::json::escape {in} { - return [string map $::json::escape_map $in] +if {![exists -proc ::json::escape]} { + + # Initialise a map from control characters to JSON escaped characters. + # Initially all non-null control characters to \u00xx sequences. + for {set i 1} {$i < 32} {incr i} { + set ::json::escape_map([format %c $i]) \\u[format %04x $i] + } + + # Then overwrite certain well known control characters with shorter + # versions. + set ::json::escape_map([format %c 8]) \\b; # backspace + set ::json::escape_map([format %c 9]) \\t; # tab + set ::json::escape_map([format %c 10]) \\n; # lf + set ::json::escape_map([format %c 12]) \\f; # ff + set ::json::escape_map([format %c 13]) \\r; # cr + # Other special sequences + set ::json::escape_map(\") {\"} + set ::json::escape_map(\\) {\\} + set ::json::escape_map(/) {\/} + + proc ::json::escape {in} { + return [string map $::json::escape_map $in] + } + + alias jescape ::json::escape } diff --git a/webif/lib/queue.class b/webif/lib/queue.class index 91ee3e1..c9d16d6 100644 --- a/webif/lib/queue.class +++ b/webif/lib/queue.class @@ -198,3 +198,17 @@ proc {queue pop} {} { return {} } +proc {queue size} {} { + set db [queue dbhandle] + + set num 0 + set ret [$db query { + select count(*) from queue + where status in ('PENDING', 'INTERRUPTED') + }] + if {[llength $ret]} { + lassign [lindex $ret 0] x num + } + return $num +} + diff --git a/webif/lib/settings.class b/webif/lib/settings.class index f2aec8f..26908f7 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -41,6 +41,8 @@ class settings { autorecperiod 10 autokeep 7 noautohours "" + autofreq 20 + autolast 0 changechangenc 0 audiomp3 0 logsize 1048576 @@ -247,6 +249,14 @@ settings method autokeep {{val -1}} { return [$self _nval_setting autokeep $val] } +settings method autofreq {{val -1}} { + return [$self _nval_setting autofreq $val] +} + +settings method autolast {{val -1}} { + return [$self _nval_setting autolast $val] +} + settings method noautohours {{val -1}} { set val [$self _tval_setting noautohours $val] return $val