Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
child2 = Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
method='DummyTask',
state=TASK_STATES['ASSIGNED'],
parent=parent,
)
parent.wait([child1.id])
parent = Task.objects.get(id=parent.id)
child1 = Task.objects.get(id=child1.id)
child2 = Task.objects.get(id=child2.id)
self.assertEquals(parent.waiting, True)
self.assertEquals(parent.awaited, False)
self.assertEquals(child1.waiting, False)
self.assertEquals(child1.awaited, True)
self.assertEquals(child2.waiting, False)
self.assertEquals(child2.awaited, False)
def test_create_subtask(self):
t_parent = Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
state=TASK_STATES['FREE'],
)
req = _make_request(self._worker)
task_id = worker.create_subtask(req, 'Label', 'Method', None, t_parent.id)
self.assertTrue(task_id > 0)
t_child = Task.objects.get(id=task_id)
self.assertEqual(t_child.parent.id, t_parent.id)
self.assertEqual(t_child.label, 'Label')
self.assertEqual(t_child.method, 'Method')
)
t = Task.objects.create(
worker=w,
arch=self._arch,
channel=self._channel,
owner=self._user,
state=TASK_STATES['OPEN'],
)
req = _make_request(self._worker)
with self.assertRaises(Task.DoesNotExist):
worker.interrupt_tasks(req, [t.id])
t = Task.objects.get(id=t.id)
self.assertEqual(t.state, TASK_STATES['OPEN'])
parent=parent,
)
child2 = Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
state=TASK_STATES['OPEN'],
parent=parent,
)
canceled = parent.cancel_subtasks()
self.assertTrue(canceled)
child1 = Task.objects.get(id=child1.id)
child2 = Task.objects.get(id=child2.id)
self.assertEquals(child1.state, TASK_STATES['CANCELED'])
self.assertEquals(child2.state, TASK_STATES['CANCELED'])
def test_close_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.close_task()
task = Task.objects.get(id=task.id)
self.assertEquals(task.state, TASK_STATES['CLOSED'])
state=TASK_STATES['FREE'],
)
self.assertEqual(t.state, TASK_STATES['FREE'])
tm = TaskManager(conf={'worker': self._worker})
task_info = t.export(False)
with patch('kobo.worker.taskmanager.os', fork=Mock(return_value=9999)) as os_mock:
os_mock.devnull = os.devnull
tm.take_task(task_info)
os_mock.fork.assert_not_called()
# reload task info
t = Task.objects.get(id=t.id)
self.assertEqual(t.state, TASK_STATES['CLOSED'])
def get_task_no_verify(request, task_id):
"""
Get information about a task, do not verify whether is assigned to a worker.
@param task_id: a task ID
@type task_id: int
@rtype: dict
"""
task = Task.objects.get(id=task_id)
return task.export()
@param chunk_start: chunk start position in the file (-1 for append)
@type chunk_start: str
@param chunk_len: chunk length
@type chunk_len: str
@param chunk_checksum: sha256 checksum (lower case)
@type chunk_checksum: str
@param encoded_chunk: base64 encoded chunk
@type encoded_chunk: str
@rtype: bool
"""
relative_path = os.path.normpath(relative_path)
if relative_path.startswith(".."):
raise ValueError("Invalid upload path: %s" % relative_path)
task = Task.objects.get(id=task_id)
full_path = os.path.join(task.task_dir(), relative_path)
if task.state != TASK_STATES["OPEN"]:
raise ValueError("Can't upload file for a task which is not OPEN: %s" % task_id)
try:
decode_xmlrpc_chunk(chunk_start, chunk_len, chunk_checksum, encoded_chunk, write_to=full_path, mode=mode)
except ValueError:
return False
return True
def cancel_task(request, task_id):
try:
task = models.Task.objects.get(id=task_id)
except ObjectDoesNotExist:
return "Specified task %s does not exist." % task_id
return task.cancel_task(user=request.user)