/usr/share/tcltk/tcllib1.18/cron/cron.tcl is in tcllib 1.18-dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | ###
# This file implements a process table
# Instead of having individual components try to maintain their own timers
# we centrally manage how often tasks should be kicked off here.
###
#
# Author: Sean Woods (for T&E Solutions)
::namespace eval ::cron {}
proc ::cron::at args {
switch [llength $args] {
2 {
variable processuid
set process event#[incr processuid]
lassign $args timecode command
}
3 {
lassign $args process timecode command
}
default {
error "Usage: ?process? timecode command"
}
}
variable processTable
if {[string is integer -strict $timecode]} {
set scheduled $timecode
} else {
set scheduled [clock scan $timecode]
}
set now [clock seconds]
set info [list process $process frequency 0 command $command scheduled $scheduled lastevent $now]
if ![info exists processTable($process)] {
lappend info lastrun 0 err 0 result {}
}
foreach {field value} $info {
dict set processTable($process) $field $value
}
::cron::wake
return $process
}
proc ::cron::in args {
switch [llength $args] {
2 {
variable processuid
set process event#[incr processuid]
lassign $args timecode command
}
3 {
lassign $args process timecode command
}
default {
error "Usage: ?process? timecode command"
}
}
variable processTable
set now [clock seconds]
set scheduled [expr {int(ceil($timecode+$now))}]
set info [list process $process frequency 0 command $command scheduled $scheduled lastevent $now]
if ![info exists processTable($process)] {
lappend info lastrun 0 err 0 result {}
}
foreach {field value} $info {
dict set processTable($process) $field $value
}
::cron::wake
return $process
}
###
# topic: 0776dccd7e84530fa6412e507c02487c
###
proc ::cron::every {process frequency command} {
variable processTable
set now [clock seconds]
set info [list process $process frequency $frequency command $command scheduled [expr {$now + $frequency}] lastevent $now]
if ![info exists processTable($process)] {
lappend info lastrun 0 err 0 result {}
}
foreach {field value} $info {
dict set processTable($process) $field $value
}
::cron::wake
}
proc ::cron::cancel {process} {
variable processTable
unset -nocomplain processTable($process)
}
###
# topic: 97015814408714af539f35856f85bce6
###
proc ::cron::run process {
variable processTable
dict set processTable($process) lastrun 0
}
proc ::cron::doOneEvent task {
variable lock 1
variable processTable
set now [clock seconds]
dict with processTable($task) {
set err [catch {uplevel #0 $command} result]
if $err {
puts $result
}
}
set lock 0
}
###
# topic: 1f8d4726623321acc311734c1dadcd8e
# description:
# Run through our process table and
# kick off overdue tasks
###
proc ::cron::runProcesses {} {
variable processTable
set now [clock seconds]
###
# Determine what tasks to run this timestep
###
set tasks {}
set cancellist {}
foreach {process} [array names processTable] {
dict with processTable($process) {
if { $scheduled <= $now } {
lappend tasks $process
if { $frequency <= 0 } {
lappend cancellist $process
} else {
set scheduled [expr {$frequency + $lastrun}]
if { $scheduled <= $now } {
set scheduled [expr {$frequency + $now}]
}
}
set lastrun $now
}
set lastevent $now
}
}
foreach task $tasks {
doOneEvent $task
}
foreach {task} $cancellist {
unset -nocomplain processTable($task)
}
}
###
# topic: 2f5a33d28948c4514764bd2f58b750fc
# description:
# Called once per second, and timed to ensure
# we run in roughly realtime
###
proc ::cron::runTasks {} {
variable lastcall
after cancel $lastcall
###
# Run the processes before we kick off another task...
###
catch {runProcesses}
variable processTable
###
# Look at our schedule and book the next timeslot
# or 15 minutes, whichever is sooner
###
set now [clock seconds]
set nexttime [expr {$now - ($now % 900) + 900}]
foreach {process} [array names processTable] {
dict with processTable($process) {
if {$scheduled > $now && $scheduled < $nexttime} {
set nexttime $scheduled
}
}
}
###
# Try to get the event to fire off on the border of the
# nearest second
###
if { $nexttime > $now } {
set ctime [clock milliseconds]
set next [expr {($nexttime-$now)*1000-1000+($ctime % 1000)}]
} else {
set next 0
}
set lastcall [after $next [namespace current]::runTasks]
}
###
# topic: 21de7bb8db019f3a2fd5a6ae9b38fd55
# description:
# Called once per second, and timed to ensure
# we run in roughly realtime
###
proc ::cron::runTasksCoro {} {
variable lastcall
after cancel $lastcall
###
# Do this forever
###
variable processTable
variable processing
while 1 {
set lastevent 0
set nextevent 0
set now [clock seconds]
###
# Determine what tasks to run this timestep
###
set tasks {}
set cancellist {}
foreach {process} [lsort -dictionary [array names processTable]] {
dict with processTable($process) {
if { $scheduled <= $now } {
lappend tasks $process
if { $frequency <= 0 } {
lappend cancellist $process
} else {
set scheduled [expr {$frequency + $lastrun}]
if { $scheduled <= $now } {
set scheduled [expr {$frequency + $now}]
}
}
set lastrun $now
} else {
if {$nextevent==0 || $scheduled < $nextevent} {
set $nextevent $scheduled
}
}
set lastevent $now
}
}
foreach task $tasks {
doOneEvent $task
yield 0
}
foreach {task} $cancellist {
unset -nocomplain processTable($task)
}
if {$nextevent==0} {
# Wake me up in 5 minutes, just out of principle
yield 300
} else {
yield $nextevent
}
}
}
proc ::cron::wake {} {
variable lock
##
# Only triggered by cron jobs kicking off other cron jobs within
# the script body
##
if {$lock} return
::cron::runTasks
}
###
# topic: 4a891d0caabc6e25fbec9514ea8104dd
# description:
# This file implements a process table
# Instead of having individual components try to maintain their own timers
# we centrally manage how often tasks should be kicked off here.
###
namespace eval ::cron {
variable lastcall 0
variable processTable
variable lock 0
}
::cron::wake
package provide cron 1.2.1
|