Magento 2 - Test cronjob from browser
Question
I have created a cronjob in:
app/code/Vendor/Module/Cron/MyCron.php
It's in development phase, so every time I need to check the data or debug anything I am creating a log file and rescheduling it to run cron.
My question is, is there any way to run cron from browser? so that I can test it instantly.
Thanks in advance.
Solution
Yes you can test your cron on browser.
Create a model in you module
app/code/Vendor/Module/Model/YourModel.php
add a function
public function doStuff()
{
//Add cronjob code here
}
Now move all the code of your cron in this function doStuff()
.
Create frontend contorller
app/code/Vendor/Module/Index/MyController.php
in execute
function call your model function
$this->model->doStuff();
where $this->model
is the object of your model.
now you can test your code through controller.
After your work is done, call the same model function in your cron
That should work :)
OTHER TIPS
You may follow the steps below to test a cron job from browser.
Step 1)
Create a PHP file names "cronLaunch.php" under your project root directory
FIle: cronLaunch.php
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
//require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['job'])) {
define('CRONJOBCLASS', $_GET['job']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the class of the cron job you want to execute as a job parameter (?job=Vendor\Module\Class)');
} elseif (!isset($argv[1])) {
die('Please add the class of the cron job you want to execute enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('CRONJOBCLASS', $argv[1]);
}
class CronRunner 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('adminhtml');
}
function launch()
{
$cron = \Magento\Framework\App\ObjectManager::getInstance()
->create(CRONJOBCLASS);
$cron->execute();
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('CronRunner');
$bootstrap->run($app);
step 2)
Open browser and text the cron job
I assume you your cron class file is MyVendor/MyModule/Cron/CronClass.php
so enter the following URL in your browser and test your cron file.
http://localhost/YOURMAGENTOPROJECT/cronLaunch.php?job=MyVendor\MyModule\Cron\CronClass
I recommend you to use n98-magerun2 https://github.com/netz98/n98-magerun2 . After you give the n98-magerun2.phar execute permission you could run
Get all available cron job list.
./n98-magerun2.phar sys:cron:list
To run you specific cron
./n98-magerun2.phar sys:cron:run your_cron_job_code
I have done some modification previously example for choice method in class
<?php
require __DIR__ . '/app/bootstrap.php';
//require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['job'])) {
define('CRONJOBCLASS', $_GET['job']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the class of the cron job you want to execute as a job parameter (?job=Vendor\Module\Class)');
} elseif (!isset($argv[1])) {
die('Please add the class of the cron job you want to execute enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('CRONJOBCLASS', $argv[1]);
}
$method = (isset($_GET['method']))? $_GET['method'] : 'execute';
define('CRONJOBMETHOD', $method);
class CronRunner 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('adminhtml');
}
function launch()
{
$cron = \Magento\Framework\App\ObjectManager::getInstance()
->create(CRONJOBCLASS);
$cron->{CRONJOBMETHOD}();
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('CronRunner');
$bootstrap->run($app);
and URL like that
http://your-host/cronLaunch.php?job=MyVendor\MyModule\Cron\CronClass&method=YourMethod