0

We can run model/observer function using cron as below in magento 1:

<crontab>
     <jobs>
         <CronCheck>
            <schedule>
                <cron_expr>*/15 * * * *</cron_expr>
            </schedule>
            <run>
                <model>khaosconnect/Observer::checkCronJobs</model>
            </run>
         </CronCheck>
    </jobs>
</crontab>

so checkCronJobs function executed according to scheduled cron job.

In magento2 how we can run the specific class function using cron? As we have use below in magento 2 :

<?php

namespace Company\Module\Cron;
class Runcronjobs
{
   protected $_logger;
   public function __construct(
       \Psr\Log\LoggerInterface $logger
   ) 
    {
        $this->_logger = $logger;
    }

   public function execute()
   {
       //Edit it according to your requirement
       $this->_logger->debug('Cron run successfully');
       return $this;
   }
}

In magento 2 I have checkCronJobs function in namespace Company\Module\Model class;

Is there any other way to call function in cron or we need to pass checkCronJobs with all dependencies in Runcronjobs.php file as we do normally in M2.

1
  • what your actual requirement to execute other function or another class in observer, you can directly write code here according to your requirement. Dec 8, 2017 at 12:00

1 Answer 1

0

You can add dependency of your model to cron class

namespace Company\Module\Cron;
class Runcronjobs
{
   protected $_logger;
   protected $_checkCron;
   public function __construct(
       \Psr\Log\LoggerInterface $logger,
       Company\Module\Model $checkCron
   ) 
    {
        $this->_logger = $logger;
        $this->_checkCron = $checkCron;
    }

   public function execute()
   {
       //Edit it according to your requirement
       $this->_checkCron->checkCronJobs();
       $this->_logger->debug('Cron run successfully');
       return $this;
   }
}

It will help you i think , please try once .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.