【发布时间】:2016-10-06 19:55:28
【问题描述】:
我正在使用 Laravel 5.2。我目前有一个 cron 作业,它从我的数据库中获取数千行并对这些行中的每一行执行计算。 cron 作业目前大约需要一个小时才能运行,但数据库每天都在不断变大。
一种选择是使用crontab -e 手动添加更多 cron 作业(即,将作业分成 5 个较小部分的 5 个 cron 作业)。但是,是否也可以执行以下操作:
- 使用
crontab -e设置单个 cron 作业。 - cron 作业脚本从数据库中获取所有行,并根据返回的行数确定应进一步拆分为多少个 cron 作业。例如,如果数据库返回 1000 行,则应将其拆分为 10 个 cron 作业,但如果数据库返回 10000 行,则应将其拆分为 100 个 cron 作业。
所以我的问题是,是否有可能在crontab -e 中只列出一个 cron 作业,然后 PHP 脚本决定它应该分成多少个 cron 作业(本质上是从 PHP 脚本调用更多的 cron 作业)?
这样做安全吗?有没有更好的方法来解决这个问题?
这是我当前的脚本:
class PostCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post_cron';
/**
* The console command description.
*
* @var string
*/
protected $description = '';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$posts = Post::all(); // currently returns ~1000 rows, but grows in size daily
foreach ($posts as $post) {
// computations on each separate post
}
}
}
【问题讨论】:
-
2 没有创建新的 cron 作业;但只需运行 X 个脚本
-
@Dagon 这就是我的意思
标签: php mysql laravel laravel-5 cron