eagor
eagor

Reputation: 10035

Upstart node.js app on dokku deployment (Digital Ocean)

I deployed my node.js app to Digital Ocean, using dokku (Docker powered mini-Heroku). The app is started by command in Procfile (web: node app.js).
How do I start it with Upstart, so that it restarts automatically after crashing?

Added:
I need it to be upstarted when I deploy with git push dokku master.

Upvotes: 1

Views: 792

Answers (2)

nanobar
nanobar

Reputation: 66355

As of Dokku 0.7.0 it has restart policies built in:

https://github.com/dokku/dokku/blob/master/docs/deployment/process-management.md#restart-policies

e.g.

# only restart it on Docker restart if it was not manually stopped
dokku ps:set-restart-policy node-js-app unless-stopped

Upvotes: 0

Daniel
Daniel

Reputation: 38791

Edit the file /etc/init/node.conf and put the following code in. Change /opt/node_project/ to the path of your project. You'll need to be root when editing this file so open your editor with sudo.

description "Node server"
author "eagor"


# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [016]

# Automatically restart process if crashed
respawn


script
  echo $$ > /var/run/node.pid;
  exec node /opt/node_project/app.js
end script


post-stop script
  rm -f /var/run/node.pid
end script

Now that you've created an Upstart config for your process you can start it from the command line:

$ sudo service node start

Upstart will monitor your process and will restart it any time it goes down.

It also redirects logs to /var/log/upstart/node.log.

Forever

The above works directly with node and would bypass Dokku. Seems like Upstart isn't the best way to handle this.

You should consider using the forever module. Add forever to your package.json dependencies. Then in your Procfile use this: web: ./node_modules/forever/bin/forever app.js

Upvotes: 1

Related Questions