Service
An interface to an application service deployment
Overview
Service:
An interface to an application service deployment
Intent
- Define an object that encapsulates the lifecycle of a software service.
- Abstract the startup and shutdown methods to control the runtime state of a service.
Problem
We want to control the runtime state of application services, but each service must be controlled through proprietary or operating specific interfaces, and often must be invoked differently depending on application environment context.
Discussion
While the Deployment type provides a standard interface and properties to manage the life cycle of a software configuration, it does not assume the software has runtime state. For example, imagine that following the installation and configuration of a Deployment, a file must be executed to start a long running process to handle requests. One would need to know how to check if the process was already running to avoid starting multiple processes which may cause undesirable conflicts. Likewise, if one had to shut down the service, one might need to consider what to do if the normal stop operation fails.
When one considers the common details and requirements for services they can be organized into different categories:
- parameters: these are key attributes that describe the service
- state management: these are the sequence of procedures needed to start, stop and check status of the service.
Because service implementations vary widely and do not present a consistent management interface, the administrator must often possess either intimate knowledge of the application code or operating system features to control a service's runtime state.

Compounding the need to have a consistent approach to starting and stopping a wide variety of services, the administrator must also adjust for environmental differences; varying command line options or running commands in different directories.
A preferred option would be to define an abstraction that builds on the Deployment type which already includes support for coordinating the installation and configuration of a software component but to add to that, standard strategies for managing the runtime state of a service.
Using this model, a generic logical skeleton for starting up and shutting down a service can be embodied yet the details for interfacing with the underlying service executables and or operating system can be provided by classes derived from the generic abstraction.

This approach is particularly useful because it separates the variant and invariant behavior, minimizing the amount of code to be written.
Structure
The Service type inherits the capabilities and relationships from Deployment incorporating startup and shutdown logic to the deployment update sequence.

The essential service properties are defined as the following parameters:
| Name | Description |
|---|---|
| startup-rank | A value specifying a level relative to other services. |
Example
The Service type defines an object that provides methods to manage the runtime state and deployment of a software service.
The diagram below describes the startup of an Apache web server managed via the Service abstraction. One can begin integrating the Service's procedural interfaces using the provided apachectl command. For example, startService can be defined as apachectl start and stopService as apachectl stop. But one might find themselves in the situation where they are unable to use the standard apachectl command due to the way Apache was installed or must be invoked. In these cases, the Service runtime state management commands can be defined to meet the specific needs yet the administrator can still rely on the standard management interfaces: Stop, Start and Status

Check List
- Identify the procedures for starting and stopping the application service
- Define the assertServiceIsUp, assertServiceIsDown, startService and stopService commands using the identified procedures.
Rules of Thumb
Design
- Super Type
- Deployment
| Role | Concrete. (Objects can be created.) |
| Instance Names | Unique |
| Notification | false |
| Template Directory | |
| Data View | Children, proximity: 1 |
| Logger Name |
Commands
The service type provides the following standard life cycle commands:
| Name | Description |
|---|---|
| Start | Starts the service if the service is down. |
| Status | Checks if service is running |
| Stop | Stops the service if the service is up. |
| Update | Executes the Service deployment lifecycle. |
The service type provides the following hook commands called by life cycle commands:
| Name | Description |
|---|---|
| assertServiceIsDown | Checks if the service is down and fails if is not. Service subclasses provide a concrete implementation. |
| assertServiceIsUp | Checks if the services is running and fails if not. Service subclasses provide a concrete impementation |
| startService | Starts the application service |
| stopService | Stops the application service |
The commands, assertServiceIsUp and assertServiceIsDown, are each examples of commands that test an assumption that is intended to be true. They act as an assertion statement inside command workflows and if the condition for which they test is not met, they exit with a failure. This causes the command sequence in the workflow to fail, the failure forwarded to the workflow's error handler.
Prepare
builds the deployment objects
The Prepare command defines a workflow that prepares the Service for Update.
- Usage
- Prepare
Workflow
- Install
Start
start the service process
Service provides the concrete implementation supporting the generic start up algorithm which first checks if the service is up and if not, to then run the startService method.

- Usage
- Start
Workflow
Error Handler
| Command |
Status
status the service
Service provides the concrete implementation supporting the generic status algorithm which first checks if the service is running, and if not, to fail.
- Usage
- Status
Workflow
Stop
stops the service process
Service provides the concrete implementation supporting the generic shutdown algorithm which first checks if the service is down, and if not, to then run the stopService method.

The standard Service algorithm to shut down a service can be maintained inspite of exceptional scenarios particular to a service. The graphic below describes the logic for shutting down a Jakarta Tomcat service. In this example, the stopService command is changed into a command workflow which first attempts to stop tomcat by sending the SHUTDOWN message to the control port. If that fails, it then invokes the runShutdownScript command which then attempts to stop the service by calling the tomcat shutdown script.

One can imagine how they can use this pattern to extend components of the startup and shutdown workflow to incorporate special logic yet still work within the basic logical skelaton provided by Service.
- Usage
- Stop
Workflow
Error Handler
| Command |
Update
run the update cycle
Service provides the concrete implementation supporting the generic service deployment lifecycle. It overrides the Deployment Update command workflow and adds to it, Stop and Start at the beginning and end of the sequence.

- Usage
- Update
Workflow
assertServiceIsDown
checks if process is down
The assertServiceIsDown command is a hook command overriden by subtypes providing a service specific check to determine if the service is not running.
- Usage
- assertServiceIsDown
| Execution | bash |
| Arguments | echo checking down status && { echo DOWN ; } || { exit 1 ; } |
assertServiceIsUp
checks if process is running
The assertServiceIsUp command is a hook command overriden by subtypes providing a service specific check to determine if the service is running.
- Usage
- assertServiceIsUp
| Execution | bash |
| Arguments | echo checking up status && { echo UP ; } || { exit 1 ; } |
startService
start the service process
The startService command is a hook command overriden by subtypes providing a service specific procedure to bring the service into a running state.
- Usage
- startService
| Execution | bash |
| Arguments | echo start |
stopService
stops the service process
The stopService command is a hook command overriden by subtypes providing a service specific procedure to bring the service into a stopped state.
- Usage
- stopService
| Execution | bash |
| Arguments | echo stopping the service |


