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

1 Answer 1

1

Use the below code to fix the issue.

<?php

use Magento\Cron\Model\ScheduleFactory;

    $jobCode  = 'jobcode';

    $schedule = $this->scheduleFactory->create();

    $schedule->setJobCode($jobCode)
             ->setCreatedAt($timecreated)
             ->setScheduledAt($timescheduled)
             ->setStatus(self::STATUS_PENDING)->save();

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.