繁体   English   中英

laravel cron 工作不工作

[英]laravel cron job not working

我的 laravel cron 工作不能自动工作。 我测试它,它似乎工作,当我 php artisan checkCommissions:check_commissions 它完美地运行我的任务。 所以我认为问题出在我在 cpanel 中写的命令上,所以有什么帮助吗?

应用\\控制台\\命令\\CheckCommissions.php:

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\User;
use App\Models\Setting;
use App\Models\Commission;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check users counters and give them commissions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $maxout = Setting::where('name', 'maxout')->first();
        if (!is_null($maxout)) {
            $maxout = $maxout->value;
        }

        if (is_null($maxout) || !is_numeric($maxout) || $maxout < 0) {
            $maxout = 0;
        }

        $commission = Setting::where('name', 'commission')->first();
        if (!is_null($commission)) {
            $commission = $commission->value;
        }

        if (is_null($commission) || !is_numeric($commission) || $commission < 0) {
            $commission = 0;
        }

        $users = User::all();

        foreach ($users as $user) {
            if ($user->counter_right >= $maxout && $user->counter_left >= $maxout) {
                $num = $maxout;
                $update_counters = User::where('id', $user->id)->update(['counter_left' => 0, 'counter_right' => 0]);
            } else {
                if ($user->counter_right >= $user->counter_left) {
                    $num = $user->counter_left;
                } else {
                    $num = $user->counter_right;
                }

                $update_counters = User::where('id', $user->id)->update(['counter_left' => $user->counter_left - $num, 'counter_right' => $user->counter_right - $num]);
            }

            if($num > 0) {
                $money = $num * ($commission / 2);

                $update_user = User::where('id', $user->id)->update(['money' => $user->money + $money]);

                Commission::insert(['user_id' => $user->id, 'money' => $money]);
            }
        }
    }
}

应用\\控制台\\内核.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\checkCommissions',
    ];

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

        $schedule->command('checkCommissions:check_commissions')
                 ->everyMinute(); //
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

和 Cpanel 中的命令:

分钟 小时 日 月 工作日 命令操作

0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1

首先欢迎使用 StackOverflow。 是的,您的命令是错误的...您想在应用程序控制台内核中运行计划的命令,而不是计划命令执行。 请阅读有关任务调度的 Laravel 官方文档(也可能是cron wiki )。

您只是告诉 CPanel crontab 每天在午夜运行命令checkCommissions:check_commissions但在您的调度程序中您希望每分钟运行一次该命令。

首先,您必须从

0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1

* * * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan schedule:run >> /dev/null 2>&1

为了启动调度程序。 然后 Laravel 会处理剩下的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM