iPXE discussion forum

Full Version: iPXE PHP LOOPs not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

I'm trying to get a php loop going ...

To save some time, i'll just describe the beginning of the script:
- server boots php script which determines its serial number (serversn)
- it connects to the database and checks if the server is there and what its (job) value is.
- if the server isn't in the database it is added to it with a default job value of 00

then the following script is where I'm having issues:

Code:
        do
        {
        print "echo Job 00 found, looping through ..."."\n";
        $result = mysqli_query($con,"SELECT * FROM servers WHERE sn='$serversn'");    //pull out details of the server
            while($row = mysqli_fetch_array($result))                //loop to pull out all rows from the database
            {
            $serial=$row['sn'];                            //grab server serial number
            $desc=$row['model'];                            //grab server model
            $job=$row['job'];                            //grab server job
            print "echo Current value of serial = ".$serial."\n";
            print "echo Current value of job = ".$job."\n";
            print "sleep 10"."\n";
            }        
        } while ($job==00);

        if ($job==01)
        {
        print "echo Job 01 found, HELLO"."\n";
        print "sleep 5"."\n";
        }

What happens is when i chain load the the script using http, it loads but says:
Code:
http://192.168.0.1/test2.php?serversn=VMware-56blabla... 100%
then it goes to
Code:
http://192.168.0.1/test2.php?serversn=VMware-56blabla... 99%
then again to:
Code:
http://192.168.0.1/test2.php?serversn=VMware-56blabla... 100%

It keeps going like that for a while, approx 60 seconds, then the script actually executes and the output is as follows:

Code:
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00
Job 00 found, looping through ...
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00

However, while the code is actually running and producing results, it is NOT connecting to the database. So when i go to the database and update the value of JOB to 01, the output is still:

Code:
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00

What is happening is while the code is stuck at loading 99%, 100%, 99% its actually connecting to the database then and pulling out results multiple times.

Does anyone know how to fix this ?
Why are the results being pulled from the database before the php script has a chance to run ?
Why isn't the database contacted while the script is executing like it should ?
What's even more interesting is while the code is stuck at 99% 100% 99%
and i go to the database and change the job value to 01 from 00, the code executes immediately, but the results are still INCORRECT, they are still being displayed for job 00 rather than job 01

Code:
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00
Job 00 found, looping through ...
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00
Job 00 found, looping through ...
Current value of serial = VMware-56
Current value of job = 00

If however, the job in the database is already 01 and the script is executed, the correct output is produced:

Code:
Job 01 found, HELLO

This is absolutely doing my head in.
(2014-07-04 13:30)jhonny Wrote: [ -> ]
Code:
        $result = mysqli_query($con,"SELECT * FROM servers WHERE sn='$serversn'");

Nothing in that code will cause a row with job=00 to be returned before a row with job=01. You may wish to add an "ORDER BY" clause to your query.

Michael
Actually,
the value for JOB is returned based on the server's serial number which is the (serversn) variable.

that's why in the select statement i have:

Code:
WHERE sn='$serversn'

because in my database i have the following structure:

Code:
sn        | model           | job
=================================
VMware-56 | VMware Virtu... | 00

Basically what i want to achieve with the script is ....

While job = 00 then keep looping and checking the database for value of JOB
If job = 01 then run a script or command
(2014-07-04 13:54)jhonny Wrote: [ -> ]Basically what i want to achieve with the script is ....

While job = 00 then keep looping and checking the database for value of JOB
If job = 01 then run a script or command

OK. Your PHP script will keep hammering the database in a tight loop, generating a potentially endless script for iPXE to execute. iPXE won't actually start executing the script until it has been downloaded in full, and even if it were executing the "sleep 10" commands generated by your PHP script, that wouldn't stop your PHP script from running its loop as fast as possible. Consider the fact that you could fetch the same URL with a web browser (which knows nothing about the iPXE scripting language), and the behaviour of your PHP script may become more obvious.

What you probably want to do is have a PHP script which doesn't loop at all: it returns a single instance of a script which causes iPXE to sleep and then retry the request.

Michael
Yes, the script was supposed to pause for 10 seconds and then check the database again to see if JOB = anything other than 00.

What this would do is put the server in a "wait" position where i could then use a WEB front end to update the JOB parameter for that server and it would execute a particular task.

How would I go about making iPXE sleep and try again ?
Are you able to give me an example of some code Michael ?
I would really really appreciate it.
Ok i got it.

So basically i broke the script up into 2 php files.

The 2nd php file now loops through itself by chain loading itself if job is 00
If job is anything other than 00 it performs the necessary actions.

Thanks for pointing me in the right direction Michael. Really appreciate it.

So now the problem i have is ...

When the file loops through itself for the 14th time it crashes.
This is consistent on both physical and virtual servers. Always on the 14th retry:

The error i get is:

Launching JOBS ..
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)
Could not boot: Error 0x00000001 (http://ipxe.org/00000001)

and then the server restarts
(2014-07-04 14:37)jhonny Wrote: [ -> ]So now the problem i have is ...

When the file loops through itself for the 14th time it crashes.
This is consistent on both physical and virtual servers. Always on the 14th retry:

Use the (undocumented, sorry) "--autofree" and "--replace" options to the chain command. You can abbreviate this to "-ar".

The problem you are hitting is that iPXE is running out of its very limited stack space. Using --replace will cause the new script to completely replace the current script (similar to the exec() syscall).

Michael
That did it !!!!
Mikey you are a genius !

Thanks so much for your help Big Grin
Reference URL's