dbusjs / node-dbus-next

🚌 The next great dbus library for node
https://www.npmjs.com/package/dbus-next
155 stars 52 forks source link

systemd service #84

Open namxam opened 2 years ago

namxam commented 2 years ago

I know that this is probably not the right place to ask, but I just created a small app which is checking a MediaPlayer for its status. The service is running great when manually started from the command line. But fails miserably when I try to create a systemd service to automatically start it when the system restarts.

Has anyone an idea how to tackle that issue?

acrisci commented 2 years ago

This is going to be a bit difficult because a media player exposes the interface on the session bus and there can be zero or many session buses running on your system. The library will read DBUS_SESSION_BUS_ADDRESS env variable to specify which one (which is usually set by a session manager). You might want to look into using a dbus-activatable service which I use for playerctld on the Playerctl project.

namxam commented 2 years ago

@acrisci Thank you. I was already researching in this direction and it seems that I have a working setup… or at least a partially working solution.

I guess there might be more people who might be interested in this solution, so I try to be as explicit as possible.

I created a dbus service

# /usr/share/dbus-1/services/org.mpris.MediaPlayer2.vlc.service
[D-BUS Service]
Name=org.mpris.MediaPlayer2.vlc
Exec=/bin/false
SystemdService=vlc.service

with a corresponding systemd service

# /etc/systemd/user/vlc.service
Description=VLC media player

[Service]
Type=dbus
BusName=org.mpris.MediaPlayer2.vlc
Environment="DISPLAY=:0"
ExecStart=/usr/bin/vlc --video-on-top --fullscreen --no-video-title-show --no-osd --no-spu --start-paused

When I send a dbus message from command line it all works as expected and vlc is launched. Next I created a systemd user service for my application

[Unit]
Description=Demo MediaPlayer
After=dbus.service

[Service]
ExecStart=/usr/bin/node demo.mjs
WorkingDirectory=/home/pi/demo-media-player
StandardOutput=inherit
StandardError=inherit
Restart=always
Environment="DISPLAY=:0"

[Install]
WantedBy=default.target

I made sure that user services get loaded during boot via loginctl enable-linger $USER. And when I enable it and reboot, the app starts as expected. BUT for some reason, it now hangs indefinitely when getting the proxy object. In my code this line hangs

let dbus = await bus.getProxyObject(
    "org.mpris.MediaPlayer2.vlc",
    "/org/mpris/MediaPlayer2"
  );

When I restart the service via systemctl --user restart demo.service it works immediately. It just hangs after rebooting the system. I also checked and the VLC service is running as expected.

Any idea what might cause it?