Parallel function execution in Bash script

Executing your code blocks in a Bash script can be vital to speed up your system. We are using a Bash script to update several applications and do some specific hacks when our load balancers spawn a new server. Some of these commands take a while, about a couple of minutes (such as installing Composer packages) and we would never want to waste so much time to init a server.

Thankfully, running multiple commands or command blocks in parallel is easier than you would imagine:

Look at this easy hack with parenthesis. You can wrap them with regular parenthesis and now we can use the ampersand operator to put this block of commands in the background. At the end of our multiple command blocks, we also issue wait  command to let the script know that we want to wait until all of the above blocks finish execution. Any other command after wait , such as the  echo  and  exit  lines will run after all of three blocks of commands finish execution.

Programmatically access to Codeception configuration

No tool is perfect. Codeception has its own flaws where you may need to interfere and do things manually. In my tests, for example, I wanted to dynamically create an SQL dump which is executed at the beginning of the test run. This dump creates the tables and sample data. However as database is a constantly changing component, I wanted to get it from my original internal API definition. I keep my database dump, procedures and test data in different SQL files in the internal API package. Before a test run, I wanted to create the SQL dump file Codeception wants dynamically.

However, I still wanted to use a bit of Codeception configuration such as the database name. Here is how I created my dynamic SQL dump file to be used by Codeception:

\Codeception\Configuration class provides a config() method which helps us access Codeception configuration. You could also use suiteSettings() method to access the configuration of a specific suite, such as in acceptance.suite.yml file.

git literally deleted my files

Imagine this scenario


my_test.php was in a folder named vendor  (like in “translation vendors”, in a path similar to application/views/vendor/my_test.php ). Apparently my .gitignore  file had this statement: /vendor . Git decided this statement means to stop following new files in any folder named “vendor”.


  1. Always have healthy, tested configurations, in your .gitignore in this specific case.
  2. Be careful with git file deletions. Never deploy to production without making sure nothing went wrong with a feature or hotfix merge.

Another error that you see once in a year: zend_mm_heap

We can’t really argue that we are experience a “memory hell” with PHP these days, as most of the maintenance is done by Zend. But times come when you encounter one of these little, random memory errors that you’ll usually never know when it will occur. This time, it is another segmentation fault,  zend_mm_heap .

As this bug report makes it clear, the issue is probably about OPCache failing to orchestrate properly its self-restart with PHP processes (whether in PHP-FPM, FastCGI (?) or mod_php).

As I encountered this error on the system for the first time after more than a year in the current structure, I can’t immediately say if this flag update on OPCache configuration fixed the issue:

The immediate fix when I first saw the error was to restart Apache.

Heroic error: MySQL server has gone away

Now that we actually started using PHP for long-running processes, we will need to read more about process management, persistent PDO connections,  how to keep things in good shape by keeping things consistent and healthy in a loooooong running environment. Like, years long. This is apparently a huge topic and I’ll try to touch upon it in various posts, but today we have a specific error: “MySQL server has gone away [MySQL Reference]”

A quick googling will give you an idea of what’s going on and how you can solve common problems that causes this error. However I could not solve my case simply before extensive debugging, even though others seemed to have this issue before and solved in a way or the other.

The generic solution to this kind of problems with PDO is to catch the PDOException  and reconnect to database server. You can do this by simply NULLifying your PDO instance and then creating a new one:

So when you encounter “MySQL server has gone away” error, you catch it and reconnect to your database server.

If you already tried this and similar methods and still can’t catch the error, there appears to be one more trick to overcome this sneaky error: silence the error and catch the exception. In a specific environment (PHP 5.5.29), PDO operations would first issue an error (old school PHP errors, not Exceptions) and then throw an Exception. I had to silence the errors with the error control operator  ( @ ); only then the program would be executed until to the point where PDO throws the exception:

tmpfile and file handle behavior on return

I spent a while to debug a tmpfile  issue where the file would get deleted before the execution ends or the resource is fclose ‘d.

Let’s see the sample code first:

Now when we call download()  method, it should return String  "This is going to fail." , I thought. It returned NULL  and $filePath  didn’t even exist at that point! After wasting some time to debug why it would NOT return what I wanted, it finally hit me, having read each line of manual very carefully: (this is right before I was getting ready to dive into PHP source code)

From tmpfile() documentation:

The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.

When I return $path at the end of the writeToFile(), there remains no reference to $fh Resource so it is released automatically resulting the deletion of my temporary file.

Solution: Return the file handle resource or take a look at tempnam(), which doesn’t delete your temporary file automatically.

Default variables in JavaScript functions

This is what we call default/optional parameters:

Here, we passed “this is my new title” string as $title to the new_post($,$,$) function. The function could parse 3 parameters, however, we only provided one parameter: $title. The other two parameters have default values, defined on between the paranthesis like $subtitle = “no subtitle”.

Unluckily, Javascript doesn’t have such a feature yet. If you don’t pass one of the arguments to the function in Javascript, it gives “undefined” type to the variable, which often breaks our code unless we catch them explicitly.

