[Unit]
Description=Elasticsearch instance <%= @resource[:instance] %>
Documentation=http://www.elastic.co
Wants=network-online.target
After=network-online.target

[Service]
EnvironmentFile=<%= @resource[:defaults_location] %>/elasticsearch-<%= @resource[:instance] %>

WorkingDirectory=<%= @resource[:homedir] %>

User=<%= @resource[:user] %>
Group=<%= @resource[:group] %>

ExecStart=/usr/share/elasticsearch/bin/elasticsearch \
                                                -p <%= @resource[:pid_dir] %>/elasticsearch-<%= @resource[:instance] %>.pid \
                                                <%= opt_flags.join(' ') %>

# StandardOutput is configured to redirect to journalctl since
# some error messages may be logged in standard output before
# elasticsearch logging system is initialized. Elasticsearch
# stores its logs in /var/log/elasticsearch and does not use
# journalctl by default. If you also want to enable journalctl
# logging, you can simply remove the "quiet" option from ExecStart.
StandardOutput=journal
StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=<%= @resource[:nofile] %>

# Specifies the maximum number of processes
LimitNPROC=<%= @resource[:nproc] %>

# Specifies the maximum number of bytes of memory that may be locked into RAM
# Set to "infinity" if you use the 'bootstrap.memory_lock: true' option
# in elasticsearch.yml and 'MAX_LOCKED_MEMORY=unlimited' in ${path.env}
#LimitMEMLOCK=infinity
<% if @resource[:memlock] == 'unlimited' %>
LimitMEMLOCK=infinity
<% elsif @resource[:memlock] %>
LimitMEMLOCK=<%= @resource[:memlock] %>
<% end %>

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0

# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM

# Send the signal only to the JVM rather than its control group
KillMode=process

# Java process is never killed
SendSIGKILL=no

# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
