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
This commit is contained in:
hummypkg 2016-12-21 12:58:25 +00:00
parent 88c7f8cef9
commit d0cb142952
15 changed files with 139 additions and 79 deletions

View File

@ -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)

View File

@ -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

View File

@ -2,7 +2,7 @@
package require cgi
source /mod/webif/lib/setup
require ts.class escape
require ts.class json
httpheader "application/json"

View File

@ -2,7 +2,7 @@
package require cgi
source /mod/webif/lib/setup
require escape
require json
httpheader "application/json"

View File

@ -3,7 +3,7 @@
package require cgi
package require pack
source /mod/webif/lib/setup
require escape
require json
httpheader "application/json"

View File

@ -2,7 +2,7 @@
package require cgi
source /mod/webif/lib/setup
require ts.class escape
require ts.class json
httpheader "application/json"

View File

@ -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\","
}
}

View File

@ -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]]\","

View File

@ -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

View File

@ -38,6 +38,9 @@ puts "
</tr>
"
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 "
<tr>
<form class=auto id=noautohours method=get action=$env(SCRIPT_NAME)>
@ -78,6 +77,10 @@ puts "
</tr>
"
setting_number autokeep \
"How many days should completed entries stay in the queue?" $autokeep \
1 365
puts {
</table></fieldset></div>

View File

@ -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."

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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