Create queue in php

PHP Queue system

Solution: Assuming your table has a PK ( ) and references a to identify which request belongs to which user and assuming there can only be a single request in the queue per user then your query would look something like the following. Put all orders into a queue (database, flat file, whatever) and have a worker take orders from the queue one by one, process them, update the result.

PHP Queue system

I need to build a queue system and came across this topic How to Build a PHP Queue System.

When the queue listener is running, this works like a charm. But once in a while, the queue listener might crash or stopped and I need to restart it. Not an issue.

The issue that exists is that in the meantime, jobs are added to the queue. But those jobs are not executed as soon when the listener is started again. The queue is empty. How can I take care that if the queue listener is not running, jobs keep being queue and processed when the queue listener is started?

 $queue = array(); //////// setup our named pipe //////// $pipefile = '/storage/queueserver'; umask(0); if(!file_exists($pipefile)) < if(!posix_mkfifo($pipefile,0666)) < die('unable to create named pipe'); >> $pipe = fopen($pipefile,'r+'); if(!$pipe) die('unable to open the named pipe'); stream_set_blocking($pipe,false); //////// process the queue //////// while(1) < while($input = trim(fgets($pipe))) < stream_set_blocking($pipe,false); $queue[] = $input; >$job = current($queue); $jobkey = key($queue); if($job) < echo 'processing job ', $job, PHP_EOL; process($job); next($queue); unset($job,$queue[$jobkey]); >else < echo 'no jobs to do - waiting. ', PHP_EOL; stream_set_blocking($pipe,true); >> 

Code to add something to the queue

$pipefile = '/storage/queueserver'; $fhp = fopen($pipefile, 'r+') or die ("can't open file $pipefile"); fwrite($fhp, "GenerateLabel|". date('H:i:s')."\n"); 

I assume now that each time your script is launched, the file is created again with the posix_mkfifo function.

Читайте также:  Java поменять знак числа

Can you confirm that by adding to your script :

umask(0); if(!file_exists($pipefile)) < if(!posix_mkfifo($pipefile,0666)) < die('unable to create named pipe'); >else < echo "Pipe created"; >> 

Then launch it a first time. It will echo. Then exit it, and relaunch. If it still echoes, that means that the problem come from the fact that PHP doesn’t see the file already exists (maybe from the permission of the file ?)

Data structures — Arrays as Queues in PHP, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Dynamic Transaction Queuing System using PHP

Watch this demo video of the Dynamic Transaction Queuing System that I made. It is created using PHP . Download the source code …

Create a queue system

I would like to create a queue system that works in this way:

  • A user fills in a form where they will have to enter some data.
  • Click on Send and these data will be saved in a SQL table.
  • Going to the index.php page will see a box containing a text like this: There are 4 requests in front of you, please wait a few minutes.

I have already tried to do such a thing, but going to create new requests the number «4» of the message grows. This is because I created a query that counts all the results on the table.

$query = $mysql->query(«SELECT COUNT(*) AS q FROM application_approve»);

While I want it to count only the results above the request that sent the user.

id name text text2 1 First request dassasad dsadasas 2 Second request dassasad dsadasas 3 Third request dsadasdsas dsadasad 

In the example above I would like to count only how many lines there are above the «Second Request»: in this case 1.

Assuming your table has a PK ( id ) and references a user_id to identify which request belongs to which user and assuming there can only be a single request in the queue per user then your query would look something like the following.

SELECT COUNT(id) AS q FROM application_approve WHERE id < ( SELECT id FROM application_approve WHERE user_id = ? ) 

This also assumes the PK id is an auto-incrementing key.

Given the user_id this query would return the number of rows above the given user's row ( assuming they have one ). Or, in other words, all ids less than the id of the given user.

For simplicity, let's assume this schema only has 2 columns ( id and user_id ):

mysql> SELECT * FROM application_approve; +------+---------+ | id | user_id | +------+---------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+---------+ 3 rows in set (0.00 sec)

So in the given table, there are 3 users, each with 1 entry in the queue.

If we wanted to find which position user 2 is in the query would give us the following result:

mysql> SELECT COUNT(id) AS q FROM application_approve WHERE id < (SELECT id FROM application_approve WHERE user_id = 2); +---+ | q | +---+ | 1 | +---+ 1 row in set (0.00 sec)

PHP - Use queue and cron job or directly process the, The advantage of the queue is being able to give a faster response to the user, so although the SMS doesn't get sent faster, the users will perceive …

PHP Queue System with Codeigniter. HOW?

I am creating a website for a client where they need the ability to generate 25,000 - 100,000 barcodes. I have licensed a software that generates the codes but it takes around 20 - 30 minutes to generate an order of say. 60,000 barcodes.

