Index: openacs-4/packages/cronjob/tcl/cronjob-init.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cronjob/tcl/cronjob-init.tcl,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/cronjob/tcl/cronjob-init.tcl 4 Nov 2001 18:07:54 -0000 1.1 @@ -0,0 +1,190 @@ +ad_library { + + Initializes cronjob package + + @creation-date 22 Sept 2001 + @author Tom Jackson + @cvs-id $Id: cronjob-init.tcl,v 1.1 2001/11/04 18:07:54 tomj Exp $ +} + + +ns_schedule_proc -thread 60 cronjob_check + +if {![info exists qd_write_query_select]} { + +ad_proc qd_write_query_select {package attrs} { + +

Returns Postgresql function with: +

+ +} { + ns_log Debug "Running qd_write_query_select with $package $attrs" + set query [list] + set in_args [list] + set args [concat $attrs] + foreach {attr sign value} $args { + lappend in_args $attr + set attr_array($attr) $value + } + set attrs_and_defaults [eval qd_choose_function $package $in_args] + foreach {attr default_value} $attrs_and_defaults { + # see if attr was passed in with value + if {![info exists attr_array($attr)]} { + + if {[string match "" $default_value]} { + ns_log Debug "Attempt to call $package with no value for $attr" + return -code error + } + lappend query $default_value + } else { + lappend query "$attr_array($attr)" + } + } + ns_log Debug "${package}([join $query ",\n"]);" + return "${package}([join $query ",\n"]);" +} + + +ad_proc qd_add_package {package args} { +

Adds a package with all the attributes for every function of the same name. +

For example, if you have two functions that could be called + foo(a,b,c) and foo(a,c,d), + the package would be registered using: +

+
+         qd_add_package foo a b c d
+       
+
+ +} { + nsv_set qd_pg_packages $package $args + set n 1 + foreach attr $args { + nsv_set $package $attr $n + set n [expr $n * 2] + } +} + +ad_proc qd_total_attributes {package attributes} { + +

Totals the attribute values. +} { + + upvar $package package_array + upvar $attributes attrs + set total 0 + foreach attribute $attrs { + incr total $package_array($attribute) + ns_log Debug "Adding $package_array($attribute) $total" + } + return $total +} + +ad_proc qd_add_function {package args} { + +

Adds a single function, including defaults to a package. +

If one foo function has three attributes: a, b and c, where + a is required and b can be null and c default to 't', + the function would be + registered using: +

+
+       qd_add_function foo a "" b "null" c "'t'"
+    
+
+ +} { + + + # total up the function value. + array set temp_package [nsv_array get $package] + set i 1 + foreach {attr default} $args { + lappend attr_list $attr + } + set total [qd_total_attributes temp_package attr_list] + nsv_set ${package}_functions $total $args + +} + +ad_proc qd_choose_function {package args} { + +

Used to choose a function based on passed in attributes. + +} { + + array set temp_package [nsv_array get $package] + set total [qd_total_attributes temp_package args] + if {[nsv_exists ${package}_functions $total]} { + ns_log Debug "Found matching sig: '$total'" + return [nsv_get ${package}_functions $total] + } + set functions [nsv_array names ${package}_functions] + foreach sig $functions { + ns_log Debug "checking sig '$total' against '$sig'" + if {$total == [expr $sig & $total]} { + ns_log Debug "Found match '$total' in '$sig'" + return [nsv_get ${package}_functions $sig] + } + } + return "!NO MATCH: $total not in $functions" +} + +proc qd_write_query {package args} { + + set query [list] + set attrs_and_defaults [eval qd_choose_function $package $args] + foreach {attr default_value} $attrs_and_defaults { + if {[lsearch $args $attr] < 0 } { + if {[string match "" $default_value]} { + ns_log Debug "Attempt to call $package with no value for $attr" + return -code error + } + lappend query $default_value + } else { + lappend query ":${attr}" + } + } + return [join $query ",\n"] +} + +proc qd_write_query_upvar {package listvar} { + + set query [list] + upvar $listvar args + set attrs_and_defaults [eval qd_choose_function $package $args] + if {[string match "!NO MATCH:*" $attrs_and_defaults ]} { + ns_log Debug "$attrs_and_defaults" + return -code error + } + foreach {attr default_value} $attrs_and_defaults { + if {[lsearch $args $attr] < 0 } { + if {[string match "" $default_value]} { + ns_log Debug "Attempt to call $package with no value for $attr" + return -code error + } + lappend query $default_value + } else { + lappend query ":${attr}" + } + } + return [join $query ",\n"] +} + + + + +} + +qd_add_package cronjob__new cronjob_id user_id description approved_p disabled_p minute hr mon day dayofweek run_sql run_tcl email creation_user creation_ip context_id +qd_add_function cronjob__new "cronjob_id" "null" "user_id" "" "description" "" "approved_p" "'f'" "disabled_p" "'f'" "minute" "'0'" "hr" "'0'" "mon" "'0'" "day" "'0'" "dayofweek" "'0'" "run_sql" "" "run_tcl" "" "email" "" "creation_user" "null" "creation_ip" "null" "context_id" "null" + + +qd_add_package cronjob__set_attrs "cronjob_id" "user_id" "description" "approved_p" "disabled_p" "minute" "hr" "mon" "day" "dayofweek" "run_sql" "run_tcl" "email" +qd_add_function cronjob__set_attrs "cronjob_id" "" "user_id" "null" "description" "null" "approved_p" "null" "disabled_p" "'f'" "minute" "null" "hr" "null" "mon" "null" "day" "null" "dayofweek" "null" "run_sql" "null" "run_tcl" "null" "email" "null" + +qd_add_package cronjob__delete cronjob_id +qd_add_function cronjob__delete cronjob_id "" Index: openacs-4/packages/cronjob/tcl/cronjob-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cronjob/tcl/cronjob-procs.tcl,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/cronjob/tcl/cronjob-procs.tcl 4 Nov 2001 18:07:54 -0000 1.1 @@ -0,0 +1,96 @@ +# Procs for cronjog package + +ad_proc cronjob_check { } { + + Checks the database for cronjobs that need to run +} { + + # setup the vars + set time [ns_time] + set minute [ns_fmttime $time %M] + set hr [ns_fmttime $time %H] + set mon [ns_fmttime $time %m] + set day [ns_fmttime $time %d] + set dayofweek [ns_fmttime $time %w] + + set sql " +select + cronjob_id +from + cronjobs +where + disabled_p = 'f' AND + approved_p = 't' AND + ((minute = :minute) OR (minute = '*')) AND + ((hr = :hr ) OR (hr = '*')) AND + ((mon = :mon ) OR (mon = '*')) AND + ((day = :day ) OR (day = '*')) AND + ((dayofweek = :dayofweek ) OR (dayofweek = '*'))" + + db_foreach cronjob_sched_foreach $sql { + + ns_schedule_proc -once -thread 1 cronjob_run $cronjob_id + } + +} + + +ad_proc cronjob_run { cronjob_id } { + + Proc to run cronjobs + +} { + set table "No SQL" + set sql " +select + description, + run_sql, + run_tcl, + email +from + cronjobs +where + cronjob_id = :cronjob_id" + ns_log Notice "Cronjob_id is $cronjob_id" + db_1row "crontab_query" $sql + db_release_unused_handles + if {![string match "" $run_sql]} { + set table "" + set db [ns_db gethandle "log"] + set sel [ns_db select $db $run_sql] + set rownum 0 + if {![string match "" $sel]} { + while {[ns_db getrow $db $sel]} { + set size [ns_set size $sel] + if {$rownum == 0} { + for {set i 0 } {$i < $size} {incr i} { + append table "\n" + } + } + append table "" + for {set i 0 } {$i < $size} {incr i} { + append table "\n" + } + append table "" + incr rownum + } + } + append table "
[ns_set key $sel $i]
[ns_set value $sel $i]
" + if {$rownum == 0} { + set table "No Rows Returned" + } + ns_db releasehandle $db + } + + # evaluate the run_tcl code + eval $run_tcl + + if {![string match "" $email]} { + ns_log notice "sending cronjob email to $email" + set headers [ns_set create] + ns_set put $headers "Content-Type" "text/html" + ns_sendmail $email "negeen@zmbh.com" "Cronjob $cronjob_id" "Description:
$description
$table" $headers + } + return + +}