Forum OpenACS Q&A: When using cronjob - missing ad_conn information

I'm trying to run some tcl code (that calls dotlrn-procs and  thus ad_conn) from the cronjob-package but gives the errorcode listed below. More to the point - the script is trying to create subjects/classes during a nightly run.

Looks like the connection is not available when the code is executed by cronjob - anybody know how to avoid this?

[12/Jun/2003:13:03:51][18685.12497930][-sched:49-] Error: no current connection
no current connection
    while executing
"ns_conn $var"
    ("-get" arm line 17)
    invoked from within
"switch -- $flag {
    -connected_p {
      return [info exists ad_conn(request)]
    }

    -set {
      set ad_conn($var) [lindex $args 2]
    }

  ..."
    (procedure "ad_conn" line 12)
    invoked from within
"ad_conn user_id"
    invoked from within
"set user_id [ad_conn user_id]"

Collapse
Posted by Tilmann Singer on
I have been running into the same problem when writing migration scripts that call dotlrn operations from a detached thread. These operations rely on ad_conn for things like the user_id and ip address of the user that creates the class. The solution is quite easy - just set these values manually before calling the dotlrn procs like this:

ad_conn -set user_id $my_automated_user_id
...

There were some places that called ns_conn directly instead of ad_conn, especially in file-storage or the dotlrn parts of it. This defeats that strategy, but they can easily be replaced with ad_conn - I'm not sure if I committed all the replacements I did to cvs.

Collapse
Posted by Tom Jackson on

Just to note, as the author or cronjob: this is not an issue with cronjob, but with library code of another package which assumes you are in a connection thread.

I ran into the same issue with the Calendar package. During the last 'upgrade' to that package a bunch of library code was written that assumed a connection thread. This made using the code from another app impossible.

My opinion: library code should avoid this assumption. You could also run into the same problem with the package instance id for subsite aware applications. In making a package subsite aware, you could inadvertently make the package useless as a service to other packages. (Of course Calendar also had this problem, I seem to remember.)

If possible it would be nice to first design the data model of a package as a service, and apply the UI as an interface to the data model, then problems of how to get the user_id and package_id will already be solved.

Collapse
Posted by Tilmann Singer on
I guess this boils down to enable the user of the tcl api to pass in package_id, user_id etc. as parameters and defaulting to the values from ad_conn, right?
Collapse
Posted by Tom Jackson on

Maybe, but that assumes that ad_conn can figure out what the default should be. I'm suggesting that a package should be designed to work as a service, meaning that something other than a web ui might create content. Shouldn't the calendar package, for instance, allow an application to use the library/tcl api to create cal_items, and query them later, without the need to be mounted somewhere? Maybe we need a generalization of the package instance for unmounted use of a package dm/api.

Collapse
Posted by Cato Kolås on
Thanks for the replies, I guess I will try to set values for ad_conn then so the code thinks its connected.