cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Fwd: Win98 and PHP reentrancy OR Curl is your friend

From: Csaba Gabor <keringo_at_hotmail.com>
Date: Mon, 21 May 2001 18:48:00 -0400

Here is a report on my recent efforts.
Someone may actually find it useful.
There's a lot of Windows specific general PHP stuff below.

     So I have a nice working PHP program on my Win 98 system,
let's call it Parser.php. Parser.php is invoked from the
web (I'm running Apache, PHP, and Curl).
Parser's task is to take requests from the user (which will
have execution starting times somehow associated with them),
and report back that the request has been processed.
The execution starting times could be up to several days
or weeks out.
     At the specified time, a certain action should be
performed by Munger.php (slightly more specifically,
I will use Munger.php (with Curl) to go out to the web,
grab some information, and react to it based on
directives in the initial request). (e.g. stock trading).
While Munger may take several minutes to run, it is
critical to start at a specific time.

     So the way I have this set up, once every 8 hours, a
Checker.php program is run by the windows Task Scheduler,
and all Checker.php does is to see which tasks are supposed to
run within the next 8.5 hours that have not yet been
scheduled. It schedules these by making use of the
DOS interface Winscheduler.exe program (you can find it
at CNET.com - I am really happy with this program - source
code included).
     Actually, php has a severe exec/backtick/passthrough
crashing bug on Windows systems such as Win98 (due to be
fixed in PHP 4.06) and you can't use these functions.
So the way that I invoke Winscheduler is to ask Excel
to do it for me:
    if (!($Excel = new COM("excel.application"))) die ("Can't load Excel");
    if (!($wb = $Excel->workbooks->Open("c:\\winapps\\Excel\\CodeBk.xls",
False, False))) die ("Can't load code book");
    $execStr = 'c:\\winapps\\schedule\\winscheduler\\winscheduler ' .
$TaskName . ' c:\\winapps\\php405\\php ' . '"-q Munger.php ' . $item . '" '
.
        'c:\\winapps\\apache\\Home\\WorkDir ' . strftime("%d%m%Y", $runTime)
. ' ' . strftime("%H%M", $runTime) . ' 0';
    $Excel->Run ("ExecMe", $execStr);
    if ($Excel) {
        $count = $Excel->Workbooks->Count;
        for ($i=$count;$i>0;$i--) {
            $wb = $Excel->Workbooks($i);
            $wb->close(False); }
        $Excel->quit();
    }

Note that the line to be Exec-ed can be quite long.
The (VBA) code in the Module within the Excel workbook is as follows:

Function ExecMe(ExecCmd)
     On Error GoTo ExecErr
     Call Shell(ExecCmd, 2)
     ExecMe = "OK"
     Exit Function
ExecErr:
     ExecMe = "Problem"
End Function

This Shell command is supposed to be asychronous, and it should
clean up after itself (which it does in this case). It also
handles the obnoxiously long line that is passed to it.

Well, all this has been the background to and introduction to the
problem, and this is where it starts to get interesting. All of this is
well and good, but in the case where time is short (i.e., Munger.php needs
to run in less than 8.5 hours), I don't have the luxury of waiting for
Checker.php to come along. But that is no, problem, because I can just
schedule Munger.php directly from Parser.php.
    But if time is really, really short (i.e. Munger.php needs to be run in
less than 2 minutes) then I can't even schedule it since the scheduler only
schedules on the minute. So I am obliged to invoke Munger.php directly,
somehow. I can't simply include it since then I would have to wait for it
to finish within Parser.php
    So if we try the technique I just outlined, where I try to Shell out
from Excel with a "c:\\winapps\\php405\\php -q
c:\\winapps\\apache\\Home\\WorkDir\\Parser.php $parserArg" then Parser.php
is not even reached, the Shell sticks around with an open DOS window - not a
very happy state of affairs. It's clear that php.exe can operate on
multiple .php files (heck, it's servicing zillions of requests as the
workhorse of the web server) but evidently recalling itself is a no-no.
Aditionally, I would add that if Munger.php is scheduled twice at the same
time (with different arguments), it is quite happy running concurrently as
evidenced by the fact that the log file entries alternate between the two
invoked versions.
    So my idea is the following. I will make an asynchronous process whose
task is to invoke CURL (Hurrah) on Munger.php. As far as PHP and Apache are
concerned, it is a bone fide request coming in, and they will happily take
care of running Munger.php, which is not bothering to produce a nice web
page since no one will read it (However, it does produce some print
statement and dump into a log file so the operator at the console can see it
working).
    Unfortunately, for reasons that Excel is not sharing, I can't seem to
invoke Curl.exe directly from a Shell command or even from a .BAT file. In
both cases, I get the dreaded hanging DOS window. Awful.

    But there is an Excel command to save the day. I regress back to the
old, arcane Excel Marcro language programming and come up with the following
Excel VBA code:

Function ExecMe4(ExecCmd)
    On Error GoTo ExecErr4
    Call Application.ExecuteExcel4Macro("EXEC(""" & ExecCmd & """,1)")
    ExecMe4 = "OK"
    Exit Function
ExecErr4:
    ExecMe4 = "Problem"
End Function

which I utilize in the following fashion from Parser.php:
    $execStr = "c:\\winapps\\curl\\curl.exe -K
c:\\winapps\\apache\\Home\\WorkDir\\MungArgs.crl";
    $Excel->Run ("ExecMe4", $execStr);

And after all of this, it works quite nicely. If the client comes in to
Parser.php with a last minute request, Munger.php is immediately started via
the above lines. While Munger.php runs, Parser.php finishes up, closes down
Excel, and reports back to the user. Meanwhile Munger.php can take its time
to finish up. In summary, I really like the idea of using CURL as a means
to asynchronously execute a php program. Hurrah, hurrah!

    With many regards and thanks to its creator(s),
        Csaba Gabor
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

_______________________________________________
Curl-and-php mailing list
Curl-and-php_at_lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/curl-and-php
Received on 2001-05-22