Skip to content

Commit

Permalink
cleanup tmp status-cache files. Fix #5128 (#5129)
Browse files Browse the repository at this point in the history
* cleanup tmp status-cache files. Fix #5128

* more compact

* remove temp files after use also in CrabRestInterface

* pylint

* ensure that file creates in /tmp starts with crab_
  • Loading branch information
belforte authored Dec 9, 2021
1 parent dfec8a5 commit f479373
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/python/CRABClient/Commands/checkwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def getPFN(self, site='T2_CH_CERN', lfn='/store/user', username='jdoe'):
"""
rucioScript = template.format(site=site, username=username, lfn=lfn)
import tempfile
(_, scriptName) = tempfile.mkstemp(dir='/tmp', prefix='lfn2pfn-', suffix='.py')
(_, scriptName) = tempfile.mkstemp(dir='/tmp', prefix='crab_lfn2pfn-', suffix='.py')
with open(scriptName, 'w') as ofile:
ofile.write(rucioScript)
cmd = 'eval `scram unsetenv -sh`; '
Expand Down
7 changes: 5 additions & 2 deletions src/python/CRABClient/Commands/status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import division # I want floating points
from __future__ import print_function

import os
import pickle
import sys
import math
Expand Down Expand Up @@ -112,8 +113,8 @@ def __call__(self):
self.logger.debug("Proxied webdir is located at %s", proxiedWebDir)

# Download status_cache file
_, local_status_cache_txt = tempfile.mkstemp(dir='/tmp', prefix='status-cache-', suffix='.txt')
_, local_status_cache_pkl = tempfile.mkstemp(dir='/tmp', prefix='status-cache-', suffix='.pkl')
_, local_status_cache_txt = tempfile.mkstemp(dir='/tmp', prefix='crab_status-cache-', suffix='.txt')
_, local_status_cache_pkl = tempfile.mkstemp(dir='/tmp', prefix='crab_status-cache-', suffix='.pkl')
gotPickle = False
gotTxt = False
# first: try pickle version
Expand All @@ -126,6 +127,7 @@ def __call__(self):
raise Exception("failed to retrieve %s" % url)
with open(local_status_cache_pkl, PKL_R_MODE) as fp:
statusCache = pickle.load(fp)
os.remove(local_status_cache_pkl)
if 'bootstrapTime' in statusCache :
statusCacheInfo_PKL = None
bootstrapMsg_PKL = "Task bootstrapped at %s" % statusCache['bootstrapTime']['date']
Expand All @@ -151,6 +153,7 @@ def __call__(self):
raise Exception("failed to retrieve %s" % url)
with open(local_status_cache_txt, 'r') as fp:
statusCacheData = fp.read()
os.remove(local_status_cache_txt)
# Normally the first two lines of the file contain the checkpoint locations
# for the job_log / fjr_parse_results files and are used by the status caching script.
# But if the job has just bootstrapped the first lines of the file are:
Expand Down
20 changes: 10 additions & 10 deletions src/python/CRABClient/CrabRestInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@

import os
import random
from CRABClient.ClientUtilities import execute_command
from ServerUtilities import encodeRequest
import json
import re
import tempfile

from CRABClient.ClientExceptions import RESTInterfaceException
import logging

try:
from urllib import quote as urllibQuote # Python 2.X
except ImportError:
from urllib.parse import quote as urllibQuote # Python 3+

import logging
from CRABClient.ClientUtilities import execute_command
from ServerUtilities import encodeRequest
from CRABClient.ClientExceptions import RESTInterfaceException

try:
from TaskWorker import __version__
Expand Down Expand Up @@ -164,9 +163,9 @@ def makeRequest(self, uri=None, data=None, verb='GET'):
# if it is a dictionary, we need to encode it to string
if isinstance(data, dict):
data = encodeRequest(data)
self.logger.debug("Encoded data for curl request: %s" %data)
self.logger.debug("Encoded data for curl request: %s", data)

fh, path = tempfile.mkstemp(dir='/tmp', prefix='curlData')
fh, path = tempfile.mkstemp(dir='/tmp', prefix='crab_curlData')
with open(path, 'w') as f:
f.write(data)

Expand Down Expand Up @@ -199,15 +198,17 @@ def makeRequest(self, uri=None, data=None, verb='GET'):
# this was the last retry
msg = "Fatal error trying to connect to %s using %s." % (url, data)
self.logger.info(msg)
os.remove(path)
raise RESTInterfaceException(stderr)
else:
try:
curlResult = json.loads(stdout)
break
except Exception as ex:
msg = "Fatal error reading data from %s using %s: \n%s" % (url, data, ex)
raise Exception(msg)
else:
break
finally:
os.remove(path)

return curlResult, http_code, http_reason

Expand Down Expand Up @@ -265,4 +266,3 @@ def put(self, api=None, data=None):
def delete(self, api=None, data=None):
uri = self.uriNoApi + api
return self.server.delete(uri, data)

0 comments on commit f479373

Please sign in to comment.