Queue Jobs for long operations in OctoberCMS

Last time I wrote about Agent Portal that we created for one of our customers.The next project from the series was a much harder task: create fully custom Reseller Portal with such features like tariff editing, customers management, CDR and online payments.

As long as Agents are quite limited (they can create clients and recharge clients and their profits are percentage of their recharges), resellers, from the other hand, are quite powerful entities. They get a rates list from switch owner with base prices and create apropriately higher tariffs - their profit is the difference between these two and depends on how much clients call.

Production tariff has about 30000 - 40000 rows. Reseller must be able to copy these rows easily, change their values by percentage, by fixed values and so on. These are operations that can take several minutes and cannot be done with a normal request.That's where we used Queue Jobs.

Let me share two classes from the Reseller plugin here. Base logic is that we get information from the Frontend Component, send them to RatesOperationsService class, which in turn creates Job and uses JobManager to dispatch it. Our October instance uses Redis as a queue engine and queue listener is hhvm artisan queue:listen command dispatched using supervisor app, which allows us to easily manage the number of required queue listener processes.

Example Job

Open in new tab

In the job above, we take a page of rates from the source tariff, go trough it, replace necessary data (right now only id_tariff, but we will add more stuff here in future) and insert it to the database.

JobManager

Open in new tab

JobManager is responsible for dispatching the Job to Queue and for job information management. With it, we were able to create another component, showing these information to the frontend client.

How it works? Take a look!