Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Check if the operation succeeded
  • Hi, I have a script which parses some XML files (one by one), takes the info's an put them in variables. I have to insert the info's in the database, using a regular \DB::insert command.
    For example :
    <code>
    $insert_event = \DB::insert('timefeeds')
    ->set(array(
    'initiator' => 1,
    'context_type' => 1,
    'context_id' => $match_id,
    'target_type' => 'team',
    'target_id' => $team_id,
    'event' => 2,
    'content' => json_encode($content_mini),
    ));
    </code> Normally, if I want to execute the query I'll put the <code>->execute()</code> at the end of the statement, BUT because the scripts takes a large amount of files (through a cronjob) an launch this script for each of them, I want to check if the operation succeeded for a file or not. If the operation succeeded, I will move the file to an archive folder, else, I want to move the file to a 'problem' folder, and let the script continue with the rest of the files. So practically, I need something like this (but it doesn't work) : <code>
    $insert_event = \DB::insert('timefeeds')
    ->set(array(
    'initiator' => 1,
    'context_type' => 1,
    'context_id' => $match_id,
    'target_type' => 'team',
    'target_id' => $team_id,
    'event' => 2,
    'content' => json_encode($content_mini),
    )); if( $insert_event->execute() )
    {
    ... //command for moving the file to the ARCHIVE folder ...
    }
    else
    {
    ... //command for moving the file to the PROBLEM folder ...
    }
    </code> Please help me, because now my script stops, when he didn't succeed to parse a file, so I remain, with a lot of unparsed files and of course with a lot of information's out of my database. Thks,
    gm
  • Why does your script stop?
  • Hi Harro, I have a foreach for all the files needed to be parsed. If an error occurred, while the script tried to save the info in the database, it doesn't go forward with other files, but stops, because it can't insert the info's in the database. So I need to check if the insert operation is possible, and if yes continue with the insert and move the file to the archive, else I have to move it to a problem folder, and continue with the next file. Thks
    Gabriel
  • I understand that, hence my question. "an error"? What error? A PHP error? An exception? Which one? Some errors can be caught by a try/catch block, but not all.
  • Hi Harro, I'll try to explain the entire process, in order to have the entire image. I run a task through a cronjob, which checks on every 15 secs a folder for new files. If founds some new files, it select them all, rename (unique names) and then move them to another folder (a temp folder). From here, the script creates a file in a separate folder (running.txt, which tells the script that he has files to parse, so don't push some more, until this file is deleted) , then launch a static method which takes through a foreach every file, and based on the name of the files launch another static method to parse the file >>> because every type of file must be parsed by a different method. The file is an .xml file. The script, parse the xml, takes the data he needed, and insert the details in the database. In this point, if something goes wrong , for example : Error - 2012-08-17 13:43:34 --> Error - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Anz' in 'order clause' with query: "SELECT ....
    2) Error - 2012-08-31 19:30:31 --> 8 - Trying to get property of non-object in /Volumes/.......
    3) Error - 2012-06-09 14:07:14 --> 8 - Undefined variable: title in /Volumes/.....
    4) etc... the script stops, and all the files TO BE parsed, are not anymore parsed. Practically, I want to try inserting the data in the DB and if doesn't work to move the file and let the script continue with parsing the others. Now the script stops when it doesn't succeed to insert the data in the database, but doesn't move the file in a 'problem' folder, in order to give free way to the others. On every 15 secs, the cronjob, checks if I have the 'running.txt' file, and of course that the file exists, no new files are passed to be parsed. If everything goes well, after the last file, the script erase the 'running.txt' file, so new files are ready to come. I hope that all the information's will help you to give me a solution. Thanks
    Gabriel
  • Have you tried what I asked, and put the processing of the file in a try/catch block?
  • It shoul look something like this ? <code>
    try
    {
    $insert_event = \DB::insert('timefeeds')
    ->set(array(
    'initiator' => 1,
    'context_type' => 1,
    'context_id' => $match_id,
    'target_type' => 'team',
    'target_id' => $team_id,
    'event' => 2,
    'content' => json_encode($content_mini),
    ))->execute();
    }
    catch (\Database_Exception $e)
    {
    \Cli::write("Message error : $e->getMessage()" , 'red'); \Log::error("Unable to insert the event in the database for the matchid : $match_id. Message error : $e->getMessage() "); ... operations for moving the file to the problem folder ...
    }
    </code> Thks.
  • If it's a database exception, yes. You could catch \Exception instead, and then dump/log $e to see what kind of exception it was. Not sure the DB layer only throws Database_Exception.
  • So, in order to be sure that I catch everything, I'll better replace the \Database_Exception with \Exception. But the rest is fine ? I mean, this is the place (in the catch block) where I should move the file, if the operation failed, correct ? Thks,
    Gabriel

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion