def test_is_process_running_with_compound_command(self): """ pid and name pattern should match a process whose command consists of multiple strings. """ p = subprocess.Popen("sleep 5", shell=True) self.assertTrue(is_process_running(p.pid, "p 5"))
def test_is_process_running_with_no_pid(self): """ The name pattern should match a process even though no pid is given. """ the_time = str(time.time()) subprocess.Popen("sleep 5; echo %s >/dev/null" % the_time, shell=True) self.assertTrue(is_process_running(pattern=the_time))
def oq_job_result(request, job_id): """This allows the GUI to poll for OpenQuake job status. The request must be a HTTP GET. If the OpenQuake job is in progress we return a 404. In case of succes and failure we return a 200 and a 500 status code respectively. Here's an example of the json data retuned in case of success: { "status": "success", "msg": "Calculation succeeded", "id": 123, "files": [{ "id": 77, "name": "loss-map-0fcfdbc7.xml", "type": "loss map", "min": 2.718281, "max": 3.141593, "layer": { "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows" "layer": "geonode:77-loss-map-0fcfdbc7"}}, { "id": 78, "name": "hazardmap-0.01-mean.xml", "type": "hazard map", "min": 0.060256, "max": 9.780226 "layer": { "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows" "layer": "geonode:78-hazardmap-0-01-mean"}}]} :param request: the :py:class:`django.http.HttpRequest` object :param integer job_id: the database key of the associated oq_job record (see also :py:class:`geonode.mtapi.models.OqJob`) :returns: a :py:class:`django.http.HttpResponse` object with status code `200` and `500` if the OpenQuake job succeeded and failed respectively. :raises Http404: when the OpenQuake job is still in progress or if the request is not a HTTP GET request. """ print("job_id: %s" % job_id) if request.method == "GET": job = OqJob.objects.get(id=int(job_id)) if job.status == "running": oqrunner_is_alive = view_utils.is_process_running( job.job_pid, settings.OQRUNNER_PATH) if oqrunner_is_alive: print "OpenQuake job in progress.." raise Http404 else: job.status = "failed" job.save() result = simplejson.dumps(prepare_job_result(job)) print "OpenQuake job failed, process not found.." return HttpResponse(result, status=500, mimetype="text/html") else: result = simplejson.dumps(prepare_job_result(job)) if job.status == "failed": print "OpenQuake job failed.." return HttpResponse(result, status=500, mimetype="text/html") else: print "OpenQuake job succeeded.." return HttpResponse(result, mimetype="text/html") else: return HttpResponse( "Wrong HTTP request type, use a GET for this API endpoint", status=500, mimetype="text/html")
def test_is_process_running_with_no_pid_and_multi_match(self): """ The name pattern should match in case where we have multiple process and no pid is given. """ the_time = str(time.time()) subprocess.Popen("sleep 5; echo %s >/dev/null" % the_time, shell=True) subprocess.Popen("sleep 6; echo %s >/dev/null" % the_time, shell=True) self.assertTrue(is_process_running(pattern=the_time))
def input_upload_result(request, upload_id): """This allows the GUI to poll for upload processing status. The request must be a HTTP GET. If the upload processing is in progress we return a 404. In case of succes and failure we return a 200 and a 500 status code respectively. Here's an example of the json data retuned in case of success: {"msg": "All uploaded files processed", "status": "success", "id": 4, "files": [{"name": "simpleFaultModel.xml", "id": 6}]} :param request: the :py:class:`django.http.HttpRequest` object :param integer upload_id: the database key of the associated upload record (see also :py:class:`geonode.mtapi.models.Upload`) :returns: a :py:class:`django.http.HttpResponse` object with status code `200` and `500` if the upload processing succeeded and failed respectively. :raises Http404: when the processing of the uploaded source model files is still in progress or if the request is not a HTTP GET request. """ print("upload_id: %s" % upload_id) if request.method == "GET": [upload] = Upload.objects.filter(id=int(upload_id)) if upload.status == "running": processor_is_alive = view_utils.is_process_running( upload.job_pid, settings.NRML_RUNNER_PATH) if processor_is_alive: print "Upload processing in progress.." raise Http404 else: upload.status = "failed" upload.save() result = prepare_upload_result(upload) print "Upload processing failed, process not found.." return HttpResponse(result, status=500, mimetype="text/html") else: result = prepare_upload_result(upload) if upload.status == "failed": print "Upload processing failed.." return HttpResponse(result, status=500, mimetype="text/html") else: print "Upload processing succeeded.." return HttpResponse(result, mimetype="text/html") else: raise Http404
def update_layers(): """Updates the geonode layers, called after shapefile registration.""" logger.info("> update_layers") command = settings.OQ_UPDATE_LAYERS_PATH logger.info("command: %s" % command) if view_utils.is_process_running(pattern=command): logger.info("A process that updates layers is already running..") return # Our default python path breaks the virtualenv running the "updatelayers" # command. python_path = os.environ["PYTHONPATH"] logger.info("PYTHONPATH: '%s'" % python_path) os.environ["PYTHONPATH"] = "" # Run the "updatelayers" command in asynchronous fashion. subprocess.Popen(command, env=os.environ) # Restore python path. os.environ["PYTHONPATH"] = python_path logger.info("< update_layers")
def update_layers(): """Updates the geonode layers, called after shapefile registration.""" logger.info("> update_layers") command = settings.OQ_UPDATE_LAYERS_PATH logger.info("command: %s" % command) if view_utils.is_process_running(pattern=command): logger.info("A process that updates layers is already running..") return # Our default python path breaks the virtualenv running the "updatelayers" # command. python_path = os.environ.get("PYTHONPATH") logger.info("PYTHONPATH: '%s'" % python_path) os.environ["PYTHONPATH"] = "" # Run the "updatelayers" command in asynchronous fashion. subprocess.Popen(command, env=os.environ) # Restore python path. if python_path is not None: os.environ["PYTHONPATH"] = python_path logger.info("< update_layers")
def test_is_process_running_with_non_matching_pattern(self): """ pid is right but the name pattern does not match the init process. """ self.assertFalse(is_process_running(1, "openquake"))
def test_is_process_running_with_matching_pattern(self): """pid and name pattern should match the init process.""" if sys.platform == "darwin": self.assertTrue(is_process_running(1, "launchd")) else: self.assertTrue(is_process_running(1, "init"))
def test_is_process_running(self): """The init process should be found.""" self.assertTrue(is_process_running(1))