Hello
I'm about to implement a queue of jobs in my application. Any information, suggestions and ideas are welcome :)
I'm using Phalcon 3.0.3 and PHP 7.0.14 (Debian).
First of all: I'm totally new on job queueing.
I read the docs: https://docs.phalcon.io/en/latest/reference/queue.html
My application is multi-module:
- 2 web applications
- An API application
- CLI application with Tasks for cronjobs
All in the same project, so they can share the same Models and libraries (they're all part of the same thing).
Let's say I need to process videos (like in the Phalcon example): now this is done by the VideoTask
in the CLI application. Every minute the CLI application runs video_task process
and that Action checks if there is any video ready to be processed and processes it.
I'd like to transform this behaviour in a queue-oriented way. How can I implement this inside my application?
1. Every time a user uploads a video, I have to run this:
$queue->put(
[
"processVideo" => 4871,
]
);
Now, we are inside a Controller in one of the Web modules of the application.
- Everything all right till here?
2. There is a script somewhere that implements the while()
in which I execute the job "processVideo"
- Is this script running "forever"? I mean, is it a PHP script that runs an infinite cycle?
- I need to have all my application Models, libraries, etc., ready to use inside this script: where do I have to put this script? Is it a new "part" of my application together with the "CLI", "API", and "Web" ones?
- How can I make sure that it starts at system startup? How can I check if it's running at a given time? What if it crashes or shuts down? How can I run it again?
3. Have a look at the example:
while (($job = $queue->peekReady()) !== false) {
$message = $job->getBody();
var_dump($message);
$job->delete();
}
- Is this condition:
$queue->peekReady() !== false
alwaystrue
? If not (I don't think) do I have to put the above code inside awhile( true ) {}
cycle?
4. Load balancing
Let's say I want to make sure that the server doesn't process more than 3 videos every 10 minutes. So, I have processed a video at 2:23, another one at 2:25 and a third one at 2:26, if I receive a new job at 2:28 I want the Queue
to wait until 2:33 before executing that job (and so all the others that arrive).
- Is it possible to implement this behaviour? How (do you have any docs link I can read)?
Thank you very much for your help.