Skip to content

Commit

Permalink
backport mula remove try logic fixes (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
noamblitz authored Mar 20, 2023
1 parent 370a437 commit 3455447
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 69 deletions.
16 changes: 0 additions & 16 deletions mula/scheduler/schedulers/boefje.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def push_tasks_for_scan_profile_mutations(self) -> None:

def push_tasks_for_random_objects(self) -> None:
"""Push tasks for random objects from octopoes to the queue."""
tries = 0
while not self.queue.full():
time.sleep(1)

Expand Down Expand Up @@ -290,20 +289,6 @@ def push_tasks_for_random_objects(self) -> None:
continue

for boefje in boefjes:
# NOTE: It is possible that a random ooi will not generate
# any tasks, for instance when all ooi's and their boefjes
# have already run. When this happens 3 times in a row we
# will break out of the loop. We reset the tries counter to
# 0 when we do get new tasks from an ooi.
if tries >= 3:
self.logger.debug(
"No tasks generated for 3 tries, breaking out of loop "
"[organisation.id=%s, scheduler_id=%s]",
self.organisation.id,
self.scheduler_id,
)
return

task = BoefjeTask(
boefje=Boefje.parse_obj(boefje),
input_ooi=ooi.primary_key,
Expand All @@ -317,7 +302,6 @@ def push_tasks_for_random_objects(self) -> None:
self.organisation.id,
self.scheduler_id,
)
tries += 1
continue

try:
Expand Down
53 changes: 0 additions & 53 deletions mula/tests/integration/test_boefje_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,59 +477,6 @@ def test_push_tasks_for_random_objects_item_on_queue(
self.assertEqual(task_db.id.hex, task_pq.id)
self.assertEqual(task_db.status, models.TaskStatus.QUEUED)

@mock.patch("scheduler.schedulers.BoefjeScheduler.is_task_running")
@mock.patch("scheduler.schedulers.BoefjeScheduler.is_task_allowed_to_run")
@mock.patch("scheduler.schedulers.BoefjeScheduler.has_grace_period_passed")
@mock.patch("scheduler.schedulers.BoefjeScheduler.get_boefjes_for_ooi")
@mock.patch("scheduler.context.AppContext.services.octopoes.get_random_objects")
def test_push_tasks_for_random_objects_everything_processed(
self,
mock_get_random_objects,
mock_get_boefjes_for_ooi,
mock_has_grace_period_passed,
mock_is_task_allowed_to_run,
mock_is_task_running,
):
"""Test if the scheduler stops when there are no more objects to
process.
We simulate this by returning the same ooi from the random object
endpoint. This will create the same tasks and should not be added to
the queue. And should fall into the grace period.
"""
# Arrange
scan_profile = ScanProfileFactory(level=0)
ooi = OOIFactory(scan_profile=scan_profile)
boefje = PluginFactory(scan_level=0, consumes=[ooi.object_type])

# Mocks
mock_get_random_objects.return_value = [ooi]
mock_get_boefjes_for_ooi.return_value = [boefje]
mock_is_task_running.return_value = False
mock_has_grace_period_passed.return_value = True

# Simulate for the first iteration the task is allowed to run
# and the subsequent iterations are not.
mock_is_task_allowed_to_run.side_effect = [True, False, False, False]

# Act
with self.assertLogs("scheduler.schedulers", level="DEBUG") as cm:
self.scheduler.push_tasks_for_random_objects()

# Assert we have 3 tries
self.assertIn("No tasks generated for 3 tries", cm.output[-1])

# Task should be on priority queue
task_pq = models.BoefjeTask(**self.scheduler.queue.peek(0).data)
self.assertEqual(1, self.scheduler.queue.qsize())
self.assertEqual(ooi.primary_key, task_pq.input_ooi)
self.assertEqual(boefje.id, task_pq.boefje.id)

# Task should be in datastore, and queueud
task_db = self.mock_ctx.task_store.get_task_by_id(task_pq.id)
self.assertEqual(task_db.id.hex, task_pq.id)
self.assertEqual(task_db.status, models.TaskStatus.QUEUED)

def test_is_allowed_to_run(self):
# Arrange
scan_profile = ScanProfileFactory(level=0)
Expand Down

0 comments on commit 3455447

Please sign in to comment.