0

This code was all working every hour on everyday for 6 months and randomly stopped working now and the Cron doesn't even run. Has anyone else experienced something similar to this? I'm on Magento 1.7 and haven't updated anything, I've cleared the cache I'm just lost as to why it would stop all of the sudden or if anyone has any suggestions on what to do.

app/code/local/Sean/CustomCron/etc/config.xml

<config>
    <global>
        <models>
            <seancustomcron>
                <class>Sean_CustomCron_Model</class>
            </seancustomcron>
        </models>
    </global>
    <crontab>
        <jobs>
            <seancustomcron>
                <schedule>
                    <cron_expr>0 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>seancustomcron/observer::setGoldPrice</model>
                </run>
            </seancustomcron>
        </jobs>
    </crontab>
</config>

app/code/local/Sean/CustomCron/Model/Observer

<?php

class Sean_CustomCron_Model_Observer
{
    public function setGoldPrice()
    {
        /**
         * Gets price of gold from goldUpdate.php
         * @return string
         */
        function getGoldPrice()
        {
            $baseUrl = "http://liveSite.mywebsite.com";
            $getString = http_build_query(array("action" => "getGoldPrice"));
            $getUrl = $baseUrl . "/server/goldPrices/goldUpdate.php?" . $getString;
            return file_get_contents($getUrl);
        }

        /**
         * Saves given price to liveSite
         *      also to svg-editor-ml
         * @param $goldPrice {number}
         */
        function saveGoldPrice($goldPrice)
        {
            date_default_timezone_set("America/Los_Angeles");
            $date = getdate();
            $year = sprintf("%04d", $date["year"]);
            $month = sprintf("%02d", $date["mon"]);
            $day = sprintf("%02d", $date["mday"]);
            $hours = sprintf("%02d", $date["hours"]);
            $minutes = "00";
            $time = $hours . ":" . $minutes;

            $postString = http_build_query(
                array(
                    "action" => "setPrice",
                    "year" => $year,
                    "month" => $month,
                    "date" => $day,
                    "time" => $time,
                    "value" => $goldPrice,
                )
            );
            $options = array(
                "http" => array(
                    "method" => "POST",
                    "content" => $postString
                )
            );
            $context = stream_context_create($options);
            // save to svg-editor-ml
            $baseUrl = "http://svg-editor-ml.mywebsite.com";
            $postUrl = $baseUrl . "/server/goldPrices/index.php";
            file_get_contents($postUrl, false, $context);
            // save to liveSite
            $baseUrl = "http://liveSite.mywebsite.com";
            $postUrl = $baseUrl . "/server/goldPrices/index.php";
            file_get_contents($postUrl, false, $context);
        }

        /**
         * Temporarily marks a goldMine as broken
         * @return string
         */
        function setBroken()
        {
            $baseUrl = "http://liveSite.mywebsite.com";
            $postString = http_build_query(array("action" => "setBroken"));
            $postUrl = $baseUrl . "/server/goldPrices/goldUpdate.php";
            $options = array(
                "http" => array(
                    "method" => "POST",
                    "content" => $postString
                )
            );
            $context = stream_context_create($options);
            return file_get_contents($postUrl, false, $context);
        }

        /**
         * Determines if should fetch gold price
         *      (based on market hours)
         * @return bool
         */
        function shouldRun()
        {
            date_default_timezone_set("America/Los_Angeles");
            $date = getdate();
            $weekday = $date["wday"];
            $hours = $date["hours"];
            switch ($weekday) {
                case 0: // Sunday
                    return $hours >= 12;
                case 1: // Monday
                case 2: // Tuesday
                case 3: // Wednesday
                case 4: // Thursday
                    return true;
                case 5: // Friday
                    return $hours <= 18;
                case 6: // Saturday
                    return false;
                default:
                    return false;
            }
        }

        if (!shouldRun()) {
            return;
        }
        do {
            $goldPrice = getGoldPrice();
            if (is_numeric($goldPrice)) {
                saveGoldPrice($goldPrice);
            } else if (setBroken() == "true") {
                break;
            }
        } while (!is_numeric($goldPrice));
    }
}

1 Answer 1

1

It sounds like your crontab configuration was changed. Is the rest of Magento cron working?

To see if the job is still set up to run, run the command crontab -e under the user account it was set up under. Look for a cron.php entry that matches the path to your project.

2
  • Are you referring to the SSH in Command/Terminal to run crontab -e? Feb 7, 2017 at 23:42
  • @CodingMageSheen yes.
    – tjons
    Feb 7, 2017 at 23:52

Your Answer

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

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