systemd integration library for Erlang applications

栏目: IT技术 · 发布时间: 5年前

内容简介:Simple library for notifying systemd about process state.Just add this to your

systemd

Simple library for notifying systemd about process state.

Features

NOTIFY_SOCKET
journal

Installation

Just add this to your rebar.config :

{deps, [systemd]}.

Or in case of Mix project, to your mix.exs :

defp deps do
  [
    {:systemd, "~> 0.2"}
  ]
end

Then call systemd:notify(ready) when your application is ready to work/accept connections.

Non-systemd systems

This application and all functions within are safe to call even in non-systemd and non-Linux OSes. In case if there is no systemd configuration options then all functions will simply work as (almost) no-ops.

Usage

Assuming you have my_app.service unit like that

[Unit]
Description=My Awesome App

[Service]
User=appuser
Group=appgroup
Type=notify
# Important! This need to run in foreground and not fork.
# Check documentation of your release tool.
ExecStart=/path/to/my_app start
WatchdogSec=10s
NotifyAccess=main
Restart=on-failure

[Install]
WantedBy=multi-user.target

(the Type=notify part is important)

You can inform systemd about state of your application. To do so just call:

systemd:notify(ready).

This will make systemctl start my_app.service to wait until application is up and running.

If you want to restart your application you can notify systemd about it with:

systemd:notify(reloading).

Message about application shutting down will be handled automatically for you.

Logs

To handle logs you have 2 possible options:

  • Output data to standard error with special prefixes. This approach is much simpler and straightforward, however do not support structured logging and multiline messages.
  • Use datagram socket with special communication protocol. This requires a little bit more effort to set up, but seamlessly supports structured logging and multiline messages.

This library supports both formats, and it is up to You which one (or both?) your app will decide to use.

Standard error

There is systemd_stderr_formatter which can be used with any logger that outputs to standard error, for example logger_std_h . So to use this format it is the best to define in your sys.config :

[
  {kernel,
   [{logger,
     [{handler, default, logger_std_h, #{formatter => {systemd_stderr_formatter, #{}},
                                                       type => standard_error}}]
    }}}
].

This will add required prefixes automatically for you.

Datagram socket

This one requires systemd application to be started to spawn some processes required for handling sockets, so the best way to handle it is to add predefined systemd handlers after your application starts:

logger:add_handlers(systemd).

Be aware that this one is not guaranteed to work on non-systemd systems, so if You aren't sure if that application will be ran on systemd-enabled OS then you shouldn't use it as an only logger solution in your application or you can end with no logger attached at all.

License

See LICENSE .


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

赢在设计

赢在设计

[美] 洛芙迪 (Lance Loveday)、[美] 尼豪斯 (Sandra Niehaus) / 刘淼、枊靖、王卓昊 / 人民邮电出版社 / 2010-8 / 55.00

企业总是面临在网站设计和改进方面进行投资的抉择。怎样才能让有限的资金发挥出最大的效益呢?网站设计不应只是把网站做得赏心悦目,它更应该是提高经济收益和获得竞争优势的战略利器。是时候让网站发挥其潜能,以业务指标为导向来做设计决策,为提升网站收益而设计了。 作者凭借多年为众多网站做咨询工作的经验,为我们揭示了赢在设计的奥秘。它针对目前网站设计中存在的典型问题,先从宏观上探讨解决问题的战略手段,围绕......一起来看看 《赢在设计》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具