Skip to content

Commit

Permalink
Avoid new FTP timeout errors on quit()
Browse files Browse the repository at this point in the history
  • Loading branch information
kwilcox committed Mar 29, 2022
1 parent 9bbbcb0 commit ebbe59d
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions gutils/watch/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def my_init(self, ftp_url, ftp_user, ftp_pass):
self.ftp_url = ftp_url
self.ftp_user = ftp_user
self.ftp_pass = ftp_pass
self.ftp_timeout = 60

def process_IN_CLOSE(self, event):
args = SimpleNamespace(file=event.pathname)
Expand All @@ -55,36 +56,37 @@ def process_IN_MOVED_TO(self, event):
self.upload_file(event)

def upload_file(self, event):
ftp = None
try:
ftp = FTP(self.ftp_url)
ftp.login(self.ftp_user, self.ftp_pass)

with nc4.Dataset(event.pathname) as ncd:
if not hasattr(ncd, 'id'):
raise ValueError("No 'id' global attribute")
# Change into the correct deployment directory
try:
ftp.cwd(ncd.id)
except BaseException:
ftp.mkd(ncd.id)
ftp.cwd(ncd.id)

with open(event.pathname, 'rb') as fp:
# Upload NetCDF file
ftp.storbinary(
'STOR {}'.format(event.name),
fp
)
L.info("Uploaded file: {}".format(event.name))
ftp_kwargs = {
'host': self.ftp_url,
'user': self.ftp_user,
'passwd': self.ftp_pass,
'timeout': self.ftp_timeout
}

with FTP(**ftp_kwargs) as ftp:

with nc4.Dataset(event.pathname) as ncd:
if not hasattr(ncd, 'id'):
raise ValueError("No 'id' global attribute")
# Change into the correct deployment directory
try:
ftp.cwd(ncd.id)
except BaseException:
ftp.mkd(ncd.id)
ftp.cwd(ncd.id)

with open(event.pathname, 'rb') as fp:
# Upload NetCDF file
ftp.storbinary(
'STOR {}'.format(event.name),
fp
)
L.info("Uploaded file: {}".format(event.name))

except BaseException as e:
L.error('Could not upload: {} to {}. {}.'.format(event.pathname, self.ftp_url, e))

finally:
if ftp is not None:
ftp.quit()


def create_ftp_arg_parser():

Expand Down

0 comments on commit ebbe59d

Please sign in to comment.