Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delayed: restart connection to vcenter after some time #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
11 changes: 11 additions & 0 deletions vcenter/vcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Union, Optional

from pyVim.connect import SmartConnect
from pyVim.connect import Disconnect as SmartDisconnect
from pyVmomi import vim, vmodl
from web.settings import Settings, log_to

Expand Down Expand Up @@ -39,6 +40,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 +66,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 +75,14 @@ def connect(self, quick=False):
self.refresh_destination_datastore()
self.refresh_destination_resource_pool()

@log_to(vcenter_logger)
def disconnect(self):
try:
SmartDisconnect(self.service_instance)
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