Would it be possible to use the set time limit function in php set to zero (no time limit) and then pass the variables to the library with Ajax and use the ignore_user_abort function. This way essentially this is what would happen:

Customer chooses barcode qty and type, they check out and after the payment gateway sends back a response of SUCCESSFUL, then we run an ajax request that sets the time limit to UNLIMITED and then tells the script to run even if the user closes their browser or exits the page. This way the server processes the order and it doesn't matter if it takes 2 seconds or 45 minutes. When the order is done the client can refresh their account and they would see their order.

Am I just dreaming to think that this sounds like a good idea or would it be a viable solution? I don't have any experience in my years of coding with php queue systems, so I don't want to have to create one unless I absolutely have to.

I would look at using something like Gearman ( http://gearman.org/index.php ).

This would also be separate to the CI app . CI will then use Gearman to process a queue.

Don't depend on one long-running PHP instance. Especially if it's tied to the user/browser in any way. Properly separate the backend system from ephemeral processes like web server responses.

Make a proper queue-worker system. Put all orders into a queue (database, flat file, whatever) and have a worker take orders from the queue one by one, process them, update the result. The worker can be a cron job, a daemon process, an endless looping PHP script (which is restarted by a monitor if it crashes) or whatever else. You can also look into AMQP, ZeroMQ or similar queuing/messaging systems.

Try using CLI at backend, it doesn't have timelimit by default.. http://php.net/manual/en/info.configuration.php#ini.max-execution-time

First save the ORDER at your database..

Second. Set crontask to run each minute, to take and proccess 1 order. When order is ready (after 30minutes) update the database with download links or what ever you return as responde and end the crontasks.

Third. After his order is ready show to user at some page the result.

Implementing a simple queue with PHP and MySQL?, A different approach would be to have some kind of master script fetch some rows (like SELECT LIMIT 5, for example) and then start an …

How can one generate a queue system for php based web application in Windows for multiple requests/jobs?

I want each form submission of my php application to run in a queue as each request takes considerable time and resources. I found out about Laravel which is kind of complete system for such tasks but unfortunately is Linux specific.
Additionally the email support is also needed to retrieve the jobs by their id.

What might be the existing tools for windows based php applications?
If not, how to achieve this manually using mysql and php?

Few hits were found on this e.g. here but not actually getting from where to start.

If I understand you correctly, then RabbitMQ should be what you're after.

It supports Windows, but you'll need to write some code to get it to do what you want.

PHP queue fail safe, Currently have a queue system setup using something like amazon's queue service. Right now I am using supervisor to keep the workers from …

Источник

Create a Simple Queue System in PHP

Hello friends, in todays article, we will create a fun little project. We will Create a Simple Queue System in PHP just to give you the idea how a Queue System works. So lets get started.

First of all, what is a queue system? We all know that, a queue is a simple line for any service. In real life, let’s say you aree in a line for coffee. The barista serves one by one “First Come, First Serve” basis. In programming world we do the same for various purposes. Like you want to send mail to 1000 customers. So you can prepare a queue system to send mail one by one. It can be run in background as a separate job.

To implement a basic queue in PHP, we can use an array and utilize array functions to add and remove elements. Here’s an example implementation:

class Queue < protected $queue; public function __construct() < $this->queue = []; > public function enqueue($item) < array_push($this->queue, $item); > public function dequeue() < if ($this->isEmpty()) < return null; >return array_shift($this->queue); > public function isEmpty() < return empty($this->queue); > public function size() < return count($this->queue); > >

This example defines a `Queue` class with the following methods:

– `enqueue($item)`: this method adds an item to the end of the queue.
– `dequeue()`: this method removes and returns the item from the front of the queue.
– `isEmpty()`: checks if the queue is empty.
– `size()`: this method returns the size of the queue.

Below is the implementation or usage of the `Queue` class:

$queue = new Queue(); $queue->enqueue('Item 1'); $queue->enqueue('Item 2'); $queue->enqueue('Item 3'); echo $queue->dequeue(); // Output: Item 1 echo $queue->dequeue(); // Output: Item 2 echo $queue->size(); // Output: 1 echo $queue->isEmpty() ? 'Queue is empty' : 'Queue is not empty'; // Output: Queue is not empty

For more clarification, This demonstrates how to add items to the queue using `enqueue()` and remove items from the front of the queue using `dequeue()`. It also shows how to check the size of the queue using `size()` and whether the queue is empty using `isEmpty()`.

Keep in mind that this is a simple implementation of a queue using an array. For more complex scenarios or performance optimizations, you might consider using other data structures or exploring built-in PHP queue implementations like `SplQueue`.

That’s all for today. Home you liked this little article to give you a simple idea regarding “Queue“. See you in some other day!

Check Out More Resources:

#php #guide #queue

Источник

Оцените статью