processlimitupstartmemory-consumption

Setting memory consumption limits with Upstart


I've recently become quite fond of Upstart. Previously I've been using God, Monit and Bluepill but I don't really like these solutions so I'm giving Upstart a try.

I've been using the Foreman gem to generate some basic Upstart configuration files for my processes in /etc/init. However, these generated files only handle the respawning of a crashed process. I was wondering whether it's possible to tell Upstart to restart a process that's consuming for example > 150mb of memory, as you would with Monit, God or Bluepill.

I read through the Upstart docs and this looks like the thing I'm looking for. Though I have no clue how to config something like this.

What I basically want is quite simple. I want to restart my web process if the memory usage is > 150mb ram. These are the files I have:

|-- myapp-web-1.conf
|-- myapp-web-2.conf
|-- myapp-web-3.conf
|-- myapp-web.conf
|-- myapp.conf

And their contents are:

myapp.conf

pre-start script

bash << "EOF"
  mkdir -p /var/log/myapp
  chown -R deployer /var/log/myapp
EOF

end script

myapp-web.conf

start on starting myapp
stop on stopping myapp

myapp-web-1.conf / myapp-web-2.conf / myapp-web-3.conf

start on starting myapp-web
stop on stopping myapp-web
respawn

exec su - deployer -c 'cd /var/applications/releases/20110607140607; cd myapp && bundle exec unicorn -p $PORT >> /var/log/myapp/web-1.log 2>&1'

Any help much appreciated!


Solution

  • Appending this to the end of myapp-web-*.conf will cause any allocation calls trying to allocate more than 150mb of memory to return ENOMEM:

    limit rss 157286400 157286400
    

    The process might crash at this point, or it might not. That's up to the process!

    Here's a test for this in the Upstart Source.