If you want to experiment with optional parameters drafted in EcmaScript 6, see MDN.

There are few widely mentioned options to implement this kind of behavior in JS. Let’s start with the one I find easiest and cleanest.

Use OR

We will  use “||” (OR) comparison operator to check if our variable is defined or undefined, and if undefined, we will assign a value to it.

This functions introduces someone to someone else :) We are not really interested his/her age, so we made this parameter optional. Let’s see what happens when we run it:

Normally, you would be more careful with such a function as you would only want to accept integer values for “age” variable.

Latest news

There is this sweet JavaScript library which allows you to manage your function variables in a better way. I have never used it because I just discovered it today.

It’s called ArgueJS and has several interesting features like default values, type checking (it doesn’t “cast” them as much as I see) and optional parameters. The library makes use of the “argument” variable of JavaScript function. “argument” is an implicit variable (an Array), containing all of the arguments passed to the function. This is the second of methods you can use for default value assignment on JS.

It seems like a perfect match for larger JS applications.

Use “arguments”

Let’s go on with the function above.

The function introduction() has no pre-defined variables to accept, but “arguments” variables contains every parameter passed to the function regardless if they were expected or not. In this method, we should always know the order of variables that should be passed to the function: index 0 would always be name and index 1 would always be age (unless if you make a workaround for a dynamic ordering). Let’s test:

That’s all. Use the one that makes the most use to you.

Laravel 4 – Custom Packages

When using Artisan to create your own custom package in Laravel 4, there is this very simple step that is never mentioned in the documentation.

You can follow this link to learn more about how to create a package in L4:

But. Just after you created your package with Artisan tool, you should go to your package directory (mostly /workbench/vendor/library/, or going on the example of Jason Lewis, /workbench/solitude/profile), and run:

This way, the package will install the dependencies (like laravel support) and add himself to the system. This is never mentioned anywhere and I struggled to find it out.

Thanks to:

Web application with any language/platform

Can a web application be made in C#, C++, Fortan, Pascal, etc?

My answer to the question from StackOverflow:

I believe you can use any platform/language for web development, as long as they provide necessary functionality.

A web process includes a few steps:

  1. Listen to a port to which client (browser, mostly) sends requests (which is :80 by default)
  2. Do something. Access to the DB, talk to somewhere. Enjoy yourself.
  3. Send an output back to the client

So, if you can make these kinds of specifications comfortable, then you are good to go with any language/platform.

Or you can build a bridge between a currently used web server (like Apache, Nginx, IIS and such) and your Pascal application. This is what most developers would go.

I am currently using PHP, Java Application through command line, Python through command line all together in a single application. I love the way command line works, simple and fast. And we will add another server with Windows to the application for a Windows-specific function. So… If you have enough ropes, you can tie anything together.

Correct me if I am wrong :)

Response now, process later

In a PHP application, I had some AJAX process going on. However, it too almost 6 seconds (I know, I know) to complete the request on the server. Client would have to wait 6 seconds to get a response and see the action completed. This was bad. Really bad.

In this AJAX request, I would process an action and then post to the Facebook wall of the user, through Facebook PHP SDK. The request to and waiting for response of Facebook was slowing the response to the client. Though, I already had the data to give the client back, before I interact with Facebook API.

So… I thought, why don’t I just give the required result back to the client instantly before interacting with Facebook which slows the response time. I could also put this time consuming action to a queue and process it in the background, but we can also do it with PHP buffer and flush stuff. See:

ob_end_clean() : clean current output… this function removes any output if you have given ob_start() command in previous lines. Most frameworks does. (I use CodeIgniter in this project). read more about this function here

ignore_user_abort() : Amazing function! The core of what we want to achieve. Normally, if the client receives a HTTP Response from the server, PHP stops execution. We don’t want it to stop, even after we provide response to the client. read more about this function here

ob_start() : This one turns of output buffering. I recommend reading more about this. You can do lots of tricky stuff with this cutie putie little bitty function. read more about this function here

header("Connection: close"); : Now we are setting HTTP response to the client. We do it by commanding header()  function in PHP. On this line, we say to the client (the requester) that this connection is finished and closed. You can let the user know the Ajax execution -or whatever- has finished. read more about this function here

header("Content-Length: " . ob_get_length()); : If the output has no size, it means null, right? It seems browsers are not that smart… We need to let them know the size of the content. ob_get_length()  makes it possible to get the size of content between ob_start()  and  ob_end_flush() . read more about ob_get_length() here, and header() here

ob_end_flush(); : Now… We are giving the client, which is often a browser, the response in buffer. What was it? See the line starting with echo ? Yep. This is our buffer between ob_start and this line (ob_end_flush()). There is a sweet concept here, between all these ob’s and flushes… Here are a couple of links to read more about them:

Basically, ob_end_flush()  function flushes (read as “printing the data in buffer”) the data between the previous ob_start()  and itself and frees the buffer (read as “removes the data, thus more empty space we have in buffer”). And we, then, should do flush() , because other components, such as a web server, may want to add something else to the response. read more about ob_end_flush() here, and flush() here.