Laravel命令和作业 [英] Laravel commands and jobs

查看:84
本文介绍了Laravel命令和作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering what the difference is between the different command-like classes in Laravel 5.1. As far as I can tell Laravel 5.1 has the following available:

  • 控制台命令(artisan make:console)
  • 命令(artisan make:command)
    • 处理程序(artisan make::command --handler)
    • Console commands (artisan make:console)
    • Commands (artisan make:command)
      • Handlers (artisan make::command --handler)

      我已经从4.2直奔5.1了,所以我不知道4.2和5.1之间发生了什么,但是我被告知基本上不应该使用中间的命令( just 命令)可以再使用-它们是从可排队作业在5.0中成为命令"时开始的,但是Laravel自此决定反对,它们只是出于兼容性考虑.但是,我不是100%,因此请您澄清一下.

      I have come straight from 4.2 to 5.1 so I don't know what happened in between 4.2 and 5.1, but I have been told that the middle one (just commands) are basically not really supposed to be used any more - they are in from when queue-able jobs became 'commands' in 5.0, but Laravel since decided against this, and they're just in for compatibility. However, I'm not 100% on this point, so clarification would be appreciated.

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

      My specific use-case is that I want a place to put a self-contained 'runnable' task. For example, something that will remove files older than 5 days from a given directory (but it could do anything).

      起初,这听起来像是控制台命令-我希望能够从artisan开始运行.但是我也可能希望按计划进行(很棒,artisan schedule:run运行控制台命令).但是我可能还想从代码异步执行它.控制台命令可以与Artisan::call()同步运行 ,但是对于异步而言,这是(我认为)队列进入的地方,突然间它就成了工作.

      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.

      好的,我们有工作.现在,我们可以从代码中将其添加到队列中,但是如何将其作为工匠命令执行(同步)呢?我可以只创建一个瘦控制台命令并向其中添加DispatchesJobs特征(或其中的代码),然后分派作业吗?作业是否总是必须排在队列中,还是我们可以使作业同步执行(理想情况下,是将其输出到控制台命令的输出?)按计划运行它也会遇到同样的问题-我是否应该创建此控制台?命令并将其添加到调度程序中,还是可以让调度程序直接运行作业?

      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?

      最后,我们有不是控制台命令的命令",它们也不是作业.正如我之前说过的,人们告诉我,这些只是从(kinda)恢复的Laravel 5.0代码更改开始的.但是artisan make命令对于他们仍然存在,因此它们不会消失.另外,如何处理自我处理命令(默认情况下,该方法随附handle方法)和需要"处理程序类(运行artisan make:command --handler)的命令有什么关系?您实际上如何使它们执行?手动使用(new App\Command\SomeCommand)->handle();(new App\handlers\SomeCommandHandler)->handle(new App\Command\SomeCommand),还是有一些我不知道的隐藏系统(也许可以使用作业/队列调度程序来调度它们)?您还可以创建排队"命令artisan make::command --queued,所以它们也有何不同?

      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?

      我想我的问题可以归结为以下内容:

      I guess my question boils down to the following:

      • 它们之间真正的(语义上的功能)区别是什么?
      • 运行"它们的正确方法是什么?
      • 对于我来说,哪种通用的独立代码需要以我认为合适的方式运行,哪个最合适?
      • What is the real (semantic and functional) difference between them all?
      • What is the correct way to 'run' them?
      • Which is best for my purposes of a generally-standalone bit of code that needs to be run, in whatever manner I feel appropriate?

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

      I found information in the documentation on how to use queues and create console commands, but nothing on exactly when to use them or really anything on command classes and handlers.

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

      Related but not exactly the same (also, it's unanswered): Laravel 5.1 commands and jobs

      推荐答案

      我看到了那些对象"就像这样:(我从我的一个辅助项目中添加了一些代码示例)

      I see those "objects" like so: (I added some code examples from one of my side projects)

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

      Things I want to execute from the command line (As you mentioned with your example with "Delete Files older than x"). But the thing is, you could extract the business logic of it to a command.

      示例:控制台命令与会发出命令以从Imgur获取图像. FetchImages 类包含获取图像的实际业务逻辑.

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

      包含实际逻辑的类.您还应该可以使用app()->make(Command::class)->handle()从应用程序中调用此命令.

      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调用并处理返回数据的逻辑.

      Example: Command mentioned in Example 1. Contains logic which does the actual API calls to Imgur and process returned data.

      我使用Laravel 5.0制作了这个应用程序,所以jobs那时还不算什么.但正如我所看到的,乔布斯就像命令一样,但是它们排在队列中并且可以分派. (正如您在这些示例中可能看到的那样,这些命令实现您提到的接口SelfHandlingShouldBeQueued).

      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).

      我认为自己是经验丰富的Laravel开发人员,但是CommandsJobs中的那些变化很难理解.

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

      从Laravel文档中:

      From the Laravel Docs:

      app/Commands目录已重命名为app/Jobs.但是,不需要将所有命令移到新位置,可以继续使用Artisan命令make:command和handler:command来生成类.

      The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.

      同样,app/Handlers目录已重命名为app/Listeners,现在仅包含事件侦听器.但是,不需要移动或重命名现有的命令和事件处理程序,并且可以继续使用handler:event命令来生成事件处理程序.

      Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.

      通过为Laravel 5.0文件夹结构提供向后兼容性,您可以将应用程序升级到Laravel 5.1,并在方便您或您的团队使用时将事件和命令缓慢升级到它们的新位置.

      By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.

      这篇关于Laravel命令和作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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