amitshahc
11 months ago
922
2
Laravel

Custom command running directly from CLI but not from schedule:run in CLI

Posted 11 months ago by amitshahc

My OS is Windows 7 : Laravel 5.8

Some how I also found the same issue and tried everything listed here but no solution yet. I am using Laravel 5.8 and when i run the command from my windows cmd using

php artisan subscription:fallback it's running and i can verify the entries in the database which been created by that job.

But the same command is not executing when i call php artisan schedule:run it prints the messages in before() and after() hooks plus sending the email but with blank content. but no code being executed inside the handle() method.

myLaravelProject >php artisan schedule:run
Running scheduled command: "D:\Installation\php7114\php.exe" "artisan" inspire >> "D:\Installation\Apache24\htdocs\spaadvisor.com\storage\logs/schedule/2019-08-29.log" 2>&1
Running scheduled command: "D:\Installation\php7114\php.exe" "artisan" subscriptions:fallback 2 --force >> "D:\Installation\Apache24\htdocs\spaadvisor.com\storage\logs/schedule/2019-08-29.log" 2>&1
subscriptions:fallback command is started..
Finished.!

FreeSubscriptionFallback:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Modules\SuperAdmin\Entities\Repository\Contract\CronjobRepository;

class FreeSubscriptionFallback extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'subscriptions:fallback {days=2: number of max days from today to calculate expiry date range}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Auto renewal of shop-owner\'s subscriptions to Free for which are expiring within next {days=2} days';

    /**
     * Create a new command instance.
     *
     * @return void
     */

    protected $repo;

    public function __construct(CronjobRepository $repo)
    {
        parent::__construct();
        $this->repo = $repo;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('Subscription fallback command logic : STARTED');
        // dd('hello');
        $days       = (int) $this->argument('days');
        $shopowners = $this->repo->getActiveSubscriptions($days);
        // dd($shopowners);
        try {
            $shopowners->each(function ($shopowner) {
                $this->line('Creating default subscription for shopowner id:', $shopowner->id);
                $this->repo->beginTransaction();
                $this->repo->createSubscription_default($shopowner);
                $this->repo->commit();
            });

            $this->info('Subscription fallback command logic : ENDED');

        } catch (\Throwable $th) {
            $this->repo->rollback();
            $this->error('Subscription fallback command logic : ERROR', $th);
            report($th);
        }
    }
}

Kernel.php:

<?php

namespace App\Console;

use Artisan;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Modules\SuperAdmin\Jobs\SubscriptionRenewal;
use Modules\SuperAdmin\Entities\Repository\Contract\CronjobRepository;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\FreeSubscriptionFallback::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $today    = \DateTimeOperations::today()->toDateString();
        $logfile  = config('constants.scheduler.logfile_path') . $today . '.log'; //storage_path("logs/schedule/". $today .".log");
        $cronRepo = resolve(CronjobRepository::class);

        $schedule->job(new SubscriptionRenewal($cronRepo))->daily()
            ->before(function () {
                echo "SubscriptionRenewal job is started..\n";
            })
            ->after(function () {
                echo "Databse queue is started..\n";
                echo $exitCode = Artisan::call('queue:work database --stop-when-empty');
                echo "Finished.!\n";
                echo Artisan::output();
            })
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));

        $schedule->command("inspire")->everyMinute()
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));

        $schedule->command("subscriptions:fallback 2 --force")->everyMinute()
            ->before(function () {
                echo "subscriptions:fallback command is started..\n";
            })
            ->after(function () {
                echo "Finished.!\n";
                // echo Artisan::output();
            })
            ->onSuccess(function () {
                echo "Success";
            })
            ->onFailure(function () {
                echo "Failed";
            })
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));
        // echo Artisan::output();
    }

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

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

After adding ->onFailure() in above i realized that it's printing Failed but no logs anywhere can be found (email, logfile, laravel log, catch..) So how to print the Failure reason?

Please sign in or create an account to participate in this conversation.