Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods to support "error state" for cron schedule #3310

Merged
merged 3 commits into from
Jun 22, 2023
Merged

Added methods to support "error state" for cron schedule #3310

merged 3 commits into from
Jun 22, 2023

Conversation

luigifab
Copy link
Contributor

@luigifab luigifab commented Jun 8, 2023

Description

This PR allow user to set cron on error without throwing an exception.
Example:

public function testcron($cron = null) {

    $msg = [];
    $objects = Mage::getResourceModel('customer/customer_collection')->setPageSize(5);

    foreach ($objects as $oid => $object) {

        try {
            throw new Exception('test');
            $msg[] = $oid.' OK!';
        }
        catch (Throwable $t) {
            Mage::logException($t);
            $msg[] = $oid.' ERR '.$t->getMessage();
            if (is_object($cron))
                $cron->setIsError(true);
        }
    }

    if (is_object($cron)) {
        $cron->setData('messages', 'memory: '.((int) (memory_get_peak_usage(true) / 1024 / 1024)).
            'M (max: '.ini_get('memory_limit').')'."\n".implode("\n", $msg));
        if (!method_exists($cron, 'getIsError') && ($cron->getIsError() === true)) // without PR 3310
            Mage::throwException('At least one error occurred while...'."\n\n".
                $cron->getData('messages')."\n\n");
    }

    return $msg;
}

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)
  • Add yourself to contributors list

@github-actions github-actions bot added the Component: Cron Relates to Mage_Cron label Jun 8, 2023
fballiano
fballiano previously approved these changes Jun 9, 2023
@fballiano fballiano changed the title Allowed to set cron on error Added methods to support "error state" for cron schedule Jun 9, 2023
@sreichel
Copy link
Contributor

sreichel commented Jun 10, 2023

Can we make it mandatory to add return types for new methods? (#2748)

To follow common naming conventions the method should have prefix is or has.

Edit:

Suggestion for naming ... rename field is_error to error_flag. Add 3 methods (setErrorFlag, getErrorFlag and hasErrorFlag)

With #3245 getData could be replaced with getDataByKey ;)

@kyrena
Copy link
Contributor

kyrena commented Jun 16, 2023

There is another setIsError/getIsError usage:

$validationMessageEnvelope->setIsError($isError);

if (!$validationMessage->getIsError()) {

addison74
addison74 previously approved these changes Jun 22, 2023
@fballiano fballiano dismissed stale reviews from addison74 and themself via ccb0cfc June 22, 2023 15:43
@fballiano fballiano merged commit ea2fa76 into OpenMage:main Jun 22, 2023
@luigifab luigifab deleted the cronerr branch June 22, 2023 17:34
@sreichel
Copy link
Contributor

At least a setter method could be added. I'm no fan of magic methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Cron Relates to Mage_Cron
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants