

Running A Leiningen/Ring Webapp As A Daemon Via Upstart (Ubuntu)
source link: https://blog.jakubholy.net/2013/07/27/running-a-leiningenring-webapp-as-a-daemon-via-upstart-ubuntu/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Running A Leiningen/Ring Webapp As A Daemon Via Upstart (Ubuntu)
- Create an all-in one uberjar via "lein with-profile production ring uberjar" (using the lein-ring plugin; a simple lein uberjar would suffice for an app with a
main-
method) - Create an upstart <service name>.conf file in /etc/init/
- Run sudo start/stop/status <service name>
Example /etc/init/mongodiffer.conf
Example Upstart config file for the service mongodiffer:
## Upstart config file (use 'start mongodiffer', 'stop mongodiffer')
## Note: Stdout and stderr will be captured in /var/log/upstart/mongodiffer.log
## (aside of the native log in /var/log/mongodiffer.log)
author "Jakub Holy"
description "Start the MongoDiffer webapp on its default port (80)"
start on (local-filesystems and net-device-up IFACE!=lo)
# Note: "start on runlevel [2345]" would also do but I want to be explicit that
# running it w/o network is meaningless
# Must run as root to be able to run on port 80; ugly but quick
#setuid mongodiffer
#setgid mongodiffer
exec java -jar /srv/mongodiffer/clj-analytics-mongodiffer-standalone.jar
## TODO: Consider enabling respawning
# respawn
## Try to restart up to 10 times within 5 min:
# respawn limit 10 300
The only necessary "stanzas" are "start on" and "exec" (see also Upstart for Java apps). We could also enable respawning (both the stanzas) so that Upstart would try to start the service again if it crashes. (Of course Clojure service never crash ;-))
Logging
I use tools.logging with logback (ch.qos.logback/logback-classic) and its RollingFileAppender with a SizeAndTimeBasedFNATP to keep the logs to a reasonable size:
<configuration>
<appender name="FILE">
<file>/var/log/mongodiffer/mongodiffer.log</file>
<rollingPolicy>
<!-- rollover daily -->
<fileNamePattern>/var/log/mongodiffer/mongodiffer-%d{yyyy-MM}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy>
<!-- or whenever the file size reaches the size -->
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" /> <!-- Useful when running locally/dev -->
</root>
</configuration>
Stdout and stderr of the service is automatically captured in /var/log/upstart/mongodiffer.log via the default console log stanza. I haven't been able to find out what to do to make sure that it won't grow infinitely.
Serving static resources from a Ring uberjar
- Put the resources under a directory in resources, f.ex. resources/static/ (=> e.g. resources/static/js/zepto/zepto.min.js)
- Configure the wrap-resource Ring middleware: "(wrap-resource "static")"
- Refer to the resources using the path after static (e.g. "/js/zepto/zepto.min.js")
Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.
Allow me to write to you!
Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.
Recommend
-
11
Ansible Tutorial 02 - Configuring a Sidekiq Upstart Job on Ubuntu 14.04 Oct 13, 2016 As I've now noted a number of times, I'm in the process of a large scale AWS migration and we've had stability problems. Unlike previ...
-
6
MySQL Tip - How do you shut down mysql when you don't use upstart or init.d Aug 27, 2014 When you find yourself uttering statements like: That mysql tip is from Peter Zaitsev. I'll follow it because I...
-
10
Ruby background processes with upstart user jobsHi, weʼre arkency 👋
-
8
Starting UpFresh Off IPO, Upstart’s CEO Shares Why the Startup Isn’t a Typical Success StoryLast week,
-
11
Comment The big corporate clunking fists are taking aim at their upstart rivals Even when they enter the market late, such as Disney with streaming, giant multinationals can still walk away with the pri...
-
5
Jami is a very cool distributed instant messaging program under the GNU project. The same GNU that brings you GIMP, glibc,
-
8
Leiningen: Split an uberjar into dependencies.jar and app.jar (to optimize Docker layers and AWS Lambda functions) November 19, 2019 ...
-
2
Tip: Import Leiningen Project to IntelliJ (With Dependencies) October 26, 2012 To import a
-
4
This post describes how to configure the jetty-maven-plugin and the Jetty servlet container to run a web application that uses...
-
4
Ubuntu 下关于 Transmission daemon 的安装与配置
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK