在laravel中一次发布所有计划任务

时间:2019-11-11 12:09:10

标签: php laravel pthreads scheduled-tasks scheduling

我在特定时间在数据库中有计划的帖子列表。并且正在使用 crontab 调用该函数并使用 foreach 循环执行所有操作。

我的问题是,假设我在同一用户上午10:00同时有10条帖子,现在crontab将触发事件并调用该函数,这里是获取所有10条帖子并传递它进入foreach循环。

现在,当最后10个帖子完成时,时间将是上午10:10,比完成一个帖子要花费1分钟或更短的时间。现在用户会想到为什么原定于上午10点发布的帖子会在上午10:10传播?

我希望同时发布所有帖子,请给我一些解决方案,我知道我需要使用多线程概念。在这方面需要帮助。

这是我的代码,如何执行。

在crontab下,我有这个。

* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1

在内核下,我有这个

  {
    $schedule->command('schedule:post')->everyMinute();
  }

并在Console-> Commands-> SchedulePost.php文件中

   public function handle(PostService $service) 
{   
    $curent_time = Carbon::now();
    $posts = ScheduledPosts::where('status',2)->where('actual_post_time',date('Y-m-d H:i'))->get();
    foreach ($posts as $key => $post) {

    // here am calling post service which will actually do post
    $post_done = $service->post($user, $newpost->message,$newpost->id);

    }

}

0 个答案:

没有答案