Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mdip226 committed Jul 19, 2023
1 parent 4a45bfc commit 818d2fb
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 72 deletions.
2 changes: 1 addition & 1 deletion gillespy2/remote/client/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get(self, endpoint: Endpoint, sub: str, request: Request = None):
while n_try <= 3:
try:
if request is not None:
return requests.get( url, timeout=30, json=request.encode())
return requests.get(url, timeout=30, params=request.encode())
return requests.get( url, timeout=30)

except ConnectionError:
Expand Down
13 changes: 6 additions & 7 deletions gillespy2/remote/core/messages/simulation_run_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ def parse(raw_response):
'''
response_dict = json_decode(raw_response)
status = SimStatus.from_str(response_dict['status'])
results_id = response_dict['results_id']
error_message = response_dict['error_message']
task_id = response_dict['task_id']
if response_dict['results'] != '':
results = Results.from_json(response_dict['results'])
else:
results = None
results_id = response_dict.get('results_id', None)
error_message = response_dict.get('error_message', None)
task_id = response_dict.get('task_id', None)
results = response_dict.get('results', None)
if results is not None:
results = Results.from_json(results)
return SimulationRunCacheResponse(status, error_message, results_id, results, task_id)
32 changes: 17 additions & 15 deletions gillespy2/remote/core/remote_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from gillespy2 import Results
from gillespy2.remote.client.endpoint import Endpoint
from gillespy2.remote.core.errors import RemoteSimulationError
from gillespy2.remote.core.messages.results import ResultsResponse
from gillespy2.remote.core.messages.results import ResultsRequest, ResultsResponse
from gillespy2.remote.core.messages.status import StatusRequest, StatusResponse, SimStatus

from gillespy2.remote.core.log_config import init_logging
Expand All @@ -44,13 +44,16 @@ class RemoteResults(Results):
:param task_id: Handle for the running simulation.
:type task_id: str
:param namespace: Optional namespace.
:type namespace: str
'''
# These four fields are initialized after object creation.
id = None

id = None # required
server = None
n_traj = None
task_id = None
namespace = None
n_traj = None # Defaults to 1
task_id = None # optional
namespace = None # optional

# pylint:disable=super-init-not-called
def __init__(self, data = None):
Expand Down Expand Up @@ -110,11 +113,12 @@ def _status(self):
It is undefined/illegal behavior to call this function if self._data is not None.
'''
if self._data is not None:
raise Exception('TODO Name this exception class. Cant call status on a finished simulation.')
# Request the status of a submitted simulation.
status_request = StatusRequest(self.id, self.namespace)
response_raw = self.server.post(Endpoint.SIMULATION_GILLESPY2,
f"/status")
raise Exception('TODO Name this exception class. Cannot call status on a finished simulation.')

status_request = StatusRequest(self.id, self.n_traj, self.task_id, self.namespace)

response_raw = self.server.get(Endpoint.SIMULATION_GILLESPY2, '/status', request=status_request)

if not response_raw.ok:
raise RemoteSimulationError(response_raw.reason)

Expand Down Expand Up @@ -142,10 +146,8 @@ def _resolve(self):

if status == SimStatus.READY:
log.info('Results ready. Fetching.......')
if self.id == self.task_id:
response_raw = self.server.get(Endpoint.SIMULATION_GILLESPY2, f"/{self.id}/results")
else:
response_raw = self.server.get(Endpoint.SIMULATION_GILLESPY2, f"/{self.id}/{self.n_traj}/results")
results_request = ResultsRequest(self.id, self.namespace)
response_raw = self.server.get(Endpoint.SIMULATION_GILLESPY2, f"/results", request=results_request)
if not response_raw.ok:
raise RemoteSimulationError(response_raw.reason)

Expand Down
23 changes: 0 additions & 23 deletions gillespy2/remote/core/remote_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,3 @@ def _run_cache(self, request):
remote_results.n_traj = request.kwargs.get('number_of_trajectories', 1)

return remote_results

def _run(self, request):
'''
Ignores the cache. Gives each simulation request a unique identifier.
:param request: Request to send to the server. Contains Model and related arguments.
:type request: SimulationRunUniqueRequest
'''
response_raw = self.server.post(Endpoint.SIMULATION_GILLESPY2, sub="/run", request=request)

if not response_raw.ok:
raise Exception(response_raw.reason)
sim_response = SimulationRunResponse.parse(response_raw.text)
if not sim_response.status is SimStatus.RUNNING:
raise Exception(sim_response.error_message)
# non-conforming object creation ... possible refactor needed to solve, so left in.
remote_results = RemoteResults()
remote_results.id = request.id
remote_results.task_id = request.id
remote_results.server = self.server
remote_results.n_traj = request.kwargs.get('number_of_trajectories', 1)

return remote_results
14 changes: 7 additions & 7 deletions gillespy2/remote/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import subprocess
from logging import DEBUG, INFO
from tornado.web import Application
from gillespy2.remote.server.is_cached import IsCachedHandler
from gillespy2.remote.server.simulation_run_cache import SimulationRunCacheHandler
from gillespy2.remote.server.sourceip import SourceIpHandler
from gillespy2.remote.server.status import StatusHandler
from gillespy2.remote.server.results import ResultsHandler
from gillespy2.remote.server.handlers.is_cached import IsCachedHandler
from gillespy2.remote.server.handlers.simulation_run_cache import SimulationRunCacheHandler
from gillespy2.remote.server.handlers.sourceip import SourceIpHandler
from gillespy2.remote.server.handlers.status import StatusHandler
from gillespy2.remote.server.handlers.results import ResultsHandler
from gillespy2.remote.core.log_config import init_logging, set_global_log_level
log = init_logging(__name__)

Expand All @@ -39,10 +39,10 @@ def _make_app(dask_host, dask_scheduler_port, cache):
(r'/api/v2/simulation/gillespy2/run/cache',
SimulationRunCacheHandler,
args),
(r'/api/v2/simulation/gillespy2/(?P<results_id>[0-9a-fA-F][0-9a-fA-F]*?)/(?P<task_id>[0-9a-fA-F][0-9a-fA-F]*?)/status',
(r'/api/v2/simulation/gillespy2/status',
StatusHandler,
args),
(r'/api/v2/simulation/gillespy2/(?P<results_id>[0-9a-zA-Z][0-9a-zA-Z]*?)/results',
(r'/api/v2/simulation/gillespy2/results',
ResultsHandler,
cache_arg),
(r'/api/v2/cache/gillespy2/(?P<results_id>.*?)/(?P<n_traj>[1-9]\d*?)/is_cached',
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def initialize(self, cache_dir):
:param cache_dir: Path to the cache.
:type cache_dir: str
'''
while cache_dir.endswith('/'):
cache_dir = cache_dir[:-1]
self.cache_dir = cache_dir + '/run/'
# while cache_dir.endswith('/'):
# cache_dir = cache_dir[:-1]
self.cache_dir = cache_dir

