13

To make a long story short xdebug will not reliably stop on the breakpoints in my cron files when using the typical:

php bin/magento cron:run

Xdebug is all setup properly and sometimes it will work but 85% of the time it will not and just hang in CLI until I turn off listen for connections in phpstorm.

Is there a way to run just one cron file somehow? I had to actually copy and paste the contents of my cron file to a separate console command to test it properly in xdebug which is not ideal.

In Magento 1.x I used to use the AOE Scheduler for testing cron jobs with Xdebug by going to the admin, checkboxing the one I wanted, and choosing run from the dropdown and it worked fine.

I'm looking to use something like:

php bin/magento modulename:cronjob

which should always run the cron even if it is not scheduled.

15

Think simple! The Cron class is a "normal" class. We can use a Playground to test our Cron: How can I bootstrap Magento 2 in a test.php script?. The Object Manager will create our Cron Object. And then, we can test our Cron by calling the url directly on Browser.

** Note if using Nginx you can put these files in the pub/ folder and adjust the require path for the Test.php file to require '../app/bootstrap.php';

Test.php

<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

TestApp.php

 public function launch()
 {
        /** @var \Vendor\Module\Cron\Test $cron */
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create('Vendor\Module\Cron\Test');

        $cron->execute();

        return $this->_response;

 }

enter image description here

  • 1
    Thank you, this worked for me but with a little tweaking. Since I'm using Nginx it does not check the root directory, so I had to put the Test.php and TestApp.php in the pub directory. After that I adjusted the path on Test.php require to: require '../app/bootstrap.php'; also on the TestApp.php there needs to be the: return $this->_response; or it will throw an error which you have on your phpstorm screenshot but not the actual code. I will try and edit your answer with this. – Kevin Chavez Jan 3 '17 at 14:38
  • @KevinJavitz you're welcome! – Khoa TruongDinh Jan 3 '17 at 14:42
17

On option to save in the development time would be to use N98MageRun for Magento 2.

This has the wonderful commands sys:cron:list and sys:cron:run.

Using these commands you will be able to find the specific job code for your cron and then trigger just that cron from the command line.

It can easily be installed via composer require-dev n98/magerun2 and I would recommend it should be a go to dev installation when working with Magento 2

  • OMG Wao best way to run cronjob by just a command – Learner Mar 14 '18 at 8:53
3

You can achieve using two files:

create folder and class in root of project like:

crons/CronprocessApp.php

    <?php
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;

class CronprocessApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface{

    public function __construct(
    \Magento\Framework\App\State $state,\Magento\Framework\App\Response\Http $response)
    {
        $this->_response = $response;
        //$state->setAreaCode('any area'); // or 'adminhtml', depending on your needs
        $state->setAreaCode('adminhtml'); // or 'adminhtml', depending on your needs
    }
    public function launch()
    {
        /** @var \Vendor\Module\Cron\Test $cron */
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create('Custom\Preorder\Cron\ChangeVisiblityNonPreorderProduct'); //pass the name of your cron class path 
        $cron->execute();       


        return $this->_response;

    }
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}
?>

Create another class file:

crons/Cronprocess.php

 <?php
require __DIR__ . '/../app/bootstrap.php';
require __DIR__ . '/../crons/cronprocessApp.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CronprocessApp');
$bootstrap->run($app);

To Run cron go to cli with project root path and run below command:

php crons/cronprocess.php
0

sometimes it will work but 85% of the time it will not and just hang in CLI until I turn off listen for connections in phpstorm.

How to prevent Xdebug from locking on running bin/magento cron:run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.