views:

74

answers:

3

Hi there,

I know this is a bit generic, but I'm sure you'll understand my explanation. Here is the situation:


The following code is executed every 10 minutes. Variable "var_x" is always read/written to an external text file when its refereed to.

if ( var_x != 1 )
{
   var_x = 1;
   //
   // here is where the main body of the script is.
   // it can take hours to completely execute.
   //
   var_x = 0;
}
else
{
   // exit script as it's already running.
}


The problem is: if I simulate a hardware failure (do a hard reset when the script is executing) then the main script logic will never execute again because "var_x" will always be "1". (I already have logic to work out the restore point).

Thanks.

A: 

It sounds like you're doing some kind of manual semaphore for process management.

Rather than writing to a file, perhaps you should use an environment variable instead. That way, in the event of failure, your script will not have a closed semaphore when you restore.

Jweede
from PHP.net: `Warning: These directives have only effect when safe-mode itself is enabled!`
scragar
true, scragar's answer is much better for PHP. I was only thinking in general.
Jweede
+5  A: 

Don't you think this would be better solved using file locks? (When the reset occurs file locks are reset as well)

http://php.net/flock

scragar
Off the top of my head (thinking about it) - perfect.
Ben
You beat me while I wrote the code ;)
Residuum
+6  A: 

You should lock and unlock files with flock:

$fp = fopen($your_file);
if (flock($fp, LOCK_EX)) { )
{
   //
   // here is where the main body of the script is.
   // it can take hours to completely execute.
   //
    flock($fp, LOCK_UN);
}
else
{
   // exit script as it's already running.
}

Edit:

As flock seems not to work correctly on Windows machines, you have to resort to other solutions. From the top of my head an idea for a possible solution:

Instead of writing 1 to var_x, write the process ID retrieved via getmypid. When a new instance of the script reads the file, it should then lookup for a running process with this ID, and if the process is a PHP script. Of course, this can still go wrong, as there is the possibility of another PHP script obtaining the same PID after a hardware failure, so the solution is far from optimal.

Residuum
+1 for your effort
Dooltaz
another +1. Thanks for the effort mate.
Ben
Can't vote :( not enough rep.
Ben
For some reason Flock doesn't seem to work for me. Flock seems to only delay the code until the file becomes free which isn't what I want.
Ben
Cheetah: Maybe we misunderstand each other: I thought, you only write $var_x=1 in the file to avoid a second instance of the script to start from running. When you lock the file (instead of writing in it), you can check for a lock on the file to confirm if the script is still running. This is what my code does, you do not have to write $var_x anywhere.
Residuum
I don't think you understand what I am saying either :).Yeah I totally agree with what you said and I am no longer writing any variables. But on my server, if a file is locked and you query whether it is locked in an if statement like what you posted above, then the script will stop (might be sleeping) until the file is unlocked before it evaluates the if statement.After some googling, it appears that this is something todo with my server setup.
Ben
See here: http://www.infoqu.com/dev/php-development/php-flock-function-211101-1/
Ben