diff --git a/actions/deluge b/actions/deluge index 3d2f0b495..609bd22de 100755 --- a/actions/deluge +++ b/actions/deluge @@ -141,19 +141,46 @@ def subcommand_setup(_): _set_deluged_daemon_options() subprocess.check_call(['systemctl', 'daemon-reload']) - # restarting deluge-web service stops also possible deluged process - # that was started from the web interface - action_utils.service_restart('deluge-web') - action_utils.service_restart('deluged') - # wait processes to start - time.sleep(10) - # configure deluge-web to autoconnect to the default deluged daemon, also - # restarts deluged and deluge-web services again to create config files + # Restarting an old deluge-web service stops also possible deluged process + # that was started from the web interface. + action_utils.service_try_restart('deluge-web') + + # Wait until core configuration is available so that status of the app can + # be shown properly. + _wait_for_configuration('deluged', 'core.conf') + + # Configure deluge-web to autoconnect to the default deluged daemon. + _wait_for_configuration('deluge-web', 'web.conf') host_id = _get_host_id() _set_configuration('web.conf', 'default_daemon', host_id) +def _wait_for_configuration(service, file_name): + """Wait until configuration file has been created.""" + conf_file = DELUGE_CONF_DIR / file_name + if conf_file.exists(): + return + + # deluge-web creates files on first run. deluged on the other than differs + # in version. Older version in Debian Buster creates the files after a + # restart while newer versions create the files on first run. The following + # approach is slightly better for create-on-exit case. + is_running = action_utils.service_is_running(service) + for interval in range(7): + action_utils.service_restart(service) + if conf_file.exists(): + break + + print('Waiting for {service} configuration') + time.sleep(2**interval) # Exponentially increase the time waited + else: + raise Exception(f'Unable to setup {service}.') + + if not is_running: + action_utils.service_stop(service) + + def main(): """Parse arguments and perform all duties.""" arguments = parse_arguments() diff --git a/plinth/modules/deluge/utils.py b/plinth/modules/deluge/utils.py index c4e3febbc..9d535a1e7 100644 --- a/plinth/modules/deluge/utils.py +++ b/plinth/modules/deluge/utils.py @@ -34,7 +34,7 @@ class Config: try: self._version = json.loads(matches.group(1)) self.content = json.loads(matches.group(2)) - except json.decoder.JSONDecoderError: + except json.decoder.JSONDecodeError: raise Exception('Unable to parse JSON in file.') if self._version['format'] != 1: