3

To sync data between 2 portal, we need some action manually trigger, on click of button need to schedule a cron via programmatically. I have found solution but it is for Magento 1.

For Magento 2 what could be change ?

Code for Magento 1 is given below :

$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 = Mage::getModel('cron/schedule');
     $schedule->setJobCode($jobCode)
        ->setCreatedAt($timecreated)
        ->setScheduledAt($timescheduled)
        ->setStatus(Mage_Cron_Model_Schedule::STATUS_PENDING)
        ->save();
   } catch (Exception $e) {
     throw new Exception(Mage::helper('cron')->__('Unable to save Cron expression'));
   }

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) {
            // 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]);
    }
}
  • have you tried using this class Magento\Cron\Model\Schedule ? – Mohit Rane Aug 8 at 13:26
  • yes, its giving a runtime exception Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object – Parth Aug 8 at 13:27
  • can you show your whole code? – Mohit Rane Aug 8 at 13:29
  • added in question – Parth Aug 8 at 13:34
  • why this code in your file Mage::helper('cron') ? – Mohit Rane Aug 8 at 13:42

Your Answer

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

Browse other questions tagged or ask your own question.