Skip to content

Commit

Permalink
Merge pull request #51 from haz/main
Browse files Browse the repository at this point in the history
Command-line time limit
  • Loading branch information
nirlipo authored Jan 3, 2024
2 parents fd48cff + 63f0376 commit 4539cd6
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions server/celery-queue/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def track_celery(method):
"""
This decorator measures celery task meta data and store it in Mysql db.
Usage:
Decorate your functions like this:
@track_celery
Expand All @@ -56,7 +56,7 @@ def download_file( url: str, dst: str):
r = requests.get(url)
with open(dst, 'wb') as f:
f.write(r.content)

def retrieve_output_file(target_file:dict, folder):
file_pattern=os.path.join(folder, target_file["files"])
file_list=glob.glob(file_pattern)
Expand Down Expand Up @@ -91,7 +91,7 @@ def write_to_temp_file(name:str, data:str, folder:str):

# problem_file = f'{tmpfolder}/{os.path.basename(problem_url)}'
# download_file(problem_url, problem_file)

# # Will generate a single output file (the plan) which is returned via HTTP
# command = f"{solver} {domain_file} {problem_file}"
# res = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
Expand All @@ -101,20 +101,20 @@ def write_to_temp_file(name:str, data:str, folder:str):
# # remove the tmp/fies once we finish
# os.remove(domain_file)
# os.remove(problem_file)

# plan = retrieve_output_file(PACKAGES[solver]['endpoint']['services']['solve']['return']['file'], tmpfolder)

# shutil.rmtree(tmpfolder)

# return {'stdout': res.stdout, 'stderr': res.stderr, 'plan':plan}

# Running generic planutils packages with no solver-specific assumptions

@celery.task(name='tasks.run.package',soft_time_limit=TIME_LIMIT,bind=True)
@celery.task(name='tasks.run.package',soft_time_limit=TIME_LIMIT+10,bind=True)
@track_celery
def run_package(self, package: str, arguments:dict, call:str, output_file:dict,**kwargs):


try:
tmpfolder = tempfile.mkdtemp()
# Write files and replace args in the call string
Expand All @@ -125,10 +125,11 @@ def run_package(self, package: str, arguments:dict, call:str, output_file:dict,*
# k is a file, we want to replace with the file path
call = call.replace("{%s}" % k, k)
else:
# k needs to be replaced with the value
# k needs to be replaced with the value
call = call.replace("{%s}" % k, str(v['value']))

# Run the command
# add the timeout to the call
call = f"timeout {TIME_LIMIT} planutils run {call}"
res = subprocess.run(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
executable='/bin/bash', encoding='utf-8',
shell=True, cwd=tmpfolder)
Expand All @@ -139,4 +140,4 @@ def run_package(self, package: str, arguments:dict, call:str, output_file:dict,*
result={"stdout":res.stdout, "stderr":res.stderr, "call":call, "output":output,"output_type":output_file["type"]}
return result,arguments
except SoftTimeLimitExceeded as e:
return {"stdout":"Request Time Out", "stderr":"", "call":call, "output":{},"output_type":output_file["type"]},arguments
return {"stdout":"Request Time Out", "stderr":"", "call":call, "output":{},"output_type":output_file["type"]},arguments

0 comments on commit 4539cd6

Please sign in to comment.