async def get(self, results_id = None):
async def get(self):
'''
Process GET request.
Expand All @@ -56,15 +56,20 @@ async def get(self, results_id = None):
:param n_traj: Number of trajectories in the request.
:type n_traj: str
'''
if results_id in ('', '/'):
results_id = self.get_query_argument('results_id', None)
n_traj = self.get_query_argument('n_traj', 0)
if results_id is None:
self.set_status(404, reason=f'Malformed request: {self.request.uri}')
self.finish()
raise RemoteSimulationError(f'Malformed request | <{self.request.remote_ip}>')
msg = f' <{self.request.remote_ip}> | Results Request | <{results_id}>'
log.info(msg)
cache = Cache(self.cache_dir, results_id)
if cache.is_ready():
results = cache.read()
if cache.is_ready(n_traj_wanted=n_traj):
if n_traj > 0:
results = cache.get_sample(n_traj)
else:
results = cache.read()
results_response = ResultsResponse(results)
self.write(results_response.encode())
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def post(self):
sim_request = SimulationRunCacheRequest.parse(self.request.body)
namespace = sim_request.namespace
log.debug('%(namespace)s', locals())
if namespace != '':
if namespace is not None:
namespaced_dir = os.path.join(namespace, self.cache_dir)
self.cache_dir = namespaced_dir
log.debug(namespaced_dir)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,25 @@ def initialize(self, scheduler_address, cache_dir):
self.scheduler_address = scheduler_address
self.cache_dir = cache_dir

async def post(self):
async def get(self):
'''
Process Status POST request.
Process Status GET request.
'''
status_request = StatusRequest.parse(self.request.body)
log.debug(status_request.__dict__)

# status_request = StatusRequest.parse(self.request)
log.debug(self.request.query_arguments)

results_id = status_request.results_id
n_traj = int(status_request.n_traj)
task_id = status_request.task_id
namespace = status_request.namespace
results_id = self.get_query_argument('results_id')
n_traj = self.get_query_argument('n_traj', None)
if n_traj is not None:
n_traj = int(n_traj)
task_id = self.get_query_argument('task_id', None)
namespace = self.get_query_argument('namespace', None)

if namespace is not None:
self.cache_dir = os.path.join(self.cache_dir, namespace)
if results_id == task_id: # True iff call made using (ignore_cache=True)
self.cache_dir = os.path.join(self.cache_dir, 'run/')
# if results_id == task_id: # True iff call made using (ignore_cache=True)
# self.cache_dir = os.path.join(self.cache_dir, 'run/')

cache = Cache(self.cache_dir, results_id)

Expand Down

0 comments on commit 818d2fb

Please sign in to comment.