Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can put your job classes where you want. It'll depend on your application structure.</p> <h1>How to create a job class</h1> <p>For example, let's suppose the class <code>VideoConversion</code>, used for the ffmpeg conversion.</p> <pre><code>class VideoConversion { public function perform() { // The code for video conversion here } } </code></pre> <p>In your main application, before using php-resque, let's say you have something like that</p> <pre><code>public function uploadVideo() { // Upload and move the video to a temp folder // Convert the video } </code></pre> <p>And you want to enqueue the 'convert video' part. Let's just queue it to the <code>convert</code> queue:</p> <pre><code>public function uploadVideo() { // Upload and move the video to a temp folder // Let's suppose you need to convert a 'source video' to a 'destination video' Resque::enqueue('convert', 'VideoConversion', array('origine-video.avi', 'destination-video.avi')); } </code></pre> <p>When queuing the job, we passed the path to the source and destination video to the VideoConversion class. You can pass other argument, it'll depend on how your VideoConversion class is written.</p> <p>A worker will then poll the <code>convert</code> queue, and execute the <code>VideoConversion</code> job. What the worker will do is to instantiate the VideoConversion class, and execute the <code>perform()</code> method. </p> <p>The job arguments (<code>array('origine-video.avi', 'destination-video.avi')</code>), third argument when queueing the job with <code>Resque::enqueue</code>, will be available inside the <code>perform()</code> method via <code>$this-&gt;args</code>.</p> <pre><code># VideoConversion.php class VideoConversion { public function perform() { // $this-&gt;args == array('origine-video.avi', 'destination-video.avi'); // Convert the video } </code></pre> <h2>Find your job classes</h2> <p>The <code>VideoConversion</code> class can be put anywhere, but you have to tell your workers where to find it. There's multiple ways to do that</p> <h3>Put you jobs classes in the include_path</h3> <p>In your .htaccess or the apache config, add the directory containing all your job classes to the include path. Your workers will automatically find them. </p> <p>Main issue with this method is that all your jobs classes must be in the same folder, and that all your job classes are available everywhere.</p> <h3>Tell each worker where to find your job classes</h3> <p>When starting the worker, use the <code>APP_INCLUDE</code> argument to point to the job classes 'autoloader'.</p> <pre><code>APP_INCLUDE=/path/to/autoloader.php QUEUE=convert php resque.php </code></pre> <p>The above command will start a new worker, polling the queue named <code>convert</code>. We're also passing the file <em>/path/to/autoloader.php</em> to the worker. (<a href="http://www.kamisama.me/2012/10/12/background-jobs-with-php-and-resque-part-4-managing-worker/">see here to learn to start a worker</a>)</p> <p>Technically, the worker will include that file with <code>include '/path/to/autoloader.php';</code>.</p> <p>You can then tell the workers how to find your job classes:</p> <p><strong>Use basic include</strong></p> <p>In the '/path/to/autoloader.php':</p> <pre><code>include /path/to/VideoConversion.php include /path/to/anotherClass.php ... </code></pre> <p><strong>Use an autoloader</strong></p> <p>Use php autoloader to load your job classes.</p> <p><strong>Use <code>set_include_path()</code></strong></p> <pre><code>set_include_path('path/to/job'); </code></pre> <p>That way, your jobs are in the <code>include_path</code> just for this worker.</p> <h2>Closing thought</h2> <p><code>APP_INCLUDE</code> is binded to the worker you're starting. If you're starting another worker, use <code>APP_INCLUDE</code> again. You can use a different file for each worker.</p> <p>You can also design your job classes to execute more than one job. There's a <a href="http://www.kamisama.me/2012/10/13/background-jobs-with-php-and-resque-part-5-creating-jobs/">tutorial</a> explaining how to do that. It covers from the basic of a queue system to how to use and implement it.</p> <p>If it's still not enough, take a look at resque documentation. php-resque API is exactly the same. Only difference is that Resque job classes are written in Ruby, whereas php-resque's one are in php.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload