使用IronWorker在云中运行任务

tech2023-10-20  96

In this article I’m going to show you how we can use IronWorker to run code in the Cloud, just as if it were being run inside our PHP application’s code. There are a number of advantages to running tasks in the cloud, for example:

在本文中,我将向您展示如何使用IronWorker在云中运行代码,就像在PHP应用程序的代码中运行它们一样。 在云中运行任务有很多优点,例如:

processor-intensive tasks can be offloaded from your web server

可以从Web服务器上卸载处理器密集型任务 better fault tolerance

更好的容错能力 the execution of your code isn’t blocked waiting for long-running tasks

等待长时间运行的任务不会阻止代码的执行

Here’s how IronWorker describes its service:

IronWorker描述其服务的方式如下:

An easy-to-use scalable task queue that gives cloud developers a simple way to offload front-end tasks, run scheduled jobs, and process tasks in the background and at scale.

一个易于使用的可扩展任务队列,为云开发人员提供了一种简单的方法来卸载前端任务,运行计划的作业以及在后台大规模处理任务。

专业术语 (Glossary of Terms)

Before we get started, let’s look at some of the terms used by IronWorker and this article which may be unfamiliar to you.

在开始之前,让我们先看一下IronWorker和本文可能使用的一些术语。

A worker is our code that performs a task. We can write and test it locally, but eventually we upload it to IronWorker’s servers and it will be executed there, rather than on on our web server.

工人是我们执行任务的代码。 我们可以在本地编写和测试它,但是最终我们将其上传到IronWorker的服务器上,它将在此处执行,而不是在我们的Web服务器上执行。

A task is an execution of the code, either on demand (think of it like calling a function) or on a scheduled basis (like a cron job). We can delay execution, queue it to run a certain number of times, or run it repeatedly until a specific time and date.

任务是代码的执行,可以是按需执行(如调用函数那样考虑)或按计划执行(如cron作业)。 我们可以延迟执行,将其排队运行一定次数或重复运行直到特定的时间和日期。

The payload is the data used to execute a task (using the function analogy, the payload would be the arguments passed to the function).

有效负载是用于执行任务的数据(使用功能类比,有效负载将是传递给函数的参数)。

The IronWorker environment is essentially a Linux sandbox in the Cloud that our tasks are executed on.

IronWorker 环境本质上是在云中执行任务的Linux沙箱。

A project can be used to group workers together, perhaps for a particular application.

一个项目可以用于将工人分组在一起,也许用于特定的应用程序。

HUD is IronWorker’s web interface which we use to manage our projects and tasks, view the results, edit and view schedules, and keep track of our usage.

HUD是IronWorker的Web界面,我们使用它来管理项目和任务,查看结果,编辑和查看时间表以及跟踪使用情况。

安装与配置 (Installation and Configuration)

There are a few steps involved in getting everything set up.

设置所有步骤涉及几个步骤。

First, we need to create an account with IronWorker. It offers a free tier with up to 200 hours of processing time per month, which should be more than adequate to follow along with this article.

首先,我们需要使用IronWorker 创建一个帐户 。 它提供了一个免费层,每月最多有200个小时的处理时间,这足以满足本文的要求。

We also need the IronWorker CLI tool; visit the documentation for full installation instructions. But if you meet the pre-requisites for its installation – primarily you must have Ruby installed – you can install it in one line:

我们还需要IronWorker CLI工具。 请访问文档以获取完整的安装说明。 但是,如果满足安装的先决条件(首先必须安装Ruby),则可以将其安装在一行中:

gem install iron_worker_ng

Next, we need to download the PHP client library for working with IronWorker, either from GitHub or with Composer.

接下来,我们需要从GitHub或Composer下载PHP客户端库以与IronWorker一起使用。

{ "require": { "iron-io/iron_worker": "dev-master" } }

The easiest way to configure IronWorker is with a JSON file that looks like this:

配置IronWorker的最简单方法是使用如下所示的JSON文件:

{ "token": "YOUR-TOKEN", "project_id": "YOUR-PROJECT-ID" }

The token identifies your account, and the project ID can be set to a default and overridden by a particular worker. You can find all of this information in HUD.

令牌标识您的帐户,并且项目ID可以设置为默认值,并由特定工作人员覆盖。 您可以在HUD中找到所有这些信息。

