Activate/Execute CRON jobs in OXID

Hi guys,
I have a problem with activating CRON jobs for one of my customers.
The problem is that while migration to our server, our sys-admin didn’t transferred the CRON jobs and now, the old hosting account is closed, so the cron jobs are not exist anymore.

For that, I have to re-activate them again to execute on some interval of time and export articles in .csv / .txt file.

So, I need a little help, because there is a module that is used to export articles.

For now, there is only one way to do the exports:

  1. by hand, button of submit form is clicked and articles are exported

Also, the structure of module is like this:

  • ModuleName
    • Controller
      • Admin
        ExportArticles.php
        Cron.php (In Controller directory)

So, I don’t know how to execute the ExportArticles.php script as a cron using Cron.php file because as I wrote in some of my previous posts, ExportArticles extends some class and I cannot execute it in the terminal.

Also, is there any way to execute the action from the submit button as a cron? Because as I mentioned above, the only option, for now, is to execute manually.

Thanks for your help!

Please post contents of cron.php.

<?php

class Controller_Cron extends Controller_Cron_Abstract
{
    const ACTION_TRANSFER_DATA = 1;

    public function execute()
    {
        $action = (int)oxConfig::getParameter('service');

        try {
            if ($action == self::ACTION_TRANSFER_DATA) {
                $this->_transferData();
            } else {
                Engine_Model_Logger::writeFailure(__METHOD__ . ' Failure: Unkown Action "' . addslashes($action) . '" was attempted to execute!');
            }
        } catch (Exception $e) {
            Engine_Model_Logger::writeException($e);
        }

        exit(0);
    }

    public function getServices()
    {
        return array
        (
            array
            (
                'id'          => 1,
                'description' => oxLang::getInstance()->translateString('MODULENAME_ADMIN_CRONJOB_SERVICE_1_DESCRIPTION')
            )
        );
    }

    protected function _transferData()
    {
        $profileId = (int)oxConfig::getParameter('id');

        if (empty($profileId)) {
            throw new Engine_Model_Exception_InvalidId(__METHOD__ . ' Failure: no valid profile ID!');
        }

        $profile     = new DataUnifier_Profile($profileId);
        $inputStream = $profile->getInputStream();
        $lastFile   = '';

        // because the path stream attribute isn't static it has to be defined here
        if ($inputStream instanceof DataUnifier_Model_Stream_File) {
            $lastFile = basename(oxConfig::getParameter('import_file'));
            $inputStream->setPath(SHOP_DIR_ROOT . '/ImportFiles/' . $lastFile);
        }

        $outputStream = $profile->getOutputStream();

        // because the path stream attribute isn't static it has to be defined here
        if ($outputStream instanceof DataUnifier_Model_Stream_File) {
            $lastFile = basename(oxConfig::getParameter('export_file'));
            $outputStream->setPath(SHOP_DIR_ROOT . '/export/' . $lastFile);
        }

        $profile->run(array());
        $profile->setExecutedAt(date('Y-m-d H:i:s'));
        $profile->setLastFile($lastFile);
        $profile->save();
    }
}

Also, I must to mention that for any different export (Idealo or Geizhals), there is unique Profile ID that is executing the export.

You can copy /bin/cron.php to the same location. Then replace the “executing maintenance tasks” section with your code, make new Controller_Cron class and call its execute method. You have to pass the ‘service’ and ‘id’ parameter to the script you created with GET or POST, e.g. yourcron.php?service=1&id=idealoid

PS: also the parameters ‘import_file’ and ‘export_file’ have to be added.

So, you suggest me to do like this:

  1. Copy /bin/cron.php to the ModuleName location
  2. Replace “executing maintenance tasks” with code from Cron.php or ExportArticles.php?
  3. Create new Controller_Cron class and call its execute method. So, this Controller_Cron class will be set-up as Cron with needed parameters, right?

Thanks for your help!

No you copy it to /bin/mycron.php

Replace the line of code that is below “//execute maintenance tasks” with something like:

$oControllerCron = oxnew("Controller_Cron");
$oControllerCron->execute();

No the Controller_Cron class is the one you posted. You create a file in /bin and setup a cron job for this new file with needed parameters.

I got your point.

Thanks for your help!

1 Like