0

I am facing a problem that somehow I don't see the solution to it. I have a XML file that needs to be importet to custom DB structure, when user uploads / imports the file the ajax post is waiting untill the file import is finished, but this could take 5 hours or more I don't know. What is the best way to handle this UI issue.

  • I was thinkg about thread uplaod, to split the file in multiple parts and upload each with it's own thread (pthreads, having problems with instalation on centos 7 / PHP 7)

  • Or if there is any other way that I could import the file in the background and whenerever the user refreshes the page there would be a status log output so that user would know when the import is finished and if successful.

6
  • The upload takes 5 hours? Or the server-side database processing does? Commented Jun 28, 2017 at 15:04
  • use a background job, and email them when it's done. Commented Jun 28, 2017 at 15:13
  • @AlexHowansky Yes the server side database processing takes long. Commented Jun 28, 2017 at 19:35
  • @ArtisticPhoenix do you have like any example that I could take a look at. Commented Jun 28, 2017 at 19:35
  • @olyar - have you ever setup a cron job for PHP? Commented Jun 29, 2017 at 4:42

1 Answer 1

1

You would want to run them using a background job ( a detached process ) this way the end user gets a confirmation message right away, and then send an email when the long running task is complete. Then they don't have to wait for it to finish. As I mentioned in the comments I have a class I wrote on my git hub for this

https://github.com/ArtisticPhoenix/MISC/blob/master/BgProcess.php

But it passes the args as a path because it's setup for Code Igniter, so you would have to change that or split the arguments up within your code.

Anyway the basics is similar to running a cron job, This varies in the implantation depending on the OS of the server. But on Linux the command is like this

 php -f "path/to/phpfile.php" "{args}" > /dev/null &

The > /dev/null & part sends the output to null ( throws it away ) and the & runs it as a non-blocking process meaning the script starting the command can continue on. So using an example as this

 .. other code before starting background job ..
 exec( 'php -f "path/to/phpfile/xmlProcessor.php" "testXML/2" > /dev/null &');
  .. code to tell user job is started .. this runs right after the call without waiting for that process to finish.

Then in xmlProcessor.php you would have this

  <?php

         $args = explode('/', $argv[1]);
         $file = $ags[0];
         $user_id = $args[1];
         ... code to process xml 
          ... email user confirmation of completion

http://php.net/manual/en/reserved.variables.argv.php

As I said typically would call it this way,

  exec( 'php -f "path/to/phpfile/xmlProcessor.php" "testXML" "2" > /dev/null &');

And access them using

   $argv[1] // = testXML
   $argv[2] // = 2

But because I use this with CI, it does it's routing for me to a special controller and handles all that. The nice thing about my class is that it should find the PHP executable under most cases, and it has windows compatibility built in ( which was a pain in the ...)

Using that class you would just call it like this

 $command = new BgProcess( "path/to/phpfile/xmlProcessor.php", "testXML", 2);

 echo $command;

Would output 'php -f "path/to/phpfile/xmlProcessor.php" "testXML/2" > /dev/null &' after starting the process ( the return is just for debugging )

Basically your running a separate background job with PHP via the command line.

Sign up to request clarification or add additional context in comments.

11 Comments

This looks nice, I'm actualy working in CodeIgniter, and I've never used a cron job ;(
This is different then a cron job in that you are not using cron, but cron lets you run a background process at an timed interval. So it's no the same, but experience coding for cron jobs or any CLI ( command line interface ) type scripts would be useful.
in CI, it would route this to something like php -f "path/to/CI/index.php" "contoller/method/arg1/arg2.." But I have a custom router so I can use multiple controller files in sub-folders ( welcome.php )
Also for CLI scripts I use a seperate index.php that is outside of the webroot, so it's not accessible from the browser.
I'm also still using CI version 2 for this, our application is about 4 years old now, and it's to much to refactor it for CI 3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.