'Your job is running' script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadnad
Forum Newbie
Posts: 1
Joined: Fri Sep 09, 2011 1:59 am

'Your job is running' script

Post by nadnad »

Visitors to my website will upload file to a simple upload.html:

Code: Select all

<form action="execute.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit"" />
</form>
As you can see, the arguments are posted to execute.php. In this, I have lines that look like this:

Code: Select all

$ob_file = fopen("../../log.txt",'w') or die ("cannot create log.txt");
      
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}


ob_start('ob_file_callback');
system ("../../sourceShell") or die ("sourceShell fail") ;

ob_end_flush();
execute.php runs a shell script called 'sourceShell', which runs a fortran program on the server. execute.php outputs the 'log' of this program (that typically would appear on the terminal) on log.txt. I use the ob_file_callback function to do this.

When a visitor clicks 'submit' (or upon the completion of uploading), I need execute.php to echo "Your job is running..please wait". And then I want to refresh the page every 5 mins until the job finishes and the result will be shown. My problem is that, when a visitor clicks 'submit', it waits until the fortran program completely finishes (which takes a long time) before it outputs the "Your job is running..please wait". I suspect this is because of the ob_file_callback function. If I don't use that function, it seems like the log prints in real time to my browser as the program runs.

I do hope this make sense... and I've spent days on this wanting to tear off my hair, so it's not like I haven't googled hard. I'm completely new to web programming, so I hope you guys understand. Thank you in advance.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: 'Your job is running' script

Post by twinedev »

What I would do is something along these lines:

File finished uploading, make a unique identifier, something like $strIdent = session_id().getmypid().time();

Create a temp file named with this with an unlikely uploaded extention (ex .waitstate) Save a indicator into the file (ex. just a 0 (zero)).
Copy the uploaded file to to appropriate place, also named this with the original extension

Redirect to the "waiting.php" script (what says "your request is processing), pass it the $strIdent

On that page, look for the temp file, if not there, kick back to upload page.

If it is there, check its value, if it is zero,
....execute the system call, but make sure you redirect output to a file
....system ("../../sourceShell > ".$strIdent.".output") or die ("sourceShell fail") ;
....I have never had need for it, but according to the manual, redirecting the output lets script continue without waiting for SYSTEM call to finish.
....IMPORTANT, make sure the script that runs in the background will change $strIden.'waitstate' file to have a value of 2 (finished)
....Now set the value of $strIdent.'waitstate' file to be 1 (indicate it was told to run now, so next call to this page doesn't re-run it)

If its value is 2, the system call is done, finish cleaning up (get rid of temp files, etc... if you need the output, it is in $strIdent.'output')

For everything but a value of 2 (finished):
Output the "Waiting" message, done with a META REFRESH header to refresh the page after so long.

So that is the steps I would try, If I knew specifics I might do something else, but would try that.

-Greg
Post Reply