Commit 6243a4ab authored by Wout De Nolf's avatar Wout De Nolf

[oarjob] quick start a job

parent 7d1f81d4
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quick start\n",
"\n",
"## Hello world\n",
"OAR job that prints \"Hello world\" to the standard output.\n",
"\n",
"### Minimal"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"......................\n",
"Succes:\n",
"Hello word\n",
"\n"
]
}
],
"source": [
"from oarpy import oarjob\n",
"\n",
"jobdef = oarjob.JobFactory(command='echo \"Hello word\"')\n",
"job = jobdef.submit()\n",
"job.wait()\n",
"\n",
"if job.exit_code:\n",
" print('Failed:\\n{}'.format(job.stderr))\n",
"elif job.exit_code is None:\n",
" print('Interrupted:\\n{}'.format(job.stdout))\n",
"else:\n",
" print('Succes:\\n{}'.format(job.stdout))\n",
"job.remove_logs()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### With resources and postponed execution"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"......................................\n",
"Succes:\n",
"Hello word\n",
"\n"
]
}
],
"source": [
"from oarpy import oarjob\n",
"from oarpy.oarresource import Resource\n",
"\n",
"resource = Resource(core=1,walltime={'hours':1,'minutes':30},gpu=False)\n",
"jobdef = oarjob.JobFactory(name='helloworld',project='oarpy',\n",
" command='echo \"Hello word\"',resource=resource)\n",
"\n",
"job = jobdef.submit(hold=True)\n",
"job.wait(states='Hold')\n",
"# job is waiting for you to resume it\n",
"job.resume()\n",
"job.wait()\n",
"\n",
"if job.exit_code:\n",
" print('Failed:\\n{}'.format(job.stderr))\n",
"elif job.exit_code is None:\n",
" print('Interrupted:\\n{}'.format(job.stdout))\n",
"else:\n",
" print('Succes:\\n{}'.format(job.stdout))\n",
"job.remove_logs()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Job management\n",
"\n",
"Find all jobs started in the last 5 minutes:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Job(1122759), Job(1122760), Job(1124796), Job(1125514), Job(1125515), Job(1125516), Job(976220)]\n",
"set([(u'denolf', u'Finishing'), (u'denolf', u'Error'), (u'in1081', u'Running'), (u'denolf', u'Terminated'), (u'ljacques', u'Error'), (u'in1096', u'Waiting')])\n"
]
}
],
"source": [
"from oarpy import oarjob\n",
"from oarpy import timeutils\n",
"\n",
"start = timeutils.add(timeutils.now(),minutes=-1)\n",
"jobs = oarjob.search(start=start)\n",
"print(sorted(jobs))\n",
"print(set([(job['owner'],job.status) for job in jobs]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve job definition (can be used to resubmit a job):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial request:\n",
" \n",
"\n",
"Wanted resources:\n",
" -l \"{type = 'default'}/host=1/core=16,walltime=16:0:0\" \n",
"\n",
"Properties:\n",
" (((((gpu='YES') AND desktop_computing = 'NO') AND cluster = 'NICE') AND opsys = 'debian8') AND interactive = 'MIXED') AND drain='NO'\n",
"\n",
"Job resource:\n",
" -l nodes=1/core=16,walltime=16:00:00 -p \"gpu='YES' and drain='NO'\"\n",
"\n",
"Job definition:\n",
" -n Gecko_2_0p7um_2474_3031__001_.par --project default -d /mntdirect/_data_visitor/md1189/id17/GeckosHR/Gecko_2/Gecko_2_0p7um_2474_3031__001_/Slices -O OAR.Gecko_2_0p7um_2474_3031__001_.par.%jobid%.stdout -E OAR.Gecko_2_0p7um_2474_3031__001_.par.%jobid%.stderr -l nodes=1/core=16,walltime=16:00:00 -p \"gpu='YES' and drain='NO'\" /mntdirect/_data_visitor/md1189/id17/GeckosHR/Gecko_2/Gecko_2_0p7um_2474_3031__001_/Slices//./tmpmd1189.sh\n",
"\n"
]
}
],
"source": [
"from oarpy import oarjob\n",
"\n",
"job = oarjob.Job(1103714)\n",
"if job.exists:\n",
" jobdef = job.definition\n",
" stats = job.stats\n",
" print('Initial request:\\n {}\\n'.format(stats['initial_request']))\n",
" print('Wanted resources:\\n {}\\n'.format(stats['wanted_resources']))\n",
" print('Properties:\\n {}\\n'.format(stats['properties']))\n",
" print('Job resource:\\n {}\\n'.format(jobdef.resource))\n",
" print('Job definition:\\n {}\\n'.format(jobdef))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remove the log files of all succesfully finished jobs of a particular user and project:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from oarpy import oarjob\n",
"jobs = oarjob.search(owner='testuser',project='oarpy',state='Terminated')\n",
"for job in jobs:\n",
" job.remove_logs()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
......@@ -424,10 +424,15 @@ class JobFactory(object):
"""
def __init__(self, command=None, resource=None, name=None, project=None,
working_directory=None, log_directory=None, log_base=None):
working_directory=None, log_directory=None, log_base=None, **resource_parameters):
self._name = None
self._log_base = None
self.command = command
if resource_parameters:
if resource:
logger.warning('Skip resource parameters {}'.format(resource_parameters))
else:
resource = oarresource.Resource(**resource_parameters)
self.resource = resource
self.name = name
self.project = project
......@@ -522,6 +527,11 @@ class JobFactory(object):
return Job(jobid)
def submit(hold=False, **parameters):
jobdef = JobFactory(**parameters)
return jobdef.submit(hold=hold)
def search(name=None, project=None, owner=None, state=None,
start=None, end=None, **properties):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment