Heroku Cloud Application Development
上QQ阅读APP看书,第一时间看更新

The Heroku process architecture

A Heroku application is a collection of processes that run on the Heroku dyno manifold. Whether you are running a local process or a remote one on a distributed cluster of machines, a Heroku application is essentially a set of processes, each consuming resources like a normal UNIX process.

To add flexibility and have better control over how you define your process configuration, Heroku defines the concept of process type. Each process that you run as a dyno can be classified as a web process or a worker process type depending on whether it handles HTTP requests or does some background processing. You could also define a custom process type to add flexibility to your application definition.

Procfile

Heroku has a language agnostic, application centric method of defining your process types through a configuration file called Procfile—a format for declaring the process types that describe how your app will run.

Procfile is a text file named Procfile placed in the root of your application that lists the process types in an application. Each process type is a declaration of a command that is executed when a process of that process type is executed. All the language and frameworks on the Cedar stack declare a web process type, which starts the application server.

Here is the content of a sample Procfile for a Sinatra application:

web: bundle exec rackup config.ru -p $PORT

The part before : defines the process type, and the part after : defines the command to launch the process type when the dyno running the process boots up.

One can use environment variables populated by Heroku in the command as shown in the preceding command.

Another example of a Procfile command could be:

web: sh path/bin/webapp

In this example, when the web process is started, it will launch the web application server located in path/bin/webapp in a new shell. Note how similar it looks to running a UNIX process on the command line.

Declaring process types

A process type declares its name and a command-line command—this is a prototype that can be instantiated in one or more running processes. Usually, the Procfile will be in the application root. In case it isn't supplied or created, Heroku by default creates a web process to run the default application.

The main purpose of a Procfile is to declare different process types and associate an action with each type of process. Essentially, it is a declarative specification of what the individual process types available for this application are and how instances of those process types should be started or operated when needed. The process types could represent different types of worker processes, a clock job, or a Node.js server that runs and processes incoming events. While declaring new process types, it is recommended that we distinguish between the process type for a transient (running for a small period of time) job and the process type for a long running job. It helps when you create instances of each of these process types to run. You can easily scale the transitory jobs independently of the long running ones

The Procfile format

The Procfile format is one process type per line, with each line containing <process type>: <command>.

The syntax is defined as follows:

  • <process type>: This is an alphanumeric string; it is a name for your process, such as web, worker, clock, or myproc
  • <command>: A command line to launch the process, such as sh path/bin/webapp

The web process type is special and is the only process type that will receive HTTP traffic from the routing mesh. Other process types can be named arbitrarily.

A sample Procfile

Here is a sample Procfile for a Node.js app with two process types:

  • web: This handles HTTP requests
  • worker: This runs background jobs;
    # Procfile
    
    web: node server.js
    worker: node bkgrnd.js
    

When you run the application that corresponds to this Procfile on Heroku, the platform detects the requested process types and boots them, as shown in the following screenshot:

A sample Procfile

Adding Procfile to Heroku

Procfile is not necessarily used to deploy applications. The platform can detect the type of application and the corresponding runtime to use. Though Heroku automatically creates a default web process type to boot the application server, it is recommended to create an explicit Procfile as that gives you a greater control on how your processes can execute in Heroku's execution environment. For Heroku to use your Procfile, add Procfile to the root of your application and then push it to Heroku, as shown in the following screenshot:

Adding Procfile to Heroku