5 from django.db import transaction
6 from django.utils.functional import SimpleLazyObject
7 from django.utils.timezone import now
9 from pyqcg.description import JobDescription
10 from pyqcg.service import Registry
11 from pyqcg.utils import Credential, TimePeriod, JobStatus, TaskStatus
13 from qcg.models import User, Job, Task, Allocation, NodeInfo
16 logger = logging.getLogger(__name__)
20 def update_user_data(user, proxy):
24 registry = Registry(Credential(proxy))
26 # put lock on user record (hopefully..?)
27 user = User.objects.select_for_update().get(pk=user.pk)
28 last_update = user.last_update
30 changed_filter = {'changed': TimePeriod(after=last_update)}
32 ###################################
34 ###################################
36 jobs = registry.jobs(stats=[v for k, v in vars(JobStatus).iteritems() if not k.startswith('__')],
42 params = Job.qcg_map(qcg_job, user)
43 job_id = params.pop('job_id')
45 Job.objects.update_or_create(job_id=job_id, defaults=params)
48 ###################################
50 ###################################
52 tasks = registry.tasks(stats=[v for k, v in vars(TaskStatus).iteritems() if not k.startswith('__')],
56 jobs_cache = SimpleLazyObject(lambda: {j.job_id: j for j in Job.objects.filter(owner=user)})
58 for qcg_task in tasks:
59 params = Task.qcg_map(qcg_task, jobs_cache[qcg_task.job_id])
60 task_id = params.pop('task_id')
62 task, created = Task.objects.update_or_create(job__job_id=qcg_task.job_id, task_id=task_id, defaults=params)
65 task.allocations.all().delete()
67 for qcg_alloc in qcg_task.allocations:
68 alloc = task.allocations.create(**Allocation.qcg_map(qcg_alloc))
70 for qcg_node in qcg_alloc.nodes:
71 alloc.nodes.create(**NodeInfo.qcg_map(qcg_node))
76 user.last_update = now()
79 elapsed = time.time() - ts
80 elapsed_jobs = jte - jts
81 elapsed_tasks = tte - tts
82 elapsed_py = elapsed - elapsed_jobs - elapsed_tasks
83 logger.info('(%.3f) USER = %s, LAST_UPDATE = %s, JOBS = %s (%.3f), TASKS = %s (%.3f), TIME = %.3f',
84 elapsed, user, last_update, jobs_count, elapsed_jobs, task_count, elapsed_tasks, elapsed_py)
88 def update_job(job, proxy):
89 if job.terminated and job.purged:
95 credential = Credential(proxy)
97 qcg_job.credential = credential
100 qcg_job.refresh_information()
101 elapsed_job = time.time() - jts
103 for name, val in job.qcg_map(qcg_job, job.owner).iteritems():
104 if getattr(job, name) != val:
105 setattr(job, name, val)
110 for task in job.tasks.all():
111 qcg_task = task.qcg_task
112 qcg_task.credential = credential
115 qcg_task.refresh_information()
116 elapsed_tasks += time.time() - tts
118 for name, val in task.qcg_map(qcg_task, task.job).iteritems():
119 if getattr(task, name) != val:
120 setattr(task, name, val)
123 task.allocations.all().delete()
125 for qcg_alloc in qcg_task.allocations:
126 alloc = task.allocations.create(**Allocation.qcg_map(qcg_alloc))
128 for qcg_node in qcg_alloc.nodes:
129 alloc.nodes.create(**NodeInfo.qcg_map(qcg_node))
131 elapsed = time.time() - ts
132 elapsed_py = elapsed - elapsed_job - elapsed_tasks
133 logger.info('(%.3f) JOB = %s (%.3f), TASKS = %d (%.3f), TIME = %.3f',
134 elapsed, job.job_id, elapsed_job, job.tasks.count(), elapsed_tasks, elapsed_py)
137 def submit_job(params, proxy):
139 desc = JobDescription(Credential(proxy))
141 direct_map = ('env_variables', 'executable', 'arguments', 'note', 'grant', 'hosts', 'properties', 'queue', 'procs',
142 'wall_time', 'memory', 'memory_per_slot', 'modules', 'input', 'stage_in', 'native', 'notify',
143 'preprocess', 'postprocess', 'persistent')
145 for name in direct_map:
147 setattr(desc, name, params[name])
149 if params['application']:
150 desc.set_application(*params['application'])
151 desc.stage_in += [params['master_file']]
152 desc.arguments.insert(0, os.path.basename(params['master_file']))
154 desc.set_nodes(*params['nodes'])
155 if params['reservation']:
156 desc.set_reservation(params['reservation'])
157 if params['watch_output']:
158 desc.set_watch_output(params['watch_output'], params['watch_output_pattern'])
162 # for prop in direct_map + ('application', 'nodes', 'env_variables', 'reservation', 'watch_output'):
163 # print prop, type(getattr(desc, prop)), repr(getattr(desc, prop))
165 # print desc.xml_description
172 def cancel(obj, proxy):
176 qcg_obj = obj.qcg_job if isinstance(obj, Job) else obj.qcg_task
177 qcg_obj.credential = Credential(proxy)
181 elapsed_cancel = time.time() - jts
183 elapsed = time.time() - ts
184 elapsed_py = elapsed - elapsed_cancel
185 logger.info('(%.3f) OBJ = %s (%.3f), TIME = %.3f', elapsed, obj, elapsed_cancel, elapsed_py)
188 def clean(obj, proxy):
192 qcg_obj = obj.qcg_job if isinstance(obj, Job) else obj.qcg_task
193 qcg_obj.credential = Credential(proxy)
197 elapsed_clean = time.time() - jts
199 elapsed = time.time() - ts
200 elapsed_py = elapsed - elapsed_clean
201 logger.info('(%.3f) OBJ = %s (%.3f), TIME = %.3f', elapsed, obj, elapsed_clean, elapsed_py)