M
M
Morfeey2018-11-12 15:52:20
Laravel
Morfeey, 2018-11-12 15:52:20

Cron and Laravel, where are the pitfalls?

There is a small project in which it is necessary to make tasks that are customizable in time.
I'm trying to use laravel, which has several options for the development of events in the dock, such as:
Commands (required for them):

Kernel.php
<?php

namespace App\Console;

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 = [
        '\App\Console\Commands\RemovalPositions'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command("site:removalPositions")->everyMinute();
    }

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

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

RemovalPositions.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class RemovalPositions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'site:removalPositions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        //$this->site = $site;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //$name = $this->site->name;
        DB::table("TEST")->insert(["sdf" =>"222323"]);
    }
}


Or, in the Kernel class, in the schedule method, write commands with callbacks of the form:
$schedule->call('[email protected]')->everyMinute();

or
$schedule->call(function () {
            DB::table("TEST")->insert(["sdf" => "12312313123"]);
        })->everyMinute();

As a result, when executing the command, php artisan schedule:run
we get the following:
No scheduled commands are ready to run.

What am I doing wrong ?
PS: The cron itself works, Laravel twitches normally once a minute according to the standard.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-11-12
@Sanasol

Because cron runs on time.
The php artisan schedule:run command itself is run by cron every minute.
And performs, respectively, the tasks that should be at this time.
In order to manually start it is necessary to start exactly in time at zero second in this case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question