def start(request,id): """Starts a service running in the background""" if request.method == 'POST': form = ServiceStartForm(request.POST) if form.is_valid(): print "Handling a valid form" service_id = form.cleaned_data['serviceChoice'] input_dir = form.cleaned_data['input_directory'] output_dir = form.cleaned_data['output_directory'] aEntry = Service.objects.get(pk=service_id) aServiceRun = ServiceRun(service=aEntry, inputParams=input_dir, outputParams=output_dir) aServiceRun.save() # elevate permissions in order to run subprocess.call(["chmod", "a+x", aServiceRun.service.location]) process = subprocess.Popen(aServiceRun.service.location, shell=False) # process = subprocess.Popen("mvim", shell=False) # process = subprocess.Popen("mvim", shell=False, preexec_fn=os.setpgrp) #preexec_fn=os.setpgrp print 'process id of service is: %s', process.pid aServiceRun.status="RUNNING" aServiceRun.pid = process.pid aServiceRun.save() # Add to the hashtable ProcessManager.get_instance().set_process(aServiceRun.pid, process) print ProcessManager.get_process_dict() messages.add_message(request, messages.SUCCESS, 'Successfully Started') return HttpResponseRedirect('/service/') else: form = ServiceStartForm() print "Invalid form, not a POST request" return render(request, 'service/start.html', locals())
def stop(request, id): """Stops a service""" runningService = ServiceRun.objects.get(pk=id) # ret = subprocess.call(["kill", "-9", "%d" % runningService.pid]) # ret = os.killpg(runningService.pid, signal.SIGTERM) process = ProcessManager.get_instance().get_process(runningService.pid) # Kill the process p = process.terminate() process.wait() x = process.poll() ret = process.returncode if ret is not None: runningService.pid = -1 runningService.status = "STOPPED" runningService.save() messages.add_message(request, messages.SUCCESS, 'Service Successfully Stopped') else: messages.add_message(request, messages.ERROR, 'Cant stop the service check the logs') all_services = Service.objects.all() running_services = ServiceRun.objects.all().filter(status="RUNNING") return render(request, 'service/index.html', locals())