1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Constant server errors from Cron job

Discussion in 'General PHP and MySQL Discussions' started by rivacom, Mar 19, 2014.

  1. rivacom

    rivacom Active Member

    So I have a file that is ran every 5 minutes via the Xenforo Cron interface. It runs fine, and everything seems to be working. But my error logs are filling up with a few errors.

    Here is my code that is being run.

    Code:
    <?php
    
    $con=mysqli_connect("localhost","username","password","database");
    
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    $deletetable = $con->prepare('TRUNCATE TABLE twitch_streams');
    $deletetable->execute();
    $deletetable->close();
    
    if ($result = $con->prepare("SELECT field_value
    FROM xf_user_field_value
    WHERE field_id = 'twitch'
    AND field_value != ''"))  {
    
    $result->execute();
    $result->bind_result($twitchfield);
    $result->store_result();
    
    while($result->fetch())
      {
       $count = 0;
       $data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $twitchfield));
       $viewer[0] = $data[0]->channel_count;
    
       if ($insert = $con->prepare("INSERT INTO twitch_streams (twitchuser, viewercount) VALUES (?, ?)")){
       $insert->bind_param('si', $twitchuser, $viewercount);
    
       $twitchuser = $twitchfield;
       $viewercount = $viewer[0];
    
       $insert->execute();
       $insert->close();
    }
    else {
    printf("Insert did not work: %s\n", $con->error);
    }
    
    }
    
    }
    else {
    printf("Prepared Statement Error: %s\n", $con->error);
    }
    
    
    mysqli_close($con);
    ?>
    Any ideas what im doing wrong here?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Post the full errors and stack traces. The line numbers the errors are occurring on will be useful.
     
  3. rivacom

    rivacom Active Member

    Undefined Set 0

    Code:
    #0 /home/revenantgaming/public_html/code/scripts/testsql.php(28): XenForo_Application::handlePhpError(8, 'Undefined offse...', '/home/revenantg...', 28, Array)
    #1 /home/revenantgaming/public_html/library/TwitchStream/cron.php(8): include('/home/revenantg...')
    #2 [internal function]: TwitchStream_cron::getUpdate(Array)
    #3 /home/revenantgaming/public_html/library/XenForo/Model/Cron.php(356): call_user_func(Array, Array)
    #4 /home/revenantgaming/public_html/library/XenForo/Deferred/Cron.php(24): XenForo_Model_Cron->runEntry(Array)
    #5 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(243): XenForo_Deferred_Cron->execute(Array, Array, 9.9999978542328, '')
    #6 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(369): XenForo_Model_Deferred->runDeferred(Array, 9.9999978542328, '', false)
    #7 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(322): XenForo_Model_Deferred->_runInternal(Array, NULL, '', false)
    #8 /home/revenantgaming/public_html/deferred.php(18): XenForo_Model_Deferred->run(false)
    Code:
    array(3) {
    ["url"] => string(42) "http://www.revenantgaming.com/deferred.php"
    ["_GET"] => array(0) {
    }
    ["_POST"] => array(4) {
    ["_xfRequestUri"] => string(8) "/forums/"
    ["_xfNoRedirect"] => string(1) "1"
    ["_xfToken"] => string(8) "********"
    ["_xfResponseType"] => string(4) "json"
    }
    }
    
    Trying to get property of non-object

    Code:
    #0 /home/revenantgaming/public_html/code/scripts/testsql.php(28): XenForo_Application::handlePhpError(8, 'Trying to get p...', '/home/revenantg...', 28, Array)
    #1 /home/revenantgaming/public_html/library/TwitchStream/cron.php(8): include('/home/revenantg...')
    #2 [internal function]: TwitchStream_cron::getUpdate(Array)
    #3 /home/revenantgaming/public_html/library/XenForo/Model/Cron.php(356): call_user_func(Array, Array)
    #4 /home/revenantgaming/public_html/library/XenForo/Deferred/Cron.php(24): XenForo_Model_Cron->runEntry(Array)
    #5 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(243): XenForo_Deferred_Cron->execute(Array, Array, 9.9999978542328, '')
    #6 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(369): XenForo_Model_Deferred->runDeferred(Array, 9.9999978542328, '', false)
    #7 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(322): XenForo_Model_Deferred->_runInternal(Array, NULL, '', false)
    #8 /home/revenantgaming/public_html/deferred.php(18): XenForo_Model_Deferred->run(false)
    #9 {main}
    Code:
    array(3) {
      ["url"] => string(42) "http://www.revenantgaming.com/deferred.php"
      ["_GET"] => array(0) {
      }
      ["_POST"] => array(4) {
        ["_xfRequestUri"] => string(8) "/forums/"
        ["_xfNoRedirect"] => string(1) "1"
        ["_xfToken"] => string(8) "********"
        ["_xfResponseType"] => string(4) "json"
      }
    }
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    The undefined offset error relates to this line:
    PHP:
    $viewer[0] = $data[0]->channel_count;
    That indicates that the array $data does not contain an index [0].

    So it suggests that:
    PHP:
    $data json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' $twitchfield));
    Isn't returning the expected data.

    You should run some tests to see what data that is returning. If you can reproduce the error and execute it manually not using the Cron system, or you can execute the code in some other way, you should look at doing some debugging on the response you are getting, e.g.

    PHP:
    Zend_Debug::dump($data);
    die();
    That will output the contents of $data and kill the PHP script.

    The next error is consistent with the above. It's saying that $data is not an object. That ties in with the above error. json_decode is not returning an object.

    Just a side note on json_decode. It's often easier to work with the data as an associative array. You can do that by changing the syntax to:

    PHP:
    $data json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' $twitchfield), true);
    (Note the true at the end)

    That will return the decoded data as a PHP array. This may not be accurate because I can't see the source data, but usage then would be something similar to:
    PHP:
    $viewer[0] = $data['channel_count'];
     
  5. rivacom

    rivacom Active Member

    Very odd, I know the decode gets a huge array of data and I'm actually only pulling 1 field currently. The script works fine when I run it without the cron, it inserts data into the database fine. I'll try with the True to see if it makes a difference.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    It's optional. I'm not saying it's part of the problem, but personally I find the output easier to work with.

    But at the times the cron is running, that json_decode is not returning what you expect.

    I would recommend putting those debug lines in and run the cron manually.
     
  7. rivacom

    rivacom Active Member

    Put the dump in a few different spots with no luck. Doesn't dump any data.

    If I try your pure PHP array, I get this new error.

    ErrorException: Undefined index: channel_count

    Code:
    #0 /home/revenantgaming/public_html/code/scripts/testsql.php(28): XenForo_Application::handlePhpError(8, 'Undefined index...', '/home/revenantg...', 28, Array)
    #1 /home/revenantgaming/public_html/library/TwitchStream/cron.php(8): include('/home/revenantg...')
    #2 [internal function]: TwitchStream_cron::getUpdate(Array)
    #3 /home/revenantgaming/public_html/library/XenForo/Model/Cron.php(356): call_user_func(Array, Array)
    #4 /home/revenantgaming/public_html/library/XenForo/Deferred/Cron.php(24): XenForo_Model_Cron->runEntry(Array)
    #5 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(243): XenForo_Deferred_Cron->execute(Array, Array, 9.9999988079071, '')
    #6 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(369): XenForo_Model_Deferred->runDeferred(Array, 9.9999988079071, '', false)
    #7 /home/revenantgaming/public_html/library/XenForo/Model/Deferred.php(322): XenForo_Model_Deferred->_runInternal(Array, NULL, '', false)
    #8 /home/revenantgaming/public_html/deferred.php(18): XenForo_Model_Deferred->run(false)
    #9 {main}
    Code:
    array(3) {
      ["url"] => string(38) "http://revenantgaming.com/deferred.php"
      ["_GET"] => array(0) {
      }
      ["_POST"] => array(4) {
        ["_xfRequestUri"] => string(10) "/admin.php"
        ["_xfNoRedirect"] => string(1) "1"
        ["_xfToken"] => string(8) "********"
        ["_xfResponseType"] => string(4) "json"
      }
    }
    
     
  8. rivacom

    rivacom Active Member

  9. Chris D

    Chris D XenForo Developer Staff Member

    It would be:
    PHP:
    $viewer[0] = $data[0]['channel_count'];
    But, again, it's not necessarily going to solve the problem. I was just offering it as an alternative.

    When you did the debug code did you follow it with die();
    ?

    Without that the script would probably finish executing without giving you the chance to see its output.
     
  10. rivacom

    rivacom Active Member

    *NM

    Dump returned the data

    rivacomrevenantgamingpravenmajerebrotatoe235BSquare172timmeh1080pthetrueazraelmissedtvtest

    Which is data being taken from the select statement. Basically every twitch name from a custom field(if the user has it filled out).
     
    Last edited: Mar 19, 2014
  11. rivacom

    rivacom Active Member

    Anyone else have any advice? Still stuck on this.
     

Share This Page