How to run cleanup code after sendRequest() called?

5 years ago

theo
Member | 50
+
0
-

Is there any way to cleanup temporary files/directories after sending response?

I have application which create temporary directory with some files (for example PDF file) in processing of request. I need to cleanup whole temporary directory after file is sent. I thought that shutdown() phase/method should be the right place for doing such thing, but this method is called before data are actualy send via sendResponse() method. After sendResponse() there's no way to call some cleanup code and I cannot delete temporary directory before sendResponse() method call, because file to be send is stored in it (also I cannot move it elsewhere for same reason). This application could send from hundred to thousands files per day and therefore volume of this temporary directories/files could be huge (lets say in scale of GB).

I can delete it periodicaly from cron daemon of course, but this brings other troubles like, how recognize already downloaded files from files still being actively downloaded (I know, that this is not a problem on unix-like systems, where files are removed only after they are closed from all processes, but I think this is not clean solution).

Can someone give me advice, or show me some way to do this in clearly?

Thank you!

5 years ago

theo
Member | 50
+
0
-

Ok, I'm an idiot, because solution is really simple :) Use of register_shutdown_function() solves this problem, and I cannot reallize why I cant remember to this. Hope this should be usefull for other dumbheads like me in future ;)

5 years ago

Quinix
Member | 108
+
+1
-

You can also add callback to Application::onShutdown – https://api.nette.org/…ion.php.html#37 :)

5 years ago

Majkl578
Moderator | 1379
+
0
-

Quinix wrote:

You can also add callback to Application::onShutdown – https://api.nette.org/…ion.php.html#37 :)

This.

5 years ago

theo
Member | 50
+
0
-

Quinix wrote:

You can also add callback to Application::onShutdown – https://api.nette.org/…ion.php.html#37 :)

Aah, that is exactly what I was searching for. Thank you :)

4 years ago

amik
Member | 124
+
0
-

Hey, I'm just solving the same issue and came into the same solution – register Application::onShutdown(). However, I think this is a non-elegant solution as many FileResponses are of a teporary file that can be deleted after.
Do you think that it would be useful to add some “deleteAfterSending” option to FileResponse? Please give opinions if this makes sense or not to you.