Jump to content

Can I automatically restart a script if it errors out?


zep

Recommended Posts

I have a script that hits a soap service, and imports data.  I plan on storing the position of the script in a database and checking it on the start of script, and resuming where it left off.  I would think that I could call another instance of the script and execute it on error, but I dont know how I would go about executing an action after the script fails. ??? 

 

Is this posssible?

 

Thanks.

Link to comment
Share on other sites

I have a script that hits a soap service, and imports data.  I plan on storing the position of the script in a database and checking it on the start of script, and resuming where it left off.  I would think that I could call another instance of the script and execute it on error, but I dont know how I would go about executing an action after the script fails. ??? 

 

Is this posssible?

 

Thanks.

 

the only way i can think of is making it object oriented and putting the "damage control/error" parts in the class destructor method :)

Link to comment
Share on other sites

Hey guys thanks for the ideas, is there any way you could suggest a little piece of code, or the function / method to have a custom "damage control/error"

 

Im ok at php, but not as masterful as many, any help you can lend to kind of  push me to the solution would be huge

 

Thanks!

Link to comment
Share on other sites

I have a script that hits a soap service, and imports data.  I plan on storing the position of the script in a database and checking it on the start of script, and resuming where it left off.  I would think that I could call another instance of the script and execute it on error, but I dont know how I would go about executing an action after the script fails. ??? 

 

Is this posssible?

 

Thanks.

 

the only way i can think of is making it object oriented and putting the "damage control/error" parts in the class destructor method :)

 

oh man im so pissed off right now  >:( >:(

i just spent at least 40 mins researching and posting the best and fullest example for you dude and ff crashed and actually did recover the tabs but not the form content  ???

 

so sorry im just gonna have to point you in the right direction as opposed to giving you a head start...

 

Read the documentation first to get a general idea:

 

phpdoc references:

serialize()

unserialize()

Magic __sleep() and __wakeup() functions (read serialize() and unserialize() to understand it better)

Magic __destruc() function

 

now read this article:

The Basics of Serializing Objects in PHP

 

basically what i meant in my previous post was that you should use try-catch blocks (see Exceptions) to surround code ts prone to generating fatal errors or even warnings (use of @ is discouraged and apparently negatively impacts performance)

 

so basically you just need to add a few methods to your class (or transform your procedural code to a class)  :)

 

function __construct();

accept and assign/process any parameters if applicable and then call init() to initialize the class

* reason why you want the actual initialization routine separate from the constructor is because you're gonna need to call it from __wakeup()

 

function init();

try to call connectDB(); and:

- if it returns true try to call checkDB();

- if it returns false assume "restart script" (just means your variables are untouched, no need to instantiate the class again for example)

 

- try to call connectSOAP(); and:

- if it returns true "GOTO (*)" :P

- if it returns false serialize "everything" (the entire class) and write to the $_SESSION

 

function __destruct();

try to call updateDB(), catch Exceptions (log them?) and write from the $_SESSION variable containing the serialized class info to a file (as per the tuturorial)

 

function updateDB(); (return bool)

try to update the database, catch Exceptions (log them?) and serialize everything to a $_SESSION variable

 

function connectDB(); (return bool)

try to connect to database, catch Exceptions (log them?) and serialize everythingto a $_SESSION variable

 

function checkDB(); (return bool)

if connected to database try to check whether you're done reading info from the webservice  then "GOTO (**)"

and return true if done and false if not.

 

function __sleep(); (return array)

this one's a freebie:

foreach($this as $key => $val){
     $serializeMe[$key] = $val;
}
return $serializeMe;

 

function __wakeup();

- do the opposite as __sleep(); ie: foreach $arrayname assign the value to the corresponding class variable

- try to call connectDB(); and:

-- if true try to call updateDB();

-- else do nothing

 

 

ok so there's a few missing things and this is far from perfect but hopefully you got the idea right?

 

NOTES: (*) and (**) are actually related.  you are going to have to define a parameter to know where you stopped and where to resume from... serialization is a good way of preserving the state, specially if you have DB issues, but you will lose all open connections/streams (such as a web-service or database) and I cant help you cause I have no context!!

 

Off the top of my head, if its indexed items maybe keep track of the index.  if its something more complex you're gonna have to decide what you need to know to decide where to resume from!

 

hope you got the idea! :)

 

Alex

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.