Initial data:

  1. Shop on Joomla
  2. Parser written in php

Task: a button is created in the admin panel that should launch the parser. Since the parsing takes a long time (several hours), you need to run it in the background so that the browser can be closed. When you click on the button from the admin panel again, you should throw on the page with the indicator, they say, the process is already running, wait.

Ideally, if the crown is already running, then the indicator should also display this.

How to do it all?

    4 answers 4

    You need to store this indicator of the running process somewhere.

    In the * nix environment, it is customary to simply create a file, for example, "parser.pid", in which a single number is written - the number of the running process. When the process is completed, it deletes its pid file with the last action.

    You can also, running the parser, create a file in a designated place in which to write the process id - you can get the id directly in php via getmypid() .

    In the admin panel, see if there is such a file, and it would be good to check whether the process is alive with this id: through the execution of the shell command, or, if the POSIX functions are not disabled , posix_kill( $pid, 0) . The latter sends a zero signal, which does nothing, to the process. F-I will return true if it was possible to send, otherwise false .

    So from any place you can find out if the process is alive. And then show, run, etc. according to the logic of your application.

    • Thank you, read. Suppose I deal with the indicator. And actually run in the background how to implement? - Philipp Klemeshov pm
    • Answer please! - Philipp Klemeshov
    • For example, you can set the flag "must be run" (create the file "parser.start" again), and the task launched every 5 minutes after the crown will look for such a flag, and if there is one, start the process and delete the flag file. - Sergiks

    I can offer the following option: create a separate table in the database, which will display the fact of starting the parser and a certain indicator of the execution of parsing, which will be updated in the process. Accordingly, when starting the parser (by button or crown), the flag is set in the database. The next time you press the button, it checks whether the parser is already running. If so, the indicator is simply displayed. Upon completion of the parsing flag is removed.

      Look towards the PHP function ignore_user_abort

      Put the parser in the loop, set the sleep loop (the right time), activate ignore_user_abort and even when you close the browser, the parser will still work until the script itself stops or PHP crashes and you don’t need any crones!

      To write the result, you can create a .txt file, open it for writing and reading and write the result of the script there, as well as lock the file for re-writing and you can easily understand that the parser is working and take the corresponding result from there. Well, this is what came to my head, I think, if you put your head around well, you can find a better solution.

        You can use $_SESSION to create a variable. Then use it like a "mutex" for your display.