Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_timeout_task(self):
task = Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['OPEN'],
)
task.timeout_task()
task = Task.objects.get(id=task.id)
self.assertEquals(task.state, TASK_STATES['TIMEOUT'])
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['OPEN'],
)
Task.objects.create(
worker=worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['FREE'],
)
Task.objects.create(
worker=None,
arch=self._arch,
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['OPEN'],
)
tasks = worker.running_tasks()
self.assertEquals(len(tasks), 1)
def test_canceled(self):
self._create_task(worker=self._worker, state=TASK_STATES['FREE'])
t2 = self._create_task(worker=self._worker, state=TASK_STATES['CANCELED'])
t3 = self._create_task(worker=self._worker, state=TASK_STATES['CANCELED'])
t4 = self._create_task(worker=self._worker2, state=TASK_STATES['CANCELED'], exclusive=True)
tasks = Task.objects.canceled()
self.assertEquals(len(tasks), 3)
self.assertEquals(tasks[0].id, t4.id)
self.assertEquals(tasks[1].id, t2.id)
self.assertEquals(tasks[2].id, t3.id)
def test_get_and_verify_fails_if_different_worker(self):
task1 = Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['FREE'],
)
with self.assertRaises(Task.DoesNotExist):
Task.objects.get_and_verify(task1.id, self._worker2)
def test_view_big_json_iterate(self):
"""Iterates over log chunks based on next_poll and new_offset in responses,
simulating the LogWatcher JS behavior. Verifies that the full log can be assembled."""
# The task has to be finished for this test, otherwise it'll loop forever
task = Task.objects.get(id=TASK_ID)
task.state = TASK_STATES["CLOSED"]
task.save()
all_content = ''
offset = 0
for i in itertools.count():
self.assertTrue(i < 10000, 'infinite loop reading log?')
response, content = self.assertGetLog(
'big.log',
view_type='log-json',
test_content_length=False,
data={'offset': offset},
)
doc = json.loads(content)
def test_failed(self):
self._create_task(worker=self._worker, state=TASK_STATES['FREE'])
t2 = self._create_task(worker=self._worker, state=TASK_STATES['FAILED'])
t3 = self._create_task(worker=self._worker, state=TASK_STATES['FAILED'])
t4 = self._create_task(worker=self._worker2, state=TASK_STATES['FAILED'], exclusive=True)
tasks = Task.objects.failed()
self.assertEquals(len(tasks), 3)
self.assertEquals(tasks[0].id, t4.id)
self.assertEquals(tasks[1].id, t2.id)
self.assertEquals(tasks[2].id, t3.id)
def test_timeout(self):
self._create_task(worker=self._worker, state=TASK_STATES['FREE'])
t2 = self._create_task(worker=self._worker, state=TASK_STATES['TIMEOUT'])
t3 = self._create_task(worker=self._worker, state=TASK_STATES['TIMEOUT'])
t4 = self._create_task(worker=self._worker2, state=TASK_STATES['TIMEOUT'], exclusive=True)
tasks = Task.objects.timeout()
self.assertEquals(len(tasks), 3)
self.assertEquals(tasks[0].id, t4.id)
self.assertEquals(tasks[1].id, t2.id)
self.assertEquals(tasks[2].id, t3.id)
def assigned_tasks(self):
"""Return list of assigned tasks to this worker."""
return Task.objects.assigned().filter(worker=self)
"""get_tasks(task_id_list): list
@param task_id_list: list of task ids, can be empty, then all tasks are
retrieved
@type task_id_list: [int]
@param state_list: task state ids by which task_id_list should be
filtered
@type: [int]
@return: list of task_info dicts
@rtype: list
"""
if task_id_list:
tasks = models.Task.objects.filter(id__in=task_id_list)
else:
tasks = models.Task.objects.all()
if state_list:
tasks = tasks.filter(state__in=state_list)
return [i.export(flat=True) for i in tasks]
def save(self, *args, **kwargs):
# precompute task count, current load and ready
tasks = Task.objects.opened().filter(worker=self)
self.task_count = tasks.count()
self.current_load = sum(( task.weight for task in tasks if not task.waiting ))
self.ready = self.enabled and (self.current_load < self.max_load and self.task_count < 3*self.max_load)
while not self.worker_key:
# if worker_key is empty, generate a new one
key = random_string(64)
if Worker.objects.filter(worker_key=key).count() == 0:
self.worker_key = key
super(self.__class__, self).save(*args, **kwargs)