0

I have a webshop, user order's will be saved on mysql and it will be process later in batch by "processor.php", it will be processed later because the requirement to send this order to other server is using a chat application

processor.php is a jabber bot, php bot that will be running 24/7 and it will check at db every 5 minutes and process the orders (send it to another bots, and update the orders based on the other bots reply)

I'am wondering if there is a way so processor.php can have a notification like "hey there is a new order, process it" from mysql every time a user submit a new order, something like a real time

  • 1
    setup a trigger. – Funk Forty Niner Apr 12 '18 at 18:38
  • 1
    Possible duplicate of [Invoking a PHP script from a MySQL trigger](https://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger) – adprocas Apr 12 '18 at 18:39
  • 1
    My suggestion would be to do this in another way. You have the web shop. Why not do the stuff that "processor.php" is doing after an order is saved? Almost every ecommerce solution includes event/observer type hooks to achieve this. It fully depends on what you are trying to achieve and what your setup is like. Why do you want it to work this way? – adprocas Apr 12 '18 at 18:41
  • processor.php is a jabber bot, a chat between two or more bots – Dhiva Banyu Wigara Apr 12 '18 at 18:46
  • @DhivaBanyuWigara, does that change the requirements of your question? Maybe it would be a good idea to include those details in your question? I still fail to see how setting up a hook in your order code to let the "processor.php" file know that a new order has been placed isn't a viable solution. Can you maybe update your question to include these things? I'll post some links and suggestions that I post to newcomers. As I said, this is seeming more and more like an architecture question, not a programming specific question. – adprocas Apr 12 '18 at 18:55
  • [Please, do more research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) then **post what you've tried** with a **clear explanation of what isn't** working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Read [How to Ask a good question](https://stackoverflow.com/help/how-to-ask). Be sure to [take the tour](https://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/questions/347937/im-new-to-stack-overflow-what-are-some-things-i-should-do-and-what-things-wil). – adprocas Apr 12 '18 at 18:56
  • I'll add more details on the questions – Dhiva Banyu Wigara Apr 12 '18 at 18:57
  • Dhiva, my answer below offers a few different solutions. Let me know if any of those would work for you. I'm also wondering what language your web shop is in – Nerdi.org Apr 12 '18 at 19:06
  • I'll check it first, and thanks for your answer @Nerdi.org – Dhiva Banyu Wigara Apr 12 '18 at 19:07

1 Answers1

1

The best you can do is edit the file that INSERTS the data in the first place.

You can use $query->insert_id; or a myqsli equivalent if you need the ID of the new order

You can then pass this ID to the processor. Believe me, relying on crons can become a nightmare and with payments and orders I highly recommend avoiding a cron solution.

That said, if your processor only changes things in the DB itself and nothing else you can use internal MySQL triggers!

https://www.siteground.com/kb/mysql-triggers-use/

But if your processor does non-DB work you will have to either:

Use your cron

Set up the order insert to then also trigger the processor on it's own without using a cron.

Set up a function that looks to see if order is ready for processing on a page a user visits, so that their order can begin processing without any delay.

You can use AJAX or even PHP to call processor.php. you can even pass it a specific ID. Essentially any basic method like file get contents, $.get(), or any other programming function (in any language) will let you call processor.php and have that file run on your web server. You can do this from any language that supports requesting a file from the same or different server (so most).

I highly recommend making sure you set up proper permissions and even a basic API so only YOUR server(s) and service(s) can trigger the processor!

Nerdi.org
  • 895
  • 6
  • 13
  • why do you say "crons can become a nightmare"? it's often the best option - especially with orders where you don't want a potential hangup in your hook to bring issues in to the order creation. with cron you've got something that's completely isolated and can't possible interfere with the script doing the insert. – But those new buttons though.. Apr 12 '18 at 19:09
  • Cron should be used to look for orders that failed to process. It shouldn't be the main thing to start processing an order. :) – Nerdi.org Apr 12 '18 at 22:24