Laravel命令和作业
收藏

我想知道Laravel 5.1中不同的类似命令的类之间有什么区别。据我所知,Laravel 5.1具有以下功能:

  • Console commands (artisan make:console)
  • Commands (artisan make:command)
    • Handlers (artisan make::command --handler)
  • Jobs (artisan make:job)

我是从4.2到5.1的,所以我不知道4.2和5.1之间发生了什么,但是有人告诉我,基本上不应该再使用中间的(只是命令)了-它们来自当可排队的作业在5.0中成为“命令”时,Laravel对此表示反对,它们只是出于兼容性考虑。但是,我不是100%,因此请您澄清一下。

我的特定用例是我想要一个放置独立的“可运行”任务的地方。例如,某些操作将从给定目录中删除超过5天的文件(但它可以执行任何操作)。

At first this sounds like a console command - I want to be able to run it from artisan, for a start. But I may also want it on a schedule (great, artisan schedule:run runs console commands). But I may also want to execute it asynchronously from code. Console commands can be run synchronously with Artisan::call(), but for asynchronous, this is (I think) where queues come in, and it suddenly has to be a job.

Okay so we have a job. We can now add it to a queue from code, but how do we execute it as an artisan command (synchronously)? Can I just create a thin console command and add the DispatchesJobs trait (or the code therein) to it, and then dispatch the job? Does the job always have to go on a queue, or can we make a job execute synchronously (and, ideally, output to the console command's output?) The same question goes for running it on a schedule - am I supposed to create this console command and add that to the scheduler, or can I make the scheduler run the job directly?

And finally, we have 'commands' that aren't console commands nor are they jobs. As I said before, people tell me these are just hangers-on from a Laravel 5.0 code change that was (kinda) reverted. But the artisan make command still exists for them, so they can't be that dead. Also, what's the deal with a self handling command (the default, comes with a handle method) and one that 'requires' a handler class (run artisan make:command --handler)? How do you actually make these execute? Manually with (new App\Command\SomeCommand)->handle(); or (new App\handlers\SomeCommandHandler)->handle(new App\Command\SomeCommand), or is there some hidden system I don't know about (maybe they can be dispatched using the job/queue dispatcher)? Also you can create 'queued' commands artisan make::command --queued, so how do these differ, too?

我想我的问题可以归结为以下几点:

  • 它们之间的真正(语义和功能)区别是什么?
  • “运行”它们的正确方法是什么?
  • 对于我需要以某种我认为合适的方式运行的一般独立代码段而言,哪种方法最合适?

我在文档中找到了有关如何使用队列和创建控制台命令的信息,但没有确切地了解何时使用队列,也没有任何有关命令类和处理程序的信息。

相关但不完全相同(也没有答案):Laravel 5.1命令和作业

最佳答案

我看到这样的“对象”是这样的:(我从一个辅助项目中添加了一些代码示例)

安慰

我想从命令行执行的事情(如您在示例中提到的“删除早于x的文件”所述)。但事实是,您可以将其业务逻辑提取到命令中。

Example: A console command with fires a command to fetch images from Imgur. The Class FetchImages contains the actual business logic of fetching images.

命令

Class which contains the actual logic. You should also be able to call this command from your application with app()->make(Command::class)->handle().

示例:示例1中提到的命令。该逻辑包含对Imgur进行实际API调用并处理返回数据的逻辑。

职位

I made this app with Laravel 5.0 so jobs weren't a thing back then. But as I see it, Jobs are like commands but they are queued and can be dispatched. (As you may have seen in those examples, those commands implement your mentioned Interfaces SelfHandling and ShouldBeQueued).

I see myself as an experienced Laravel Developer but those changes in Commands and Jobs are quite difficult to understand.

编辑: 从Laravel文档中:

app / Commands目录已重命名为app / Jobs。但是,您不需要将所有命令都移到新位置,并且可以继续使用Artisan命令make:command和handler:command来生成类。      同样,app / Handlers目录已重命名为app / Listeners,现在仅包含事件侦听器。但是,不需要移动或重命名现有的命令和事件处理程序,并且可以继续使用handler:event命令来生成事件处理程序。      通过为Laravel 5.0文件夹结构提供向后兼容性,您可以将应用程序升级到Laravel 5.1,并在方便您或您的团队时将事件和命令缓慢升级到它们的新位置。

    公众号
    关注公众号订阅更多技术干货!