最佳实践如何安排 symfony2 操作 [英] best practice how to schedule symfony2 action

查看:23
本文介绍了最佳实践如何安排 symfony2 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于 symfony2 的应用程序必须在不同的时刻和不同的频率做一些独立的工作.例如,发送简讯、编辑报告...

My application based on symfony2 has to do some standalone jobs at differents moments and differents frequencies. For exemple, sending newsletter, editing report ...

我希望能够从应用程序添加/编辑每个任务我的任务将非常接近 cron 作业,并且将包括调用特定的 url.

I want to be able to add/edit each task from the application My task would be very close to cron jobs and will consist to call a specific url.

我考虑创建一个 cron 作业,每半小时或十分钟启动一个脚本.这个脚本只做一个 curl 命令女巫得到一个 docronjob 动作.这个 docronjobaction 位于 symfony 内部,负责获取所有任务并启动它必须执行的任务.

I think about creating a cron job that launch a script every half hour, or ten minutes. this script only do a curl command witch get a docronjob action. This docronjobaction is inside symfony and is responsible to get all the task and launch the one it has to.

  • 有没有更简洁的方法来实现这一目标?
  • 在文档中我如何启动其他操作?
  • 如何将整个控制器的时间限制设置为 0?
  • 是否已经存在执行此操作的捆绑包?

推荐答案

听起来您正在将任务计划存储在数据库中.没问题.为每一种不同类型的任务(例如发送时事通讯、保存报告)创建一个服务来完成任务.然后向这个服务添加一个标签(比如 twig.extension,但是你自己的),所以你有一个类似于 TaskChain 的东西,它知道所有的任务.

It sounds you are storing your schedule of tasks in the database. No problem. For every different type of task (eg. sending newsletter, save reports) create a service, which does the task. Then add to this services a tag (like twig.extension, but your own), so you have something like a TaskChain, which knows all the tasks.

为了执行创建一个控制台命令,它从数据库,加载任务链,并执行任务.这个控制台命令可以简单地从 cronjob 中调用,而无需将其暴露给网络.事实上,您是通过 php-cli 而不是从浏览器调用此命令,标准时间限制是无限的.执行时不应涉及控制器.

For executing create a console command, which retrieves the schedule from the database, loads the TaskChain, and executes the tasks. This console command can be simply called from a cronjob without exposing it to the web. In fact your are calling this command via the php-cli and not from a browser, the standard time limit is unlimited. No controllers should be involved for executing.

这应该全部组织在一个额外的 TaskBundle 中.

This should be all organized in an extra TaskBundle.

这篇关于最佳实践如何安排 symfony2 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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