[nycphp-talk] PHP and running background tasks...
inforequest
1j0lkq002 at sneakemail.com
Sat Feb 24 22:09:36 EST 2007
Brian Dailey support-at-dailytechnology.net |nyphp dev/internal group
use| wrote:
> From the last link: "Note that you should never attempt to use these
> process control forking functions when using a webserver; you should
> only fork applications when using the PHP command line client."
>
> I guess that kind of answers my question. exec() might actually be the
> only solution, I'm not sure - it just seems somewhat untidy. I would
> have to log any failures as I go along, I guess.
>
> As an explanation of what I'm doing...
>
> I have a cache. If an item in the cache is more than a week old, I
> want to re-fetch the item. I could schedule a job to check for all
> week-old objects and re-fetch them, but I think it would be more
> efficient to only run the re-fetch if someone loads the cached item
> and it's more than a week old (it's ok if web user gets the old item
> on that occurance). I don't want to make the user wait for fetching
> the cached item. I'd like to hand it off to another script that
> fetches it in the background, and doesn't tie up the user with the
> operation.
>
> I hope that clarifies what I'm trying to do. Perhaps exec() is my only
> option.
>
> - Brian
It sounds like a "dumb cache" or "lazy cache" I've done this many times,
many different ways.
One way is have a front controller either displays an existing file or
dispatches to a dynamic view script, based on date check. The dynamic
script replaces the "dumb cache" file version. Sort of typical cache
behavior.
Another way is to store "expired" token in the database, and have the
view controller tick it when a page request comes in and the file is
noted to be out of date. A CRON-based script regenerates the out of date
files based ona query of the expired bits. If the regeneration is fast
enough, I like to use a 404 controller to do the regeneration
automagically on page request.
In your case you've specified that your user is allowed to see an out of
date file, and it sounds like your regeneration takes a while, so I'd
use the database (or a flat file if you don't already maintain a db
connection.. the cron'd script can be thorough with cleanup tasks
because it really does hav all day to get the job done).
Hope that's helpful.
-=john andrews
Competitive Webmaster blogging at www.johnon.com
More information about the talk
mailing list