Skip to content

Commit

Permalink
Updated panda client for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Foorth committed Sep 20, 2022
1 parent 23421a4 commit e66f81d
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 158 deletions.
11 changes: 10 additions & 1 deletion core/oauth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,13 @@ def deny_rights(request, rtype):
return False


return True
return True

def get_auth_provider(request):
user = request.user

if user.is_authenticated and user.social_auth is not None:
auth_provider = (request.user.social_auth.get()).provider
else:
auth_provider = None
return auth_provider
7 changes: 1 addition & 6 deletions core/panda_client/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

from core.panda_client import views as panda_client

app_name = "bigpandamon"

urlpatterns = [
re_path(r'^panda_client/get_pandaserver_attr/$', panda_client.get_pandaserver_attr, name='get_pandaserver_attr'),
re_path(r'^panda_client/setNumSlots/$', panda_client.setNumSlots, name='setNumSlots'),
# re_path(r'^killTask/(?P<jeditaskid>.*)/$', panda_client.killTask, name='killTask')
# re_path(r'^finishTask/(?P<jeditaskid>.*)/$', panda_client.finishTask, name='finishTask')
re_path(r'^panda_client/$', panda_client.client, name='panda_client'),
]
67 changes: 63 additions & 4 deletions core/panda_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import time

from requests import post
from core.oauth.utils import get_auth_provider
baseURL = 'https://pandaserver.cern.ch/server/panda'
def get_auth_indigoiam(request):
header = {}
organisation = 'atlas'
user = request.user

if user.is_authenticated and user.social_auth is not None:
auth_provider = (request.user.social_auth.get()).provider
auth_provider = get_auth_provider(request)

if auth_provider:
social = request.user.social_auth.get(provider=auth_provider)
if (auth_provider == 'indigoiam'):
if (social.extra_data['auth_time'] + social.extra_data['expires_in'] - 10) <= int(time.time()):
Expand All @@ -23,6 +25,63 @@ def get_auth_indigoiam(request):

return header

def kill_task(auth, jeditaskid):
"""Kill a task
request parameters:
jediTaskID: jediTaskID of the task to be killed
"""
if jeditaskid is not None:
data = {}

data['jediTaskID'] = jeditaskid
data['properErrorCode'] = True

url = baseURL + '/killTask'

try:
resp = post(url, headers=auth, data=data)
resp = resp.text
except Exception as ex:
resp = "ERROR killTask: %s %s" % (ex, resp.status_code)
else:
resp = 'Jeditaskid is not defined'

return resp


def finish_task(auth, jeditaskid, soft=True):
"""Finish a task
request parameters:
jediTaskID: jediTaskID of the task to be finished
soft: If True, new jobs are not generated and the task is
finihsed once all remaining jobs are done.
If False, all remaining jobs are killed and then the
task is finished
"""
if jeditaskid is not None:

data = {}

data['jediTaskID'] = jeditaskid
data['properErrorCode'] = True
data['soft'] = soft


url = baseURL + '/finishTask'

try:
resp = post(url, headers=auth, data=data)
resp = resp.text
except Exception as ex:
resp = "ERROR finishTask: %s %s" % (ex, resp.status_code)
else:
resp = 'Jeditaskid is not defined'

return resp

### TODO change it later
# def pandaclient_initialization(request):
# user = request.user
Expand Down
132 changes: 15 additions & 117 deletions core/panda_client/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from requests import get, post
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_exempt

from core.oauth.utils import login_customrequired
from core.panda_client.utils import get_auth_indigoiam
from core.panda_client.utils import get_auth_indigoiam, kill_task, finish_task
from core.views import initRequest

baseURL = 'https://pandaserver.cern.ch/server/panda'
Expand All @@ -27,128 +28,25 @@ def get_pandaserver_attr(request):
resp = auth['detail']

return HttpResponse(resp, content_type='application/json')

# kill task
@login_customrequired
@never_cache
def killTask(request):
"""Kill a task
request parameters:
jediTaskID: jediTaskID of the task to be killed
"""
valid, response = initRequest(request)
if not valid:
return response

auth = get_auth_indigoiam(request)

if auth is not None and ('Authorization' in auth and 'Origin' in auth):
if 'jeditaskid' in request.session['requestParams'] and request.session['requestParams']['jeditaskid'] is not None:
data = {}

data['jediTaskID'] = request.session['requestParams']['jeditaskid']
data['properErrorCode'] = True

url = baseURL + '/killTask'

try:
resp = post(url, headers=auth, data=data)
except Exception as ex:
resp = "ERROR killTasl: %s %s" % (ex, resp.status_code)
else:
resp = 'jeditaskid is not defined'
else:
resp = auth['detail']

return HttpResponse(resp, content_type='application/json')

@login_customrequired
@never_cache
def finishTask(request):
"""Finish a task
request parameters:
jediTaskID: jediTaskID of the task to be finished
soft: If True, new jobs are not generated and the task is
finihsed once all remaining jobs are done.
If False, all remaining jobs are killed and then the
task is finished
"""
@csrf_exempt
def client(request):
valid, response = initRequest(request)
if not valid:
return response

auth = get_auth_indigoiam(request)

info = {}
if auth is not None and ('Authorization' in auth and 'Origin' in auth):
if 'jeditaskid' in request.session['requestParams'] and request.session['requestParams']['jeditaskid'] is not None:

