Skip to content

Commit

Permalink
Merge 5b3d30d into 80657d6
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Aug 16, 2020
2 parents 80657d6 + 5b3d30d commit ee7958f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
18 changes: 12 additions & 6 deletions pyiron/base/job/generic.py
Expand Up @@ -879,10 +879,16 @@ def run_if_non_modal(self):
The run if non modal function is called by run to execute the simulation in the background. For this we use
multiprocessing.Process()
"""
p = multiprocessing.Process(
target=multiprocess_wrapper,
args=(self.job_id, self.project_hdf5.working_directory, False),
)
if not s.using_local_database:
p = multiprocessing.Process(
target=multiprocess_wrapper,
args=(self.job_id, self.project_hdf5.working_directory, False, None),
)
else:
p = multiprocessing.Process(
target=multiprocess_wrapper,
args=(self.job_id, self.project_hdf5.working_directory, False, str(self.project.db.conn.engine.url)),
)
if self.master_id and self.server.run_mode.non_modal:
del self
p.start()
Expand Down Expand Up @@ -1660,9 +1666,9 @@ def _print_error(self, file_name, string='', print_yes=True):
return True


def multiprocess_wrapper(job_id, working_dir, debug=False):
def multiprocess_wrapper(job_id, working_dir, debug=False, connection_string=None):
job_wrap = JobWrapper(
working_directory=str(working_dir), job_id=int(job_id), debug=debug
working_directory=str(working_dir), job_id=int(job_id), debug=debug, connection_string=connection_string
)
job_wrap.job.run_static()

Expand Down
4 changes: 3 additions & 1 deletion pyiron/base/job/wrapper.py
Expand Up @@ -41,9 +41,11 @@ class JobWrapper(object):
"""

def __init__(self, working_directory, job_id=None, hdf5_file=None, h5_path=None, submit_on_remote=False,
debug=False):
debug=False, connection_string=None):
self.working_directory = working_directory
self._remote_flag = submit_on_remote
if connection_string is not None:
s.open_local_sqlite_connection(connection_string=connection_string)
pr = Project(path=os.path.join(working_directory, '..', '..'))
if job_id is not None:
self.job = pr.load(int(job_id))
Expand Down
20 changes: 13 additions & 7 deletions pyiron/base/project/generic.py
Expand Up @@ -1150,21 +1150,27 @@ def switch_to_local_database(self, file_name="pyiron.db", cwd=None):
file_name (str): file name or file path for the local database
cwd (str): directory where the local database is located
"""
if not isinstance(self.db, FileTable):
if cwd is None:
cwd = self.path
if cwd is None:
cwd = self.path
if not s.project_check_enabled:
s.switch_to_local_database(file_name=file_name, cwd=cwd)
s.open_connection()
self.db = s.database
super(Project, self).__init__(path=self.path)
else:
s.switch_to_local_database(file_name=file_name, cwd=cwd)
s.open_connection()
self.db = s.database

def switch_to_central_database(self):
"""
Switch from local mode to central mode - if local_mode is enable pyiron is using a local database.
"""
if not isinstance(self.db, FileTable):
s.switch_to_central_database()
s.switch_to_central_database()
if not s.database_is_disabled:
s.open_connection()
self.db = s.database
else:
self.db = FileTable(project=self.path)
super(Project, self).__init__(path=self.path)

def queue_delete_job(self, item):
"""
Expand Down
41 changes: 28 additions & 13 deletions pyiron/base/settings/generic.py
Expand Up @@ -109,6 +109,7 @@ def __init__(self, config=None):
]

# Build the SQLalchemy connection strings
self._database_is_disabled = self._configuration["disable_database"]
if not self.database_is_disabled:
self._configuration = self.convert_database_config(
config=self._configuration
Expand All @@ -123,6 +124,10 @@ def __init__(self, config=None):
self._publication_lst = {}
self.publication_add(self.publication)

@property
def using_local_database(self):
return self._use_local_database

@property
def database(self):
return self._database
Expand All @@ -133,11 +138,14 @@ def queue_adapter(self):

@property
def project_check_enabled(self):
return self._configuration["project_check_enabled"]
if self.database_is_disabled:
return False
else:
return self._configuration["project_check_enabled"]

@property
def database_is_disabled(self):
return self._configuration["disable_database"]
return self._database_is_disabled

@property
def publication_lst(self):
Expand Down Expand Up @@ -205,29 +213,36 @@ def switch_to_local_database(self, file_name="pyiron.db", cwd=None):
file_name (str): SQLite database file name
cwd (str/None): directory where the SQLite database file is located in
"""
if not self._use_local_database and not self.database_is_disabled:
if not self._use_local_database:
if cwd is None and not os.path.isabs(file_name):
file_name = os.path.join(os.path.abspath(os.path.curdir), file_name)
elif cwd is not None:
file_name = os.path.join(cwd, file_name)
self.close_connection()
self._database = DatabaseAccess(
"sqlite:///" + file_name, self._configuration["sql_table_name"]
)
self._use_local_database = True
self.open_local_sqlite_connection(connection_string="sqlite:///" + file_name)
else:
print("Database is already in local mode or disabled!")

def open_local_sqlite_connection(self, connection_string):
self.close_connection()
self._database = DatabaseAccess(connection_string, self._configuration["sql_table_name"])
self._use_local_database = True
if self.database_is_disabled:
self._database_is_disabled = False

def switch_to_central_database(self):
"""
Switch to central database
"""
if self._use_local_database and not self.database_is_disabled:
if self._use_local_database:
self.close_connection()
self._database = DatabaseAccess(
self._configuration["sql_connection_string"],
self._configuration["sql_table_name"],
)
self._database_is_disabled = self._configuration["disable_database"]
if not self.database_is_disabled:
self._database = DatabaseAccess(
self._configuration["sql_connection_string"],
self._configuration["sql_table_name"],
)
else:
self._database = None
self._use_local_database = False
else:
print("Database is already in central mode or disabled!")
Expand Down

0 comments on commit ee7958f

Please sign in to comment.