elgatito / plugin.video.elementum

Elementum add-on for Kodi. Development of this addon has been stopped!
http://elementum.surge.sh
MIT License
468 stars 157 forks source link

Running Elementum without Kodi #1048

Open Dim0N22 opened 1 month ago

Dim0N22 commented 1 month ago

In releases 0.1.88-0.1.92 there was a changelog

Added support for reading configuration form json/yaml file (Kodi is not needed to startup)

I assume it is now possible to run Elementum without Kodi, is this correct? Is there any documentation on how to do this? Specifically on how to save the current Elementum configuration and then run the binary with the config.

I run Kodi with Flatpak and once it's closed, it also stops Elementum. I want to continue seeding torrents even if Kodi has been closed.

Dim0N22 commented 1 month ago

Found that you can export current config with

./elementum -exportConfig=<path.yaml>

Though when I then start elementum with

./elementum -configPath=config.yaml

if crashes with

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb851e5]

goroutine 29 [running]:
github.com/elgatito/elementum/xbmc.(*XBMCHost).NewDialogProgressBG(0x0, {0x1534c56, 0x9}, {0x0, 0x0}, {0x0, 0x0, 0x0})
        /go/src/github.com/elgatito/elementum/xbmc/xbmcgui.go:73 +0x145
github.com/elgatito/elementum/bittorrent.(*Service).onDownloadProgress(0xc0004c6000)
        /go/src/github.com/elgatito/elementum/bittorrent/service.go:1609 +0xc3d
created by github.com/elgatito/elementum/bittorrent.NewService in goroutine 1
        /go/src/github.com/elgatito/elementum/bittorrent/service.go:124 +0x445

Apparently it still tries to communicate with Kodi which is not running. Is it possible to prevent this?

Dim0N22 commented 1 month ago

I had to set

disable_bg_progress: true

to stop it from crashing.

I also want to run it as a systemd service, though it doesn't work by default due to the check in https://github.com/elgatito/elementum/blob/master/main.go#L233:

if os.Getppid() == 1 {
  log.Warning("Parent shut down, shutting down too...")
  go shutdown(exit.ExitCodeSuccess)
  break
}

My understanding is that parent PID is 1 if you run a service with systemd, thus elementum immediately exits. I had to use a dirty hack of running screen which in turn runs elementum:

[Service]
Type=forking
Restart=on-failure
ExecStart=/usr/bin/screen -d -m <path>/elementum -configPath=<path>/elementum_config.yaml

It would be great to see a better solution for running elementum as a service.

Another issue is that web requests (e.g. /torrents/list) to elementum fail with

2024/06/02 00:38:47 [Recovery] 2024/06/02 - 00:38:47 panic recovered:
runtime error: invalid memory address or nil pointer dereference
/usr/x86_64-linux-gnu/go/src/runtime/panic.go:261 (0x57a517)
/usr/x86_64-linux-gnu/go/src/runtime/signal_unix.go:861 (0x57a4e5)
/go/src/github.com/elgatito/elementum/xbmc/xbmc.go:534 (0xb824c3)
/go/src/github.com/elgatito/elementum/api/torrents.go:364 (0xe45b77)

As far as I understand, this happens if Kodi wasn't started when elementum starts. Starting Kodi later doesn't help. As a result, no torrents are shown in Web UI.

antonsoroko commented 1 month ago

@Dim0N22

  1. about disable_bg_progress and /torrents/list - root cause is the same.

for disable_bg_progress - elementum tries to make a dialog pop-up (which requires request to kodi) https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/bittorrent/service.go#L1609 https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/xbmc/xbmcgui.go#L73 so elementum tries to make RPC to kodi call but xbmcHost is nil.

same for /torrents/list elementum tries to translate torrent status (which also requires request to kodi) https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/api/torrents.go#L364 https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/xbmc/xbmc.go#L534

and you end up here https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/xbmc/jsonrpc.go#L120 with nil.

this is because XBMCLocalHost is nil in your case. and it is nil because you started elementum w/o kodi and elementum tried to find kodi ip and failed: https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/xbmc/jsonrpc.go#L46

but if you would have specified -remoteHost then elementum would force set of XBMCLocalHost value https://github.com/elgatito/elementum/blob/2c644cdb6d2908b4d2dfbe0e17c1f2fdea5ef7d5/config/config.go#L358

so most likely if you set -remoteHost command line option - it would stop elementum from panic in both cases. although, obviously since kodi's RCP is unavailable you will have side effects, like no status labels in web UI (this can not be easily fixed as i can see), and background sync of kodi library will fail (probably w/o panic but i am not sure).

  1. about os.Getppid() == 1

the easy fix from your side would be to use systemd --user mode, e.g.:

$ systemctl --user status wireplumber.service
● wireplumber.service - Multimedia Service Session Manager
     Loaded: loaded (/usr/lib/systemd/user/wireplumber.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-06-11 14:33:38 +03; 1 day 2h ago
   Main PID: 5417 (wireplumber)
...

$ ps -o ppid= -p 5417
   5394

$ ps -f -p 5394
UID          PID    PPID  C STIME TTY          TIME CMD
abcd       5394       1  0 июн11 ?     00:00:01 /usr/lib/systemd/systemd --user

so your elementum service will have systemd as parent. (e.g. systemctl edit --user --force --full elementum.service to create user-level service)

also, Type=forking is not needed since elementum does not do fork().

another fix could be adding a check that if -configPath= is set then do not run watchParentProcess() fucntion (thus no exit).


https://github.com/elgatito/elementum/commit/1dba83b97282cf1cfb3eaafd4792e14947cf2e5c says Added initial support for running Elementum without Kodi. so most likely some use cases were not covered. if @elgatito will have time, maybe he can provide more elegant fixes than i did.

antonsoroko commented 3 weeks ago

@Dim0N22 So, have -remoteHost helped you? And does Elementum work fine in systemd user mode?

elgatito commented 1 day ago

I was not trying running from systemd, was only testing from console, on a host that does not have Kodi.