Carol.Kar
Carol.Kar

Reputation: 5205

Laravel 5.7 - Kill artisan after certain amount of time

I am developing a laravel 5.7 application.

I have created a command that should setup my database:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class TestSetupCommand extends Command
{
    protected $signature = 'test:data';

    protected $description = 'Basic Setup for Test Data';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Artisan::call('migrate:refresh', ['--seed' => '']);
        Artisan::call('basis:cc');
        Artisan::call('tick:exchange');

        $this->info("DB freshly setup: DONE");
        $this->info("Coin: DONE");
        $this->info("Exchange Scrapped: DONE");
    }
}

My problem is that each command takes several minutes to run through. In total it costs me 25 minutes to fill the whole database with data.

I would like to run the commands only for 1 minutes each and kill them afterwards.

Any suggestions how to accomplish this within my laravel command?

Upvotes: 0

Views: 710

Answers (1)

Msencenb
Msencenb

Reputation: 5104

I think the best way to do this is to extract these commands into background job. This artisan command then becomes code to queue up that new job (or jobs).

Why? It's very easy to configure jobs to timeout after x amount of time, by overriding a value like so:

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 120;
}

Also, why are you refreshing the database? That seems.... like a crazy idea unless this is purely an analytics platform (no user data at all). It's probably a bad thing if that refresh command times out - you may look into job chaining so that the refresh command is guaranteed to succeed, then the other commands (new jobs now) have set timeouts.

Upvotes: 1

Related Questions