Posted by Pedro Sanchez on Nov 30, 2017

An excuse to use React PHP

A while ago, I wondered if it was possible to write asynchronous code using PHP. After some research I found a library called ReactPHP.

After watching the videos, reading the documentation, reviewing examples and writing some sample code using the library, I struggled to think of something useful that I could create with it… then one day, staring at my screen, I had a very stupid idea about how it could be very helpful.

Here at MyBuilder, most of the time we build the PHP applications we use then upload it to the development server. We have our own helpers and applications that make it easier, However, they took a considerable amount of time to build everything: the Symfony config, generating the propel models, updating dependencies, etc.. and building the JS apps (yarn dependencies, etc…).

It took around 2 minutes to prepare the environment because everything ran in one after another. We need to do this a lot… well, you get the idea.

Then the stupid idea came…

Why not run all of these in parallel? That would be a improvement, right?

My initial idea was to use NodeJS… I could write a very simple module to do that. However, I was still looking for an excuse to use ReactPHP, so I said to myself… why not!

To be honest, I wasn’t convinced that it would work. I felt that it was kinda stupid to use PHP to do something that could be achieved very easily (I guess) with NodeJS.

Anyway I wanted to use ReactPHP, So i gave a try, and with the help of my colleagues here at MyBuilder, I built a new tool to do just that: The Async Command Executor, ACE.

Setting up ACE

Follow the setup instructions on the ACE to set it up and create a default ~/.ace/config.yml

How we use it

The basic idea of ACE is that you define a set of commands within a group. All commands within the group are then executed in parallel.

You can have multiple command groups, only one group of commands is executed at a time. After a group is finished the next group, in order, will be executed in the same maner.

For our case, we have three groups:

  1. First group executes commands to build all the Symfony applications in parallel.
  2. Second group executes commands to build all the JS applications in parallel.
  3. Third group executes the command to upload the sources to the development server.

In this way the overall process of building all our Symfony and JS apps is, on average, a 75% faster.

Other uses

Actually you can use ACE as a quick solution to achieve something similar described above. Sometimes I use it to run the PHPUnit tests in parallel without the need of installing and configuring PHPUnit extension paratest. But as I said… I just use it as a quick alternative.

I sometimes also use it to execute the find command. Rather than doing…

$ find -E ~/ -regex ".*\.(jpg|gif|png|jpeg)"

…I can create a group and do all of these at the same time:

  • find -E ~/Desktop -regex ".*\.(jpg|gif|png|jpeg)"
  • find -E ~/Downloads -regex ".*\.(jpg|gif|png|jpeg)"
  • find -E ~/Documents -regex ".*\.(jpg|gif|png|jpeg)"

Can you think on another use for it? Let us know in the comments!

Happy coding ❤️

Jobs at MyBuilder

We need an experienced software engineer who loves their craft and wants to share their hard-earned knowledge.

View vacancies
comments powered by Disqus