

def run(job):
    job_model = ModelImporter.model('job', 'jobs')
    job_model.updateJob(job, status=JobStatus.RUNNING)
    try:
        kwargs = job['kwargs']
        token = kwargs['token']
        datasetId = str(kwargs['dataset']['_id'])
        girderPort = config.getConfig()['server.socket_port']
        client = girder_client.GirderClient(port=girderPort)
        client.token = token['_id']
        source = client.getItem(kwargs['params']['sourceId'])
        esUrl = ('https://%s@%s' % (decryptCredentials(source['meta']['minerva']['elasticsearch_params']['credentials']), source['meta']['minerva']['elasticsearch_params']['host_name']))
        es = Elasticsearch([esUrl])
        searchResult = es.search(index=source['meta']['minerva']['elasticsearch_params']['index'], body=json.loads(kwargs['params']['searchParams']))
        tmpdir = tempfile.mkdtemp()
        outFilepath = tempfile.mkstemp(suffix='.json', dir=tmpdir)[1]
        writer = open(outFilepath, 'w')
        writer.write(json.dumps(searchResult))
        writer.close()
        outFilename = 'search.json'
        humanFilepath = os.path.join(tmpdir, outFilename)
        shutil.move(outFilepath, humanFilepath)
        client.uploadFileToItem(datasetId, humanFilepath)
        user_model = ModelImporter.model('user')
        user = user_model.load(job['userId'], force=True)
        item_model = ModelImporter.model('item')
        dataset = item_model.load(datasetId, level=AccessType.WRITE, user=user)
        metadata = dataset['meta']
        minerva_metadata = metadata['minerva']
        file_model = ModelImporter.model('file')
        existing = file_model.findOne({
            'itemId': dataset['_id'],
            'name': outFilename,
        })
        if existing:
            minerva_metadata['original_files'] = [{
                '_id': existing['_id'],
                'name': outFilename,
            }]
        else:
            raise Exception(('Cannot find file %s in dataset %s' % (girderPort, datasetId)))
        jsonRow = jsonArrayHead(humanFilepath, limit=1)[0]
        minerva_metadata['json_row'] = jsonRow
        shutil.rmtree(tmpdir)
        metadata['minerva'] = minerva_metadata
        item_model.setMetadata(dataset, metadata)
        job_model.updateJob(job, status=JobStatus.SUCCESS)
    except Exception:
        (t, val, tb) = sys.exc_info()
        log = ('%s: %s\n%s' % (t.__name__, repr(val), traceback.extract_tb(tb)))
        job_model.updateJob(job, status=JobStatus.ERROR, log=log)
        raise

dataset/ETHPy150Open Kitware/minerva/server/jobs/elasticsearch_worker.py run/VarMisuse@426/624 `outFilename`->`girderPort`