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

Close datasets properly #492

Merged
merged 2 commits into from
Sep 23, 2021
Merged
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
6 changes: 5 additions & 1 deletion snowfakery/data_generator_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from contextlib import contextmanager

from typing import Optional, Dict, Sequence, Mapping, NamedTuple, Set
from warnings import warn

import jinja2
import yaml
Expand Down Expand Up @@ -354,7 +355,10 @@ def __enter__(self, *args):

def __exit__(self, *args):
for plugin in self.plugin_instances.values():
plugin.close()
try:
plugin.close()
except Exception as e:
warn(f"Could not close {plugin} because {e}")


class RuntimeContext:
Expand Down
15 changes: 13 additions & 2 deletions snowfakery/standard_plugins/Salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,19 @@ def _load_dataset(self, iteration_mode, rootpath, kwargs):
return self.iterator

def close(self):
self.iterator.close()
self.tempdir.close()
if self.iterator:
self.iterator.close()
self.iterator = None

if self.tempdir:
self.tempdir.cleanup()
self.tempdir = None

def __del__(self):
# in case close was not called
# properly, try to do an orderly
# cleanup
self.close()


def create_tempfile_sql_db_iterator(mode, fieldnames, results):
Expand Down
4 changes: 4 additions & 0 deletions snowfakery/standard_plugins/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ def iterate(self, **kwargs):
def shuffle(self, **kwargs):
return self.context.plugin.dataset_impl._iterate(self, "shuffle", kwargs)

def close(self):
if self.dataset_impl:
self.dataset_impl.close()


class Dataset(DatasetPluginBase):
def __init__(self, *args, **kwargs):
Expand Down