Scheduled Agents Equivalent in PHP
So, a day without Notes. In fact I spent close to a week working on a PHP site, but launched Notes for various reasons on the other days.
Although I said I didn't miss Notes, there were certain aspects of it that I did. For one there was the concept of a Scheduled Agent.
The accepted method of having a PHP-based task run on a schedule seems to be that you use a "cron" job. You can read all about how you do this here. Basically you add a line like this to your cron file:
30 2 * * * /usr/bin/curl -u jake:password http://www.site.com/nightly.php
What this does is tell the server that, at 2:30am each day it should load the "curl" program and tells it to load the webpage found at the URL specified. The user name/password are passed as it's a good idea, I thought, to lock access to this file.
Immediately it seems odd. Rather than PHP being able to manage scheduled tasks you have to rely on the OS to run a program for you. What struck me about this was that it relies on internet access. I guess it's ok to assume a web server is always on and has access to the internet 24/7, but what if, for some reason, at 2:30am one night it can't see site.com? The "agent" fails to run!
You could of course just have curl load http://localhost/nightly.php, assuming Apache is set to respond to that host name. However, any reference in your script to the "SERVER_NAME" CGI variable is then lost and generated emails have broken links unless you're willing to "hard code" the domain name.
Another aspect to this is the lack of self-containment of the code. Whereas I can deliver a customer one single NSF (in most cases) and trust them to get it up and running, with PHP they'd have to pass it on to an internal techy with a little more know-how and access to the shell or knowledge of SSH.
In the case of the system I developer for my friend new client I am hosting the system for them and so have the rights to set up cron jobs etc. If this weren't the case I think I'd miss the arrangement I have currently with my Notes-based clients whereby my /ROCKALL id is cross-certified on their systems and I can pretty much make instant updates to most sites I've developed.
There were other aspects of Notes that would have made life simpler for me, but not having simple way to run code each night seemed the most obvious case in which Notes comes out on top.
Jake,
Isn't it the right way to do a stored procedure and put it on your database machine instead of typing it to php?
After all, your data should be manipulating data or send emails which most back ends do.
And it's reasons like these that, after working with them all, I still prefer notes. Let the detractors say what they want, but it sure does make managing server-side stuff _easy_.
It's been a long time since I commented as I've been out of Notes development for a long time.
Now to the point in hand - cron.
You can use anacron which never assumes that the computer is running 24/7 and will execute scripts the next time it is able to do so. (If you happen to have a Darwin host, launchd should also be available for your use)
You can however invoke a php script as a shell executable, with this in mind, you can write a php script to have a while(1) loop which performs tasks only at set times and sleeps the rest of the time. That can be started by a php management script (from the web if you want) to start and stop it.
Although not simple, you should be able to knock something up fairly quickly and maintain the "bundled" feel to your code. (If it's too difficult a quick Google does show "http://www.phpjobscheduler.co.uk/" - no idea how well it works as I've never used it - I wrote my own)
veer. So, MySQL can do anything the PHP script can do? I keep meaning to look in to stored procedures. Will do now.
Thanks for the advice Gareth. I'll remember anacron if there's a next time, which I hope there will be. By the way, glad you (and other now non-Dominoers) are still reading.
Good point, Jake. Scheduled agents is also one of the things I miss outside Domino, even in .NET you have the same issue as with PHP (although it's not a cron but a service or scheduled task at OS level).
Over a year ago, I wrote an article that covers a lot of aspects that you would miss outside Domino:
http://ferdychristant.com/blog/archive/DOMM-6YGCFQ
Although it's a bit old, it may be a nice way to remember that Domino isn't that bad after all.
Why not running php-cli instead of relying on Apache ?
Your PHP "agent" should be located outside the web server's root, for obvious security reasons (and therefore, no need for credentials).
Finally, you may use "anacron" instead of "cron" to resolve the downtime issues. It comes with most of the distributions.
Jake,
Let me first say that I don't know MySQL. :-)
I'm sure MySQL would not do everything PHP does, however the idea was that if it allows you to schedule jobs you could make URL requests to php.
In essence, MySQL will be your job scheduler which could run stored procs or external calls.
Glad to see you on this topic Jake, as I've been working to learn the basics myself as of late.
Roughly, when you schedule a Notes Agent in a database on the server, a request is made to the scheduler which then schedules the agent to run.
It would seem like building a package that gave a similar functionality via PHP wouldn't be terribly difficult - I would assume phpjobscheduler does something like this. I'd probably start by creating my own admin mysql database with the parts I'd want for keeping track of agents and other tasks that come up... such as logging (or better to have a log database like Notes has separate log and admin request dbs). I think the pattern used by Domino is terrific - and the fact that it's prepackaged for us makes it an easy way to go. If you have to do the same in PHP, it seems like the ideal model to emulate. Effectively, cron would take the role of your domino server task scheduler - all logic would be controlled by your database using sh or php to access it.
Btw, jake, if you're learning php entangled with cron, learn shell scripting as well. Many wonderful things there. Wrox has a good "Beginning Shell Scripting" book.