Sunday, February 27, 2011

Re: how to redirect and let the server do some lengthy job in the background?

On Feb 27, 2011, at 09:48, Tan Cheng wrote:
> On Feb 27, 10:13 am, Ryan Schmidt wrote:
>> On Feb 27, 2011, at 09:06, Tan Cheng wrote:
>>
>>> I have a question. I'm using cakephp to develop a video site, like
>>> youtube, I want the user to upload their video to the site, but I only
>>> want them to wait for the upload, after it is uploaded, I hope the
>>> converting process ( using ffmpeg ) run in the background and they
>>> will be redirected to a different page.
>>
>> You will want your video conversion to happen in a separate process that has nothing to do with the web server. PHP in a web server imposes execution time limits which are not appropriate for lengthy operations like video encoding.
>>
>> In your web page, after the file has been uploaded, you'll probably want to enter the new video into the database and indicate with a flag column that the video has not yet been encoded. Then you have some other process (a CakePHP shell, if you like) that runs on the server as a daemon. It would find all videos in the database that have not yet been encoded, encodes them, and then marks them in the database as having been encoded, then sleeps for awhile before checking again.
>
> WOW, thanks a lot, Ryan. That makes much more sense. I have never used
> a daemon before. I think I need to investigate into that more. Do you
> happen to know some keywords or article to take a look at? Sorry, I'm
> really a newbie to php...

"Daemon" just means "program that runs in the background". Typically daemons stick around more or less forever, and do things when something happens. For example, your web server is a daemon that's always running and does stuff (sends web pages over the network) when something happens (network connection comes in from a user requesting a web page). Cron is a daemon that does stuff (calls scripts you've configured it to call) when something happens (when the date / time you've configured arrives).

Your daemon would do stuff (encode videos) when something happens (when new unencoded videos appear in the database). A simple skeleton for such a thing might be:

<?php

initializeStuff();
while (true) {
$unencodedVideos = getUnencodedVideos();
if (!empty($unencodedVideos)) {
foreach ($unencodedVideos as $unencodedVideo) {
encodeVideo($unencodedVideo);
}
}
sleep(60);
}

?>

All you have to do is start this daemon on your server and it keeps running forever checking for new videos every 60 seconds and encoding them. This assumes you have the ability to start long-running scripts on your server, and aren't using some kind of limited shared hosting account. You'd probably also want to add a startup script to your server so that it starts this daemon on system startup, in the event that your server is ever restarted.

You'll probably want this daemon to be a CakePHP shell, so that you have access to your CakePHP models and config files; some rudimentary information about this is available here:

http://book.cakephp.org/view/1106/The-CakePHP-Console

Obviously the code I've written above is not in the CakePHP style, but that should be easy to change. getUnencodedVideos() probably becomes a method of your Video model; maybe encodeVideo() does too.


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: