处理包含许多HTTP请求的Laravel作业,无法通过超时尝试

时间:2019-10-11 14:30:53

标签: laravel cron laravel-5.8 laravel-seeding laravel-jobs

我有一个使用Laravel 5.8框架构建的API REST。

我必须从公共REST API获取我的应用程序的数据。为此,我必须提出很多要求。我做了一个播种机,然后完成整个迁移数据过程大约需要2分钟(取自公共Api并插入到我的应用程序数据库中)。

我无法通过cronjob运行播种机,因为它不起作用(看来需要用户执行命令才能起作用)。因此,我创建了一个从内核文件中调用的作业类,该队列配置被设置为数据库(QUEUE_CONNECTION=database),就像调度的任务(要与Supervisor:https://laravel.com/docs/5.8/queues#supervisor-configuration执行)一样。

尽管如此,该作业失败,因为执行时间很长,所以我的数据没有更新。

我可以成功完成批处理工作吗?

这是我的kernel.php

<?php

namespace App\Console;

use App\Jobs\Seed\ApiPlayerStatisticJob;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->job(new ApiPlayerStatisticJob)->everyFiveMinutes();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

我的ApiPlayerStatisticJob.php

<?php

namespace App\Jobs\Seed;

use App\ApiExternal;
use App\ApiPlayer;
use App\ApiTeam;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ApiPlayerStatisticJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $api_teams = ApiTeam::get();
        foreach ($api_teams as $api_team) {
            // echo 'TEAM: '.$api_team->id;
            $api_external_players = ApiExternal::getTeamPlayerStatistics($api_team->id);
            foreach ($api_external_players as $api_external_player) {
                // echo PHP_EOL.'PLAYER: '.$api_external_player['id'];
                $api_player = ApiPlayer::find($api_external_player['id']);
                if ($api_player != null) {
                    $api_player->update($api_external_player);
                    // echo PHP_EOL.'> PLAYER UPDATED ';
                } else {
                    // echo PHP_EOL.'X PLAYER DIDNT UPDATED ';
                }
            }
            // echo PHP_EOL;
            // echo PHP_EOL;
        }
    }
}

我的Seeder我用来构造我的工作的东西(通过复制,不包括打印表达式):

<?php

use Illuminate\Database\Seeder;

use App\ApiExternal;
use App\ApiPlayer;
use App\ApiTeam;

class ApiPlayerStatisticsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $api_teams = ApiTeam::get();
        foreach ($api_teams as $api_team) {
            echo 'TEAM: '.$api_team->id;
            $api_external_players = ApiExternal::getTeamPlayerStatistics($api_team->id);
            foreach ($api_external_players as $api_external_player) {
                echo PHP_EOL.'PLAYER: '.$api_external_player['id'];
                $api_player = ApiPlayer::find($api_external_player['id']);
                if ($api_player != null) {
                    $api_player->update($api_external_player);
                    echo PHP_EOL.'> PLAYER UPDATED ';
                } else {
                    echo PHP_EOL.'X PLAYER DIDNT UPDATED ';
                }
            }
            echo PHP_EOL;
            echo PHP_EOL;
        }
    }
}

最后,静态函数ApiExternal::getTeamPlayerStatistics(int id)是否需要所有请求来获取数据(如30个请求),所以我可以做些什么来在后台处理此作业(或直接执行播种机)而不会失败?

1 个答案:

答案 0 :(得分:0)

您是否正确配置了config/queue.php文件?

引用Job expirations段落中的文档:

  

在config / queue.php配置文件中,每个队列连接定义一个retry_after选项。此选项指定队列连接在重试正在处理的作业之前应等待多少秒。