How to do it...

Fuge needs a simple configuration file in order to take control of our development system, let's write it now.

We need to create a directory called fuge at the same level as our webapp and service directories:

$ cd micro
$ mkdir fuge

Next we need to create a file fuge.yml in this directory and add the following configuration code:

fuge_global:
tail: true
monitor: true
monitor_excludes:
- '**/node_modules/**'
- '**/.git/**'
- '**/*.log'
adderservice:
type: process
path: ../adderservice
run: 'node service.js'
ports:
- main=8080
webapp:
type: process
path: ../webapp
run: 'npm start'
ports:
- main=3000

Fuge will provide us with an execution shell for our apps and services.

We can enter the fuge shell environment with the following screenshot:

$ fuge shell fuge.yml 

Fuge will read this configuration file and provide a Command Prompt, as shown in the following screenshot:

Type help and hit return to see the list of available commands, the following screenshot shows the help output:

Let's try the ps command:

This shows us a list of managed processes based on the earlier configuration.

We can see that fuge understands that it's managing our webapp and our adderservice. Let's start both with the start all command (still in the Fuge shell):

Once we issue the start all command Fuge will spin up an instance of each managed process and trace STDOUT and STDERR output from these processes into the shell console, coloring the output on a per process basis.

We can now point our browser to http://localhost:3000/add and the system should work exactly as in the previous recipe.

Let's now make a change to our adderservice code, say by adding some additional logging. We'll add a console.log statement to our respond function, so that our service code looks as follows:

const restify = require('restify')

function respond (req, res, next) {
const result = (parseInt(req.params.first, 10) +
parseInt(req.params.second, 10)).toString()
console.log('adding numbers!')
res.send(result)
next()
}

const server = restify.createServer()
server.get('/add/:first/:second', respond)

server.listen(8080, () => {
console.log('%s listening at %s', server.name, server.url)
})

If we now go back to the Fuge shell we can see that the change was detected and our adderservice has been restarted automatically. If we add some numbers through the webapp interface we can also see that our new console.log statement is displayed in the Fuge shell. The following screenshot shows us loading the web app, the adderservice restarting in response to our change, and then a new log message occurring after we fill out the HTML form and hit submit:

Now we'll shut down our system by issuing the stop all command in the Fuge shell. Fuge will stop all managed processes. We can check that this has completed successfully by issuing a ps command.

We can now exit the Fuge shell by typing exit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset