AIM/MOTIVATION
I want to write a service, which should run all the time. But it should not be possible to start the service again while it is already running.
USE CASE
User X opens the page myService.php and starts the service by clicking a button on the page. After that closes the browser. After a while an another user opens the browser and tries to start the service. But the service should be started only one time.
(SEMI) PSEUDO CODE
<?php
ignore_user_abort(TRUE);
set_time_limit(0);
$sleepTime = 60;
while(1){
if( serviceAlreadyStarted() ){
echo "Service has already been started.";
exit;
}
if( shouldServiceBeStopped() ){
echo "Service has been stopped.";
exit;
}
// DO SOMETHING
sleep($sleepTime);
}
?>
EXPECTED BEHAVIOUR
On the second call for switching on the service again, the user gets the message immediately.
ACTUAL BEHAVIOUR
The second call of the script gives no response until the first call exited by the second condition shouldServiceBeStopped()