Jump to content

How to check condition again and again until it satisfy ?


ankur0101

Recommended Posts

Hello,

I am making a full backup of cPanel account.

It takes few seconds for zipping 4 to 10 MB of data but when there are more than 200 MB of data on cpanel account, obviously it will take some more time, say for example 2 minutes.

 

On cpanel based servers, while zipping, it temporarily creates a ZIP file of 33KB

And this file's size stays at 33KB until the process get completed i.e. till it completes a backup file

 

 

 

So conditionCHK is >> If example.zip > 33KB

 

then, backup ZIP file is ready

 

else

sleep(15) and check conditionCHK again

 

Point is that, it should check condition again and again until it satisfy.

So in my case, it script will check again and again whether example.zip has become more than 33KB or not.

If it not, then sleep for 15 seconds and again check

Once condition will satisfy, it will show "Success"

 

Which loop can I use here ? I am totally confused in developing a logic.

Thanks.

Link to comment
Share on other sites

I'm no expert on this, but a recursive function would do the task you need.

 

ie

function conditionCHK(){

if (example.zip > 33 ){
$result= "done";
}
else
{
sleep(15);
conditionCHK();
}
return $result;
}

As I said, I'm no expert so you will need to check the syntax and maybe read up on recursive function, but I's say this is probably your best bet.

 

 

 

Link to comment
Share on other sites

Why do you not want to create a function? 

 

You're going to be performing the same routine possibly hundreds of times depending on your processing speed.  This is one of the main reasons for using functions. 

You could (theoretically) nest hundreds of If/else statements to acheive the same thing, but why bother when you can reuse the same code you write once?

Link to comment
Share on other sites

Sounds like this is a job for the while loop ;)

 

<?php

// somewhat psuedo-code
$i = 0;
while(filesize('file.zip') == 33kb)
{
  // Add a timeout, otherwise you'll end up in an endless loop
  if($i > 1000)
    break;
  $i++;

  // sleeeep!
  sleep(15);
}

 

Link to comment
Share on other sites

Using a while loop is ok, except if the processing speed is incredibly slow for some reason, $i will hit 1000 and the loop will stop without the procedure completing.

 

The correct way to do this is with a recursive function which will go forever until the conditions are met.

 

As I said earlier, the problem sounds as though it s with the code in the script, not the functionality of using a recursive function.  Until ankur0101 posts some code and/or errors we're all using guesswork as to how to help him.

Link to comment
Share on other sites

Using a while loop is ok, except if the processing speed is incredibly slow for some reason, $i will hit 1000 and the loop will stop without the procedure completing.

 

The correct way to do this is with a recursive function which will go forever until the conditions are met.

You can easily remove the failsafe, but I would not really recommend it. The reason for adding it is so that you don't have 5 scripts that will never end because there was an error on the cpanel processing side for whatever reason. The failsafe does not just have to break out of the loop and that's that, it could also call an error logger or something along those lines.

Link to comment
Share on other sites

The correct way to do this is with a recursive function which will go forever until the conditions are met.

 

And if the script takes a long time to process (or errors) your recursive function is going to annihilate your stack by making it grow too large, thus causing php to crash.

 

No, the correct way to handle this is with a simple loop.  Also, given the sleep(15) you'll be waiting 4 hours before you hit the $i > 1000 failsafe.

 

Link to comment
Share on other sites

Sounds like this is a job for the while loop ;)

 

<?php

// somewhat psuedo-code
$i = 0;
while(filesize('file.zip') == 33kb)
{
  // Add a timeout, otherwise you'll end up in an endless loop
  if($i > 1000)
    break;
  $i++;

  // sleeeep!
  sleep(15);
}

 

 

This thought came in my mind, we are limiting attempts by counter.

This is not exactly what I want but this will work

 

Thanks

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.