1

To sync data between 2 portal, I 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.

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]);
    }
}

Its throwing runtime exception

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object

Shaheryar.Akram
  • 722
  • 10
  • 19
Parth
  • 55
  • 1
  • 2
  • 12

1 Answers1

-1

Please refer this URL to create custom cron job in Magento 2 programattically.

https://www.mageplaza.com/devdocs/magento-2-create-cron-job/

Mitali
  • 144
  • 4
  • That is not what the OP asked for. They are looking to dynamically schedule an already registered cron job. The link you provided tells how to register a new cron job with Magento. – Louis B. Jul 21 '21 at 07:06