Manage and push applications with multiple processes in Cloud Foundry!

That’s right! The introduction of processes in Cloud Foundry API V3 allows application developers to define different process types for their applications. In other words, this feature simplifies the cf push experience by removing the need of maintaining different code repositories and managing multiple applications in Cloud Foundry.

Does this mean my application container shares multiple processes?

No! Each process runs in its own container and can be scaled individually. It is like having two or more applications but with the advantage of managing them with a single push command from a single source repository.

In this post we describe how to get started with processes and how manage them by modifying a nodejs web application adding a separate and simple worker process. So, let’s get started!

First, clone the repository:

git clone https://github.com/swisscom/cf-sample-app-nodejs

 

Prepare an App with Multiple Processes

The next step is to modify the application logic to include different type of processes it can execute; here we add a simple switch that checks if the process is either web or worker. The web process is the same as before and additionally our application has now a worker process that logs a message and sleep for 1 second (note that the sleep variable must be added at the top of the file).

src/index.js

As a new library is required, modify package.json and add the sleep package as a dependency:

package.json

Once you have defined the different processes of our application, we need to create a Procfile in the root directory of the application. In this Procfile we define each process and its start command.

Procfile

Push an App with Multiple Processes

Finally, the app can be pushed to Cloud Foundry! There are different ways of doing it, depending on the cli version you are currently using. Here we list both options currently available but be aware that cf7 is currently beta.

Please also note that this is currently a Beta feature in both CLI versions and the commands in this post may change.



cf v3-push my-multi-process-app
cf7 push my-multi-process-app

View Processes

As soon as the application is up and running you can see the two different type of processes running and their state.

cf app my-multi-process-app

Scale a Process

As show above our application has a web process running but no workers, let’s then scale the worker process and check the logs to see the new container spinning up and the worker in action:



cf v3-scale my-multi-process-app --process worker -i 2

cf7 scale my-multi-process-app --process worker -i 2

cf logs my-multi-process-app

Et Voilà! You can now enjoy and scale processes individually with your new multi process application in Cloud Foundry!

Further reading on application Procfile and processes:

 

You can find the modified source code used in this blog post at https://github.com/swisscom/cf-sample-app-nodejs/tree/multi-process-sample.