1

I want to continue the execution of the script after closing the connection.

Here is what i tried ..

log_message('debug','Test started' );

//Show all errors   
error_reporting(E_ALL); 

//Configurations 
@ini_set("output_buffering", "Off");
//@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);

//User abort & maximum time
ignore_user_abort(true);
set_time_limit(0);

//check the level       
if (ob_get_level() == 0) {
    ob_start();
}

//Actual output
echo('hello world ...'); 

// get buffer length        
$size = ob_get_length(); 

// set content length
header("Content-Length:$size"); 

//close the connection 
header("Connection:close");  

// content encoding 
header("Content-Encoding: none"); 

//content type
header("Content-Type: text/html; charset=utf-8"); 

//Release buffer
ob_flush();
ob_end_flush();  
flush(); 

// Continue on background
// never gonna execute from here ...
sleep(20); 

//There is no aliens (class) exists , expecting error on log
$alien = new alienEncoder(); 

$alien->get_aliens( new alienDecoder() );

log_message('debug','End Test'); 

Everything works well execpt the CONTINUE part ( it stops after FLUSH), There is no error log on server.

The same code works in another host, but not here.

Please help.

SERVER : IIS 7.5 Shared, Using Codeigniter

Notes

  1. There is no alienEncoder() or something like that, so i am expecting some error log on server, but there is no error
  2. ResponceBufferLimit is set to zero
  3. using CGI/FastCgi
Red
  • 6,230
  • 12
  • 65
  • 112

2 Answers2

0

PHP-FPM has a function especially for this purpose, fastcgi_finish_request()

Though I have no idea how to run it with IIS

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

Php is coupled with apache request system. It is started when the request is received When it finish, the request is closed.

Flush just send a partial responce to the client, but does not terminate this responce, so the browser will still hang, waiting for more content. It will consume bandwidth, apache and php ressources needlessy. Also I have to say it's a pretty bad pratice.

The best way to do computation intensive task without slowing the client is to use a defered task system. Personnaly I recommend resque (google it!). It enable you to add your action to a queue, then a worker will take all queued task and compute them.

For a cloud-hosted and almost free option that work, look at iron mq (from iron.io). They work the same way (you add a task to the queue, then the queue call a configurable url that will trigger the task computation)

Atrakeur
  • 4,126
  • 3
  • 17
  • 22
  • Also i dont think that the browser will hang after sending the proper headers. – Red Aug 07 '13 at 11:19
  • Read me again, I don't have mentionned cron (scheduling system), I've mentionned resque (task system) Also for shared host where you don't have all options available, I've mentionned iron.io, which is a cloud solution allready hosted which will work with any environement as long as you have a public url available to call. – Atrakeur Aug 07 '13 at 11:58
  • I understand. We are actually doing the same thing, an external app is used to POLL the script. In fact i dont have something to echo back to the app, but i dont want the app to wait until the full execution time and thats why we are closing the connection. and this works well with other server ( in linux,windows ) but not in the new host. – Red Aug 07 '13 at 12:10
  • I understand that on a shared host, this solution isn't possible. But with iron.io, it is their service that call you website to perform the task. So the polling and the echo to your app is done on their side. Again, your solution is a bad practice. It's time to use a real solution created especially for what you want to acheive instead of putting crappy things together and hope it works in the end. – Atrakeur Aug 07 '13 at 13:13
  • Confused !! Where i put the source code ? my server or their server ? – Red Aug 07 '13 at 13:18
  • Your server send task info to their server. Their server process task one by one in the queue. Foreach task, it calls your server with the task info. It's up to your server to do what it want with this task! So your server execute the task then report it as complete. More info: http://dev.iron.io/mq/reference/push_queues/ – Atrakeur Aug 07 '13 at 13:57