def add_new_job(self, identifier, image_name, callback, tasks): """ Add a new job to the job queue :param identifier: The identifier for the job :param image_name: The name of the image to run each task :param callback: The callback URL to report results :param tasks: The individual tasks to run """ if not tasks: self._logger.error( 'No tasks provided when submitting job {i}'.format( i=identifier)) raise ValueError('Tasks must be provided with the job') self._logger.info('Adding job {i} to the queue'.format(i=identifier)) self._job_db.add_job(identifier, image_name, callback) self._jobs.add(identifier) for t in tasks: self._tasks.appendleft( TaskEntry(identifier, t['task_name'], t['task_args'], image_name, None, None)) self._job_db.add_tasks(identifier, tasks)
class TupleTest(TestCase): def setUp(self): task = Task.objects.create(name='new task') self.te = TaskEntry(task=task) def test_time(self): self.te.start() c = self.te.clock_started t = self.te.time_record hour,min,sec = t.hour,t.minute,t.second formatted_time = time(hour,min,sec) def test_date(self): self.te.start() datetime.combine(self.te.date_field,self.format_time(self.te.time_record)) def format_time(self,t): hour,min,sec = t.hour,t.minute,t.second return time(hour,min,sec) def test_subtract_dates(self): self.te.start() df = self.te.date_field ft = self.format_time(self.te.time_record) diff = datetime.now() - datetime.combine(df,ft) diff.seconds def test_edit_data(self): dt = datetime.strptime('2016-01-21T22:50',"%Y-%m-%dT%H:%M") time = dt.time() date = dt.date()
def _scan_for_dead_jobs(self): def item_filter(entry: TaskEntry): return entry.task_id not in self._overdue_tasks while True: time.sleep(self.DEAD_SCAN_INTERVAL) with self._lock: overdue = [ t for t in self._running_tasks if datetime.datetime.now() - t.started > self.DEAD_JOB_INTERVAL ] for task in overdue: self._overdue_tasks.add(task.task_id) self._tasks.appendleft( TaskEntry(task.identifier, task.name, task.args, task.image, None, None)) self._running_tasks = list( filter(item_filter, self._running_tasks)) self._signal_should_run()
def set_details(entry: TaskEntry): if entry.identifier != identifier or entry.name != name: return entry return TaskEntry(entry.identifier, entry.name, entry.args, entry.image, task_id, datetime.datetime.now())
def start_experiment(request, save=True): ''' Handles presses of the 'Start Experiment' and 'Test' buttons in the browser interface ''' #make sure we don't have an already-running experiment if exp_tracker.status.value != '': http_request_queue.append((request, save)) return _respond(dict(status="running", msg="Already running task, queuelen=%d!" % len(http_request_queue))) # Try to start the task, and if there are any errors, send them to the browser interface try: data = json.loads(request.POST['data']) task = Task.objects.get(pk=data['task']) Exp = task.get(feats=data['feats'].keys()) entry = TaskEntry(subject_id=data['subject'], task=task) params = Parameters.from_html(data['params']) entry.params = params.to_json() kwargs = dict(subj=entry.subject, task_rec=task, feats=Feature.getall(data['feats'].keys()), params=params) # Save the target sequence to the database and link to the task entry, if the task type uses target sequences if issubclass(Exp, experiment.Sequence): print "creating seq" print "data['sequence'] POST data" print data['sequence'] seq = Sequence.from_json(data['sequence']) seq.task = task if save: seq.save() entry.sequence = seq kwargs['seq'] = seq else: entry.sequence_id = -1 response = dict(status="testing", subj=entry.subject.name, task=entry.task.name) if save: # Save the task entry to database entry.save() # Link the features used to the task entry for feat in data['feats'].keys(): f = Feature.objects.get(pk=feat) entry.feats.add(f.pk) response['date'] = entry.date.strftime("%h %d, %Y %I:%M %p") response['status'] = "running" response['idx'] = entry.id # Give the entry ID to the runtask as a kwarg so that files can be linked after the task is done kwargs['saveid'] = entry.id # Start the task FSM and exp_tracker exp_tracker.runtask(**kwargs) # Return the JSON response return _respond(response) except Exception as e: # Generate an HTML response with the traceback of any exceptions thrown import cStringIO import traceback err = cStringIO.StringIO() traceback.print_exc(None, err) err.seek(0) return _respond(dict(status="error", msg=err.read()))
def setUp(self): task = Task.objects.create(name='new task') self.te = TaskEntry(task=task)