If you name the JSON file .iron.json (note the preceding dot) and place it in your home directory, it will be used to provide default values for any of your projects. Alternatively, you can name it iron.json (no preceding dot) and place it in the same directory as the worker. The file in the same directory will override values in the home file, so you can easily set global defaults for and then override them locally for specific projects as needed.

如果您将JSON文件.iron.json (注意前面的圆点)并将其放置在主目录中,它将用于为您的任何项目提供默认值。 或者,您可以将其命名为iron.json (没有iron.json点)并将其放置在与worker相同的目录中。 同一目录中的文件将覆盖主文件中的值,因此您可以轻松地为其设置全局默认值,然后根据需要在本地为特定项目覆盖它们。

Alternatively, you can set these parameters using environment variables:

另外,您可以使用环境变量设置这些参数:

IRON_TOKEN=MY_TOKEN IRON_PROJECT_ID=MY_PROJECT_ID

你好世界的例子 (A Hello World Example)

We’ll start with a very simple example to show what’s involved in getting tasks running in the Cloud. First, create a file called HelloWorld.php:

我们将从一个非常简单的示例开始,以显示在云中运行任务所涉及的内容。 首先,创建一个名为HelloWorld.php的文件:

<?php echo 'Hello World';

Although this doesn’t do anything remotely useful, it will demonstrate an important point that you’ll see when we come to run it – anything printed to STDOUT (i.e. using echo, print, etc.) is logged by IronWorker and made able in HUD.

尽管这并没有做任何远程有用的事情,但是它将展示出一个重要的点,当我们开始运行它时,您会看到–打印到STDOUT的所有内容(例如,使用echo , print等)都由IronWorker记录并能够平视显示器

Then create a file called HelloWorld.worker with the following:

然后使用以下命令创建一个名为HelloWorld.worker的文件:

runtime 'php' exec 'HelloWorld.php'

As you can probably infer from the first line, we’re not restricted to just PHP; IronWorker supports a multitude of languages. Check the documentation for a list of supported languages.

正如您可能从第一行推断的那样,我们不仅限于PHP;还包括PHP。 IronWorker支持多种语言。 查看文档以获取支持的语言列表 。

Now we need to upload our code to IronWorker’s servers. Using the CLI tool, this is a breeze:

现在,我们需要将代码上传到IronWorker的服务器。 使用CLI工具,轻而易举:

iron_worker upload HelloWorld

Behind the scenes, the CLI tool creates a ZIP file containing our code, uploads it to IronWorker, and registers it as an available task for our project. Any time you change your code and run the upload command again, it will re-upload the code and create a new revision. You can download any revision from HUD.

在后台,CLI工具将创建一个包含我们的代码的ZIP文件,并将其上传到IronWorker,并将其注册为我们项目的可用任务。 每当您更改代码并再次运行upload命令时,它将重新上传代码并创建新的修订版。 您可以从HUD下载任何修订版。

We can enqueue tasks to run from our PHP application like this:

我们可以像这样从我们PHP应用程序中排队要运行的任务:

<?php require_once '../vendor/autoload.php'; $worker = new IronWorker(); $taskID = $worker->postTask('HelloWorld');

You can view the current state of all tasks in HUD. Depending on your account setup, you may also receive emails to keep you updated, particularly if a task fails for whatever reason.

您可以在HUD中查看所有任务的当前状态。 根据您的帐户设置,您可能还会收到电子邮件以保持最新状态,特别是如果任务由于任何原因而失败时。

An optional second argument to postTask() allows us to provide a payload if we want to send one.

如果要发送一个有效载荷,则postTask()第二个可选参数允许我们提供有效载荷。

$payload = array( 'foo' => 'bar', 'biz' => 'fuzz' ); $taskID = $worker->postTask('MyTask', $payload);

The payload is encoded for us and sent to the IronWorker servers. In the task itself, we can receive the decoded payload simply like this:

有效负载已为我们编码并发送到IronWorker服务器。 在任务本身中,我们可以像这样简单地接收解码后的有效负载:

$payload = getPayload();

We can set up a task to run on a scheduled basis, much like a cron job is run, using the postScheduleSimple() method. Simple scheduling is like saying “run this in x seconds time.”

我们可以使用postScheduleSimple()方法将任务设置为按计划运行,就像运行cron作业一样。 简单的排程就像说“以x秒的时间运行”。

// run the task in five minutes time (given in seconds) $schedID = $worker->postScheduleSimple('HelloWorld', $payload, 5 * 60);

