Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Oct 28, 2024
1 parent 540c7b8 commit 8b70102
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/controllers/App/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ def write_conf(self, configuration):
},
'modules': {
**configuration['modules']
},
'service': {
**configuration['service']
}
}

Expand Down Expand Up @@ -593,6 +596,43 @@ def set_service_to_restart(self, services: str = None):
self.write_conf(configuration)


#-----------------------------------------------------------------------------------------------
#
# Get linupdate service startup delay
#
#-----------------------------------------------------------------------------------------------
def get_service_startup_delay(self):
# Get current configuration
configuration = self.get_conf()

# Set default startup delay to 3 seconds
delay = 3

if 'service' in configuration and 'startup_delay' in configuration['service']:
delay = configuration['service']['startup_delay']

return delay


#-----------------------------------------------------------------------------------------------
#
# Set linupdate service startup delay
#
#-----------------------------------------------------------------------------------------------
def set_service_startup_delay(self, delay: int):
# Get current configuration
configuration = self.get_conf()

if 'service' not in configuration:
configuration['service'] = {}

# Set service startup delay
configuration['service']['startup_delay'] = delay

# Write config file
self.write_conf(configuration)


#-----------------------------------------------------------------------------------------------
#
# Append a module to the enabled list
Expand Down
13 changes: 11 additions & 2 deletions src/controllers/App/Service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path

# Import classes
from src.controllers.App.Config import Config
from src.controllers.Module.Module import Module

class Service:
Expand All @@ -33,9 +34,17 @@ def main(self):
restart_file = '/tmp/linupdate.restart-needed'

try:
myconfig = Config()

# Retrieve service startup delay from configuration, if any
startup_delay = myconfig.get_service_startup_delay()

# Print a message to inform that the service is starting
print("[linupdate] Hi, I'm linupdate service. I will start all enabled module agents and let them run in background. Stop me and I will stop all module agents.")
# Wait 3 seconds to let the above message to be read
time.sleep(3)

# Wait X seconds to let the above message to be read or to let the startup delay to be executed
# Default is 3, but it can be changed in the configuration file to make the service wait before executing the next steps
time.sleep(startup_delay)

while True:
# Restart check
Expand Down
43 changes: 43 additions & 0 deletions src/controllers/Args.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def parse(self):
# Module disable
parser.add_argument("--mod-disable", action="store", nargs='?', default="null")

# Linupdate service
# Get service startup delay
parser.add_argument("--get-service-startup-delay", action="store_true", default="null")
# Set service startup delay
parser.add_argument("--set-service-startup-delay", action="store", nargs='?', default="null")

# Parse arguments
args, remaining_args = parser.parse_known_args()

Expand Down Expand Up @@ -592,6 +598,27 @@ def parse(self):
raise ArgsException('Could not disable module: ' + str(e))
else:
raise ArgsException('Module name is required')

#
# If --get-service-startup-delay param has been set
#
if args.get_service_startup_delay != "null":
try:
print(' Current service startup delay: ' + Fore.GREEN + str(myAppConfig.get_service_startup_delay()) + ' second(s)' + Style.RESET_ALL, end='\n\n')
myExit.clean_exit(0, False)
except Exception as e:
raise ArgsException('Could not get service startup delay: ' + str(e))

#
# If --set-service-startup-delay param has been set
#
if args.set_service_startup_delay != "null":
try:
myAppConfig.set_service_startup_delay(args.set_service_startup_delay)
print(' Service startup delay set to: ' + Fore.GREEN + str(args.set_service_startup_delay) + ' second(s)' + Style.RESET_ALL, end='\n\n')
myExit.clean_exit(0, False)
except Exception as e:
raise ArgsException('Could not set service startup delay: ' + str(e))

# Catch exceptions
# Either ArgsException or Exception, it will always raise an ArgsException to the main script, this to avoid sending an email when an argument error occurs
Expand Down Expand Up @@ -819,6 +846,22 @@ def help(self):
'option': 'MODULE',
'description': 'Disable a module'
},
{
'title': 'Linupdate service'
},
{
'args': [
'--set-service-startup-delay'
],
'option': 'DELAY',
'description': 'Set a delay before starting the linupdate service'
},
{
'args': [
'--get-service-startup-delay'
],
'description': 'Get the current service startup delay'
}
]

# Add options to table
Expand Down
1 change: 0 additions & 1 deletion src/controllers/Module/Reposerver/Reposerver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Import classes
from src.controllers.Module.Reposerver.Config import Config as Config
from src.controllers.Module.Reposerver.Status import Status
from src.controllers.Module.Reposerver.Args import Args

class Reposerver:
Expand Down
2 changes: 2 additions & 0 deletions templates/linupdate.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ main:
profile: Host
modules:
enabled: []
service:
startup_delay: 3

0 comments on commit 8b70102

Please sign in to comment.