data = {}
data['jediTaskID'] = request.session['requestParams']['jeditaskid']

data['properErrorCode'] = True

if 'soft' in request.session['requestParams'] and bool(request.session['requestParams']['soft']) == True:
data['soft'] = True
if len(request.session['requestParams']) > 0:
data = request.session['requestParams']
if data['action'] == 'finishtask' and ('task' in data and data['task'] is not None):
info = finish_task(auth=auth, jeditaskid=data['task'])
elif data['action'] == 'killtask' and ('task' in data and data['task'] is not None):
info = kill_task(auth=auth, jeditaskid=data['task'])
else:
data['soft'] = False

url = baseURL + '/finishTask'

try:
resp = post(url, headers=auth, data=data)
except Exception as ex:
resp = "ERROR finishTask: %s %s" % (ex, resp.status_code)
info = 'Operation error'
else:
resp = 'jeditaskid is not defined'
info = 'Request body is empty'
else:
resp = auth['detail']

return HttpResponse(resp, content_type='application/json')

@login_customrequired
@never_cache
def setNumSlots(request):
"""Finish a task
request parameters:
pandaQueueName: string
numSlots: int
gshare: string
resourceType: string
validPeriod: int (number of days)
"""
valid, response = initRequest(request)
if not valid:
return response

auth = get_auth_indigoiam(request)
details = {}
if auth is not None and ('Authorization' in auth and 'Origin' in auth):
data = {}
if 'pandaqueuename' in request.session['requestParams'] and request.session['requestParams']['pandaqueuename'] is not None:
data['pandaQueueName'] = request.session['requestParams']['pandaqueuename']
if 'gshare' in request.session['requestParams'] and request.session['requestParams']['gshare'] is not None:
data['gshare'] = request.session['requestParams']['gshare']
if 'resourcetype' in request.session['requestParams'] and request.session['requestParams']['resourcetype'] is not None:
data['resourceType'] = request.session['requestParams']['resourcetype']
if 'numslots' in request.session['requestParams'] and request.session['requestParams']['numslots'] is not None:
data['numSlots'] = request.session['requestParams']['numslots']
if 'validperiod' in request.session['requestParams'] and request.session['requestParams']['validperiod'] is not None:
data['validPeriod'] = request.session['requestParams']['validperiod']
url = baseURL + '/setNumSlotsForWP'
try:
resp = post(url, headers=auth, data=data)
details['code'] = resp.status_code
details['text'] = resp.text
details['auth_details'] = auth
details['data'] = data
except Exception as ex:
resp = "ERROR setNumSlots: %s %s" % (ex, resp.status_code)
details['message'] = resp
else:
resp = auth['detail']
details['message'] = resp
info = auth['detail']

return HttpResponse(json.dumps(details), content_type='application/json')
return HttpResponse(info, content_type='text/html')
29 changes: 28 additions & 1 deletion core/templates/taskInfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,13 @@ <h5>Logged status: {{ task.errordialog|safe }}</h5>
{% with 'done finished failed ready broken aborted defined' as finalstatelist %}
{% if not task.status in finalstatelist %}
<div style="float: right">
{% if 'indigoiam' in authtype %}
<a onclick="javascript:killtasks_token({{ task.jeditaskid }}, 'finishtask');" class='bp-button kill'>Finish (PanDA client)</a>
<a onclick="javascript:killtasks_token({{ task.jeditaskid }}, 'killtask');" class='bp-button kill'>Abort (PanDA client)</a>
{% else %}
<a onclick="killtasks({{ task.jeditaskid }}, 0);" class='bp-button kill'>Finish</a>
<a onclick="killtasks({{ task.jeditaskid }}, 1);" class='bp-button kill'>Abort</a>
{% endif %}
</div>
{% endif %}
{% endwith %}
Expand Down Expand Up @@ -1134,6 +1139,28 @@ <h5>Warning! {{ warning.memoryleaksuspicion.message }}</h5>
}
}

function killtasks_token(taskID, action) {
var message = '';
if (action === 0)
message = 'Are you sure you want to FINISH this task?'
else
message = 'Are you sure you want to ABORT this task?'
if (confirm(message)) {
$.ajax({
url: {% url 'panda_client' %},
data: {task: taskID, action: action},
async: false,
type: "POST",
success: function(msg){
alert(msg);
}
})
} else {
// Do nothing!
}
}


function buildDatasetsTable(data) {

$('#datasetstable').dataTable({
Expand Down Expand Up @@ -1175,11 +1202,11 @@ <h5>Warning! {{ warning.memoryleaksuspicion.message }}</h5>
let str = '';
if (data > 0) {
str += '<a href = "{% url 'fileList' %}?jeditaskid=' + full['jeditaskid'] + '&datasetid=' + full['datasetid'] + '">' + full['nfiles'] + '</a>';
if (full['nfilesmissing'] > 0) {str += ' <span class="lost">+' + full['nfilesmissing'] + ' lost</span>'; }
}
else {
str += data;
}
if (full['nfilesmissing'] > 0) {str += ' <span class="lost">+' + full['nfilesmissing'] + ' lost</span>'; }
return str
}
},
Expand Down
Loading

0 comments on commit e66f81d

Please sign in to comment.