Tweeted twitter.com/StackMagento/status/1164824628591648769
4 remove extra code
source | link
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use ABC\Du\Helper\Data;

class Products extends Action
{

    protected $resultJsonFactory;

    protected $schedule;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @param Context $context
     * @param Model $schedule
     * @param JsonFactory $resultJsonFactory
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Magento\Cron\Model\Schedule $schedule,
        JsonFactory $resultJsonFactory,
        Data $helper
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->schedule = $schedule;
        parent::__construct($context);
    }

    /**
     * Collect relations data
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
        $timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
        $jobCode = 'job_id';

        try {
            // schedule job

        } catch (Exception $e) {
            throw new Exception(Mage::helper('cron')->__('Unable to save// Cronexception expression'));handle
        }

        $message = 'Cron schedule at ' . $timecreated . ' will execute at ' . $timescheduled;
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true, 'time' => $message]);
    }
}
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use ABC\Du\Helper\Data;

class Products extends Action
{

    protected $resultJsonFactory;

    protected $schedule;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @param Context $context
     * @param Model $schedule
     * @param JsonFactory $resultJsonFactory
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Magento\Cron\Model\Schedule $schedule,
        JsonFactory $resultJsonFactory,
        Data $helper
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->schedule = $schedule;
        parent::__construct($context);
    }

    /**
     * Collect relations data
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
        $timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
        $jobCode = 'job_id';

        try {
            // schedule job

        } catch (Exception $e) {
            throw new Exception(Mage::helper('cron')->__('Unable to save Cron expression'));
        }

        $message = 'Cron schedule at ' . $timecreated . ' will execute at ' . $timescheduled;
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true, 'time' => $message]);
    }
}
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use ABC\Du\Helper\Data;

class Products extends Action
{

    protected $resultJsonFactory;

    protected $schedule;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @param Context $context
     * @param Model $schedule
     * @param JsonFactory $resultJsonFactory
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Magento\Cron\Model\Schedule $schedule,
        JsonFactory $resultJsonFactory,
        Data $helper
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->schedule = $schedule;
        parent::__construct($context);
    }

    /**
     * Collect relations data
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
        $timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
        $jobCode = 'job_id';

        try {
            // schedule job

        } catch (Exception $e) {
            // exception handle
        }

        $message = 'Cron schedule at ' . $timecreated . ' will execute at ' . $timescheduled;
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true, 'time' => $message]);
    }
}
3 updated action code
source | link

Magento 2 action code

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use ABC\Du\Helper\Data;

class Products extends Action
{

    protected $resultJsonFactory;

    protected $schedule;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @param Context $context
     * @param Model $schedule
     * @param JsonFactory $resultJsonFactory
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Magento\Cron\Model\Schedule $schedule,
        JsonFactory $resultJsonFactory,
        Data $helper
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->schedule = $schedule;
        parent::__construct($context);
    }

    /**
     * Collect relations data
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
        $timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
        $jobCode = 'job_id';

        try {
            // schedule job

        } catch (Exception $e) {
            throw new Exception(Mage::helper('cron')->__('Unable to save Cron expression'));
        }

        $message = 'Cron schedule at ' . $timecreated . ' will execute at ' . $timescheduled;
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true, 'time' => $message]);
    }
}

Magento 2 action code

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use ABC\Du\Helper\Data;

class Products extends Action
{

    protected $resultJsonFactory;

    protected $schedule;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * @param Context $context
     * @param Model $schedule
     * @param JsonFactory $resultJsonFactory
     * @param Data $helper
     */
    public function __construct(
        Context $context,
        Magento\Cron\Model\Schedule $schedule,
        JsonFactory $resultJsonFactory,
        Data $helper
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->schedule = $schedule;
        parent::__construct($context);
    }

    /**
     * Collect relations data
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $timecreated   = strftime("%Y-%m-%d %H:%M:%S",  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
        $timescheduled = strftime("%Y-%m-%d %H:%M:%S", mktime(date("H"), date("i")+ 5, date("s"), date("m"), date("d"), date("Y")));
        $jobCode = 'job_id';

        try {
            // schedule job

        } catch (Exception $e) {
            throw new Exception(Mage::helper('cron')->__('Unable to save Cron expression'));
        }

        $message = 'Cron schedule at ' . $timecreated . ' will execute at ' . $timescheduled;
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true, 'time' => $message]);
    }
}
2 added related tag to find solution quicker
| link
1
source | link