设置和使用 php-resque 的正确方法是什么? [英] What is the proper way to setup and use php-resque?

查看:35
本文介绍了设置和使用 php-resque 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 php-resque 在我的服务器上排队和执行 ffmpeg 转换.我大致了解它应该如何工作,但我在细节方面遇到了一些问题,找不到任何教程.具体来说,我不明白我应该把我的工作课程放在哪里,以及如何给我的工人上课并开始我的工人.读我只说让你的申请正在进行中还包括告诉工人你的工作类别,通过自动加载器或包括它们."

I am trying to use php-resque to queue and execute ffmpeg conversions on my server. I understand broadly how it should work, but I am having some trouble with the details and can not find any tutorials. Specifically, I don't understand where I should place my job classes, and how to give the classes to my workers and start my workers. The read me only says "Getting your application underway also includes telling the worker your job classes, by means of either an autoloader or including them."

希望有人能概述使用 php-resque 的整体结构.

Hopefully someone can outline the overall structure of using php-resque.

推荐答案

您可以将作业类放在您想要的位置.这取决于您的应用程序结构.

You can put your job classes where you want. It'll depend on your application structure.

例如,假设类 VideoConversion,用于 ffmpeg 转换.

For example, let's suppose the class VideoConversion, used for the ffmpeg conversion.

class VideoConversion {

    public function perform() {
        // The code for video conversion here
    }

}

在您的主应用程序中,在使用 php-resque 之前,假设您有类似的东西

In your main application, before using php-resque, let's say you have something like that

public function uploadVideo() {
    // Upload and move the video to a temp folder
    // Convert the video
}

并且您想将转换视频"部分加入队列.让我们将它排入 convert 队列:

And you want to enqueue the 'convert video' part. Let's just queue it to the convert queue:

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'));
}

在排队作业时,我们将源视频和目标视频的路径传递给 VideoConversion 类.您可以传递其他参数,这取决于您的 VideoConversion 类的编写方式.

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.

然后工作人员将轮询 convert 队列,并执行 VideoConversion 作业.worker 要做的是实例化 VideoConversion 类,并执行 perform() 方法.

A worker will then poll the convert queue, and execute the VideoConversion job. What the worker will do is to instantiate the VideoConversion class, and execute the perform() method.

作业参数 (array('origin-video.avi', 'destination-video.avi')),使用 Resque::enqueue 排队作业时的第三个参数code>,将通过 $this->argsperform() 方法中可用.

The job arguments (array('origine-video.avi', 'destination-video.avi')), third argument when queueing the job with Resque::enqueue, will be available inside the perform() method via $this->args.

# VideoConversion.php
class VideoConversion
{
    public function perform() {
    // $this->args == array('origine-video.avi', 'destination-video.avi');
    // Convert the video
}

查找您的工作类别

VideoConversion 类可以放在任何地方,但您必须告诉您的工作人员在哪里可以找到它.有多种方法可以做到这一点

Find your job classes

The VideoConversion class can be put anywhere, but you have to tell your workers where to find it. There's multiple ways to do that

在您的 .htaccess 或 apache 配置中,将包含所有作业类的目录添加到包含路径.您的员工会自动找到他们.

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.

此方法的主要问题是您的所有作业类必须位于同一个文件夹中,并且您的所有作业类都可以在任何地方使用.

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.

启动工作程序时,使用 APP_INCLUDE 参数指向作业类自动加载器".

When starting the worker, use the APP_INCLUDE argument to point to the job classes 'autoloader'.

APP_INCLUDE=/path/to/autoloader.php QUEUE=convert php resque.php

上面的命令将启动一个新的worker,轮询名为convert的队列.我们还将文件 /path/to/autoloader.php 传递给 worker.(看这里学习开始一个工人)

The above command will start a new worker, polling the queue named convert. We're also passing the file /path/to/autoloader.php to the worker. (see here to learn to start a worker)

从技术上讲,worker 将使用 include '/path/to/autoloader.php'; 包含该文件.

Technically, the worker will include that file with include '/path/to/autoloader.php';.

然后您可以告诉工人如何找到您的工作类别:

You can then tell the workers how to find your job classes:

使用基本的包含

在/path/to/autoloader.php"中:

In the '/path/to/autoloader.php':

include /path/to/VideoConversion.php
include /path/to/anotherClass.php
...

使用自动加载器

使用 php 自动加载器加载您的作业类.

Use php autoloader to load your job classes.

使用set_include_path()

set_include_path('path/to/job');

这样,您的工作就位于 include_path 中,仅供该工作人员使用.

That way, your jobs are in the include_path just for this worker.

APP_INCLUDE 绑定到您正在启动的工作程序.如果您正在启动另一个工作程序,请再次使用 APP_INCLUDE.您可以为每个工作人员使用不同的文件.

APP_INCLUDE is binded to the worker you're starting. If you're starting another worker, use APP_INCLUDE again. You can use a different file for each worker.

您还可以设计您的作业类来执行多个作业.有一个教程解释如何做到这一点.它涵盖了从队列系统的基础到如何使用和实现它.

You can also design your job classes to execute more than one job. There's a tutorial explaining how to do that. It covers from the basic of a queue system to how to use and implement it.

如果还不够,请查看 resque 文档.php-resque API 完全一样.唯一的区别是 Resque 作业类是用 Ruby 编写的,而 php-resque 的作业类是用 php 编写的.

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.

这篇关于设置和使用 php-resque 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