views:

388

answers:

4

Hello all, I am new here.

I recently installed my video script to a new server but I am seeing that it will start to convert 1 video (via mencoder) then before finishing it, it will try and convery another, and another, so it will be trying to convert 4+ videos at the same time causing the server to shut down. The script developer said:

"It converts each video in a PHP background process. There might be a way to limit the number of PHP background processes on your server and queue them."

So how is this done please?

Regards

A: 

Use some sort of lock. For example, use file locking on a directory so only one process at a time can use the resource.

This would require a wrapper script for the encoder which will wait for the lock to be released by the currently running encoder.

It should also be smart enough to detect when a lock wasn't freed if the encoder crashes, and release the lock.

Edit: My understanding of the problem was there were multiple invocations of the script, each calling mencoder. However, from the other response, it seems that it might be one invocation running all the processes in the background. Then I think the solution using Semaphores is better.

Edit: Looks like someone asked this question before:

best-way-to-obtain-a-lock-in-php

I like that answer, because it attacks the essential problem: there's no out-of-the-box solution in PHP, you have to brew your own thing for such kind of thing.
mark
@Juan: Regd your Edit, I believe even with multiple invocations of the script. A SHM semaphore will be able to do the job (like POSIX semaphores in Unix).
Mohit Nanda
+1  A: 

Use PHP Semaphores

You can use a shared counting Semaphore in PHP, and implement a queue with a cap on the no. of parallel executions. Semaphores are always the most recommended method for any form of concurrency control.

Using this you can easily configure and control the parallel executions of mencoder, and limit them as well.


Pseudocode

Begin
    init sem=MAX;

    wait(sem) //sem--, waits if sem=0, till atleast one process comes out of the critical section
    /*
     Critical Section
     where you execute mencoder
    */
    signal(sem) //sem++
End
Mohit Nanda
A: 

Hmmmm,

This place is great..I will wait for my partner to try and implement...I am not sure how it`s doing it to be honest, but hopefully one of the above solutions will do the tricK.. i WILL report back once done.

Regards

A: 

Hello, Please forgive me but I was given the file that needs to be altered. This is the coding. Is there anyway to do any of what you mentionned above to it?

Thank you in advance....

//===================================================== // Include Setup And Config
//===================================================== require_once("functions.php"); require_once($config['sitepath'].'/classes/videoencode.class.php');

//===================================================== // Passed Values
//===================================================== $filename = $argv[1]; $filename2 = $argv[2]; $id = $argv[3];

if (($filename != "") && ($filename2 != "") && ($id != "")) {
$filename_orig = $filename;

  //=====================================================
  //  Encode Video
  //=====================================================
  $encode = new VideoEncode();  
  $encode->smclass =& $smclass;
  $encode->background_convert = false;

  $encode->init($filename);

  if ( $encode->encode() ) 
  {
   //-----------------------------------------
   //  Delete source video
   //-----------------------------------------
   if ($config['delete_source_video'] == 1)
   {
       @unlink($config['sitepath']."/files/".$filename_orig);
    $filename_orig = $encode->filename_new;
   }

   $filename = $encode->filename_new;
   $filesize = $smclass->_get_file_size($config['sitepath'].'/files/'.$filename);
  }

  //=====================================================
  //  Media Slave Server
  //=====================================================
  if ($config['enable_slaves'])
  {
      $db->query("SELECT * FROM servers WHERE server_type = 'media' AND enabled = 1 ORDER BY RAND() LIMIT 1");
   $serv = $db->fetch_row();

   if ( $db->get_num_rows() )
   {
       $file_path = $config['sitepath']."/files/".$filename;
    $file_orig_path = $config['sitepath']."/files/".$filename_orig;

    require($config['sitepath'].'/classes/ftp.class.php');
    $ftp = new FTP($serv['ftp_username'], $serv['ftp_password'], $serv['ftp_host']);
    $ftp->smclass =& $smclass;

    if ( $ftp->connect() )
    {    
        //-----------------------------------------
     //  Move file to slave server
     //-----------------------------------------
     $ftp->put($serv['server_path']."/files/".$filename,  $file_path);

     //-----------------------------------------
     //  Delete original file
     //-----------------------------------------
     @unlink($file_path);
     if (file_exists($file_orig_path))
     {
      @unlink($file_orig_path);
     }

     $db->query("UPDATE files SET server_id = '".$serv['server_id']."' WHERE file_id = ".intval($id)."");
    }
   }
  }

  //=====================================================
  //  Update                
  //=====================================================
  sleep(10);

  $db->query("UPDATE files SET file_size = '".$filesize."', file_orig = '".$filename_orig."', file = '".$filename."' WHERE file_id = ".intval($id)."");

}

?>

Please format the code in a better way. The initials line seem to be out of the code block.
Mohit Nanda