Advanced scheduling is done with postScheduleAdvanced() and allows us to set the time of the first run, the interval, and either the time it should stop or the number of times to run it. Because so many arguments are needed, it’s it’s helpful to look at the method’s definition:

高级调度是使用postScheduleAdvanced()完成的, postScheduleAdvanced()使我们能够设置第一次运行的时间,间隔以及应该停止的时间或运行的次数。 因为需要这么多参数,所以查看方法的定义会很有帮助:

/** * Schedules task * * @param string $name Package name * @param array $payload Payload for task * @param int|DateTime $start_at Time of first run in unix timestamp format or as DateTime instance. Example: time()+2*60 * @param int $run_every Time in seconds between runs. If omitted, task will only run once. * @param int|DateTime $end_at Time tasks will stop being enqueued in unix timestamp or as DateTime instance format. * @param int $run_times Number of times to run task. * @param int $priority Priority queue to run the job in (0, 1, 2). p0 is default. * @return string Created Schedule id */ public function postScheduleAdvanced($name, $payload = array(), $start_at, $run_every = null, $end_at = null, $run_times = null, $priority = null){

Here are some examples of its use:

以下是其用法的一些示例:

// Run the task immediately, then every five minutes – a total of ten times $worker->postScheduleAdvanced('HelloWorld', $payload, time(), 5 * 60, null, 10); // run task in 3 minutes time, every two minutes, five times $worker->postScheduleAdvanced('HelloWorld', $payload, time() + (3 * 60), 2 * 60, null, 5); // run the task every hour for the next 24 hours $worker->postScheduleAdvanced('HelloWorld', $payload, time(), 60 * 60, time() + (60 * 60 * 24));

一个更有用的示例–发送电子邮件 (A More Useful Example – Sending Email)

Let’s build something slightly more useful now, and in doing so demonstrate a slightly different approach to how we structure or project and upload our code. Rather than uploading the worker with the CLI tool, we’ll create a simple upload script.

现在让我们构建一些更有用的工具,并在此过程中演示一种稍有不同的方法,以进行结构化,项目化和上载代码。 我们将创建一个简单的上传脚本,而不是使用CLI工具上传工作程序。

Create a fresh directory called worker. Copy iron.json into it, and if necessary create a new project via HUD and update the project ID in your configuration file. Then, download PHPMailer and place it in the worker directory.

创建一个名为worker的新目录。 将iron.json复制到其中,并在必要时通过HUD创建一个新项目,并在配置文件中更新项目ID。 然后, 下载PHPMailer并将其放置在worker目录中。

In the worker directory, create the file config.php which will store some configuration values for PHPMailer. It should look something like this:

在worker目录中,创建文件config.php ,该文件将存储PHPMailer的一些配置值。 它看起来应该像这样:

<?php return array( 'smtp' => array( 'host' => 'smtp.example.com', 'port' => 465, 'encryption' => 'ssl', 'credentials' => array( 'username' => 'username', 'password' => 'password' ) ) );

Now create the file Emailer.php:

现在创建文件Emailer.php:

<?php require_once './phpmailer/class.phpmailer.php'; $payload = getPayload(); $config = require './config.php'; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = $config['smtp']['host']; $mail->Port = $config['smtp']['port']; $mail->SMTPAuth = true; $mail->Username = $config['smtp']['credentials']['username']; $mail->Password = $config['smtp']['credentials']['password']; $mail->SMTPSecure = $config['smtp']['encryption']; $mail->From = 'no-reply@example.com'; $mail->FromName = 'SitePoint IronWorker Tutorial'; $mail->AddAddress($payload->recipient->email, $payload->recipient->name); $mail->WordWrap = 100; $mail->IsHTML(true); $mail->Subject = $payload->subject; $mail->Body = $payload->body; if (!$mail->Send()) { print 'Could not send email: ' . $mail->ErrorInfo; }

Create Emailer.worker:

创建Emailer.worker :

runtime 'php' exec 'Emailer.php'

And create upload.php:

并创建upload.php :

<?php require_once '../vendor/autoload.php'; $worker = new IronWorker(); $worker->upload('worker/', 'Emailer.php', 'Emailer');

That last line tells IronWorker to upload the whole of the worker directory, which of course includes not just the worker code but also theh configuration file and supporting libraries. The second argument is the name of the file to run to execute the task itself, and the third argument is simply an identifier for the worker.

最后一行告诉IronWorker上传整个worker目录,该目录当然不仅包括worker代码,而且还包括配置文件和支持库。 第二个参数是运行以执行任务本身的文件的名称,第三个参数只是工作程序的标识符。

Run the upload script from the command line:

从命令行运行上传脚本:

php upload.php

We can run the task from our PHP application the same way as before:

我们可以像以前一样从PHP应用程序运行任务:

<?php require_once '../vendor/autoload.php'; $worker = new IronWorker(); $payload = array( 'recipient' => array( 'name' => 'Joe Bloggs', 'email' => 'joe.bloggs@example.com' ), 'subject' => 'This is a test', 'body' => 'This is the body' ); $taskID = $worker->postTask('Emailer', $payload);

If you want to send a batch of emails, you can either do a simple loop to enqueue multiple tasks, or wait until the previous task has finished executing, like so:

如果要发送一批电子邮件,则可以执行简单的循环以使多个任务入队,或者等待直到上一个任务完成执行,如下所示:

// loop... $taskID = $worker->postTask('Emailer', $payload); $worker->waitFor($taskID);

更多信息 (More Information)

You should refer to the documentation to understand the IronWorker environment your code will run in. Highlights include (at the time of this writing):

您应该参考文档以了解将在其中运行代码的IronWorker环境 。主要内容包括(在撰写本文时):

The operating system is Ubuntu Linux 12.04 x64.

操作系统是Ubuntu Linux 12.04 x64。 The version of PHP is 5.3.10.

PHP的版本是5.3.10。 The php-curl, php-mysql, php-gdm and mongo modules are installed (athough you need to call require_once(‘module-name’) to use them).

会安装php-curl,php-mysql,php-gdm和mongo模块(尽管您需要调用require_once('module-name')才能使用它们)。

ImageMagick, FreeImage, SoX, and cURL are available.

ImageMagick , FreeImage , SoX和cURL可用。

Tasks have access to up to 10Gb of temporary local disk space.

任务最多可以访问10Gb的临时本地磁盘空间。 The maximum data payload is 64Kb.

最大数据有效负载为64Kb。 Each worker has around 320Mb available memory, and a maximum run time of 60 minutes.

每个工作程序具有约320Mb的可用内存,最长运行时间为60分钟。 By default, you can have up to 100 scheduled tasks per project.

默认情况下,每个项目最多可以有100个计划任务。

Although beyond the scope of this article, there are also a few features worth noting:

尽管超出了本文的范围,但是还有一些功能值得注意:

REST API – there is an API available whereby you can view or modify projects, tasks, and schedules.

REST API –有可用的API,您可以通过它查看或修改项目,任务和计划。

Webhooks – you can set up your tasks to run when a POST request is made to a specified endpoint. For example, you could set up a task to run when you commit code to GitHub.

Webhooks –您可以将任务设置为在对指定端点发出POST请求时运行。 例如,您可以设置一个任务,以在将代码提交到GitHub时运行。

Status – you can call getTaskDetails($taskID) to get that task’s status – “queued”, “complete”, “error”, etc.

状态 -您可以调用getTaskDetails($taskID)来获取该任务的状态-“排队”,“完成”,“错误”等。

Progress – you can set the progress of a task in your worker using setProgress($percent, $message), which is made available to getTaskDetails() above.

进度 –您可以使用setProgress($percent, $message)在您的工作程序中设置任务的进度,该进度可用于上述getTaskDetails() 。

Logs – as previously mentioned, anything printed to STDOUT is logged by IronWorker and is viewable in HUD. You can also access it from the worker with getLog($taskID).

日志 –如前所述,任何打印到STDOUT的内容均由IronWorker记录,并且可以在HUD中查看。 您也可以使用getLog($taskID)从工作人员访问它。

摘要 (Summary)

In this article, we’ve looked at running tasks in the cloud using IronWorker. I’ve shown you how to upload workers and queue and schedule tasks, and I’ve also mentioned some of the additional features available. If you want to explore further, I’d recommend taking time to read the documentation and look at the repository of examples on GitHub.

在本文中,我们研究了使用IronWorker在云中运行任务。 我已经向您展示了如何上传工作人员以及如何安排队列和计划任务,并且还提到了一些可用的其他功能。 如果您想进一步探索,建议您花些时间阅读文档,并查看GitHub上的示例存储库 。

翻译自: https://www.sitepoint.com/running-tasks-in-the-cloud-with-ironworker/

相关资源:Filecoin挖矿部署全流程解析.docx
最新回复(0)