Skip to content

Commit

Permalink
fix: delayed: restart connection to vcenter after some time
Browse files Browse the repository at this point in the history
  • Loading branch information
ireznice committed Nov 6, 2024
1 parent ec6525a commit 6bfabea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
16 changes: 14 additions & 2 deletions delayed.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ def host_info_obtainer(conn, vc):
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
vc = None
if Settings.app["vsphere"]["hosts_folder_name"]:
vc_needed = Settings.app["vsphere"]["hosts_folder_name"] is not None
loop_counter = 0
if vc_needed:
vc = vcenter.VCenter()
vc.connect(quick=True)

process_actions = True
while process_actions:
loop_counter = loop_counter + 1

with data.Connection.use('conn2') as conn:
try:
Expand Down Expand Up @@ -158,6 +161,15 @@ def host_info_obtainer(conn, vc):
logger.error('Exception while processing request: ', exc_info=True)
break

time.sleep(Settings.app['delayed']['sleep'])
# check whether we shouldn't re-connect
if vc_needed and \
(loop_counter % Settings.app['delayed']['reconnect_every_n']) == 0:
vc.disconnect()
logger.debug(f'vCenter disconnected at: {loop_counter} iteration')
vc.connect(quick=True)
logger.debug(f'vCenter re-connected at: {loop_counter} iteration')

time.sleep(Settings.app['delayed']['sleep'])
if vc_needed:
vc.disconnect()
logger.debug("Delayed finished")
10 changes: 10 additions & 0 deletions vcenter/vcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self):
self.vm_folders = None
self.destination_datastore = None
self.destination_resource_pool = None
self.service_instance = None

def __check_connection(self):
result = self.__get_objects_list_from_container(self.content.rootFolder, vim.Datastore)
Expand All @@ -64,6 +65,7 @@ def connect(self, quick=False):
)

self.content = si.content
self.service_instance = si
self._connection_cookie = si._stub.cookie
self.si_stub = si._stub # to be used for rapid managed object creation
self._connected = True
Expand All @@ -72,6 +74,14 @@ def connect(self, quick=False):
self.refresh_destination_datastore()
self.refresh_destination_resource_pool()

@log_to(vcenter_logger)
def disconnect(self):
try:
self.service_instance.Disconnect()
except Exception as ex:
self.__logger.debug('cannot disconnect from vCenter server', exc_info=True)
self.service_instance = None

def idle(self):
self.__check_connection()
self.__logger.debug('keeping connection alive: {}'.format(self.content.about.vendor))
Expand Down
1 change: 1 addition & 0 deletions web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Settings:
},
'delayed': {
'sleep': 1.5,
'reconnect_every_n': 10,
},
'ticketeer': {
'sleep': 6,
Expand Down

0 comments on commit 6bfabea

Please sign in to comment.