The idea of this bit (ll. 149+ of the new code) is that we run a list of queries instead of running each separately. This means that the whole lot can be protected by a transaction and either succeed or fail together.
Let's work it through. At ll.132+ in the class function
proc {queue dbqueryl} {query_list {txn_mode ""}}
we run each query
Code:
foreach q $query_list {
$db query {*}$q
}
So
query_list
is a list of items, each of which is a list of arguments to the Jim SQLite
query
database method, which can bind parameters like so:
Code:
# Eg: '$db' is a handle to an open DB with a table 'tab' having an integer column 'col'
$db query { select * from tab where col > %s } 12
# will run
# select * from tab where col > 12;
In the problem code, there are 3 constant queries followed by one that has to be computed each time. The problem, I believe, is that
[command substitution]
doesn't happen inside {}, so the computed query isn't being constructed properly.
The list and its 4th item have to be constructed instead of being written out, something like this:
Code:
return [queue dbqueryl [list { {
update queue
set status = 'INTERRUPTED',
log = 'Job will be retried automatically.',
retries = retries + 1,
interrupts = interrupts + 1
where status = 'RUNNING'
} } { {
update queue
set status = 'FAILED',
log = 'Too many interrupts.'
where status = 'INTERRUPTED'
and interrupts >= 5
} } { {
update queue
set status = 'PENDING'
where status = 'DEFER'
} } [list {
delete from queue
where status in ('COMPLETE', 'FAILED')
and submitted < %s
} [expr [clock seconds] - 86400 * $days]
] ] ]
An alternative is to use
subst
which forces an additional evaluation:
Code:
return [queue dbqueryl [subst { { {
update queue
set status = 'INTERRUPTED',
log = 'Job will be retried automatically.',
retries = retries + 1,
interrupts = interrupts + 1
where status = 'RUNNING'
} } { {
update queue
set status = 'FAILED',
log = 'Too many interrupts.'
where status = 'INTERRUPTED'
and interrupts >= 5
} } { {
update queue
set status = 'PENDING'
where status = 'DEFER'
} } { {
delete from queue
where status in ('COMPLETE', 'FAILED')
and submitted < %s
} [expr [clock seconds] - 86400 * $days]
} } ] ]
This approach would cause problems if the list being processed contained $ or [ outside the command that was intended to be evaluated, but it doesn't (yet).