-1

For example i have the following class. How i can prevent execution of get_entity task if create_entity task was not executed?

class MyTaskSequence(TaskSequence):

    @seq_task(1)
    def create_entity(self):
        self.round += 1
        with self.client.post('/entities', json={}, catch_response=True) as resp:
            if resp.status_code != HTTPStatus.CREATED:
                resp.failure()
                # how to stop other tasks for that run?

        self.entity_id = resp.json()['data']['entity_id']

    @seq_task(2)
    def get_entity(self):
        # It is being always executed, 
        # but it should not be run if create_entity task failed
        resp = self.client.get(f'/entities/{self.entity_id}')
        ...

I found TaskSet.interrupt method in documentation, but does not allow to cancel root TaskSet. I tried to make parent TaskSet for my task sequence, so TaskSet.interrupt works.

class MyTaskSet(TaskSet):
    tasks = {MyTaskSequence: 10}

But now i see that all results in ui are cleared after i call interrupt!

I just need to skip dependent tasks in this sequence. I need the results.

avasin
  • 9,186
  • 18
  • 80
  • 127

3 Answers3

0

The easiest way to solve this is just to use a single @task with multiple requests inside it. Then, if a request fails just do a return after resp.failure()

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
0

Might self.interrupt() be what you are looking for?

See https://docs.locust.io/en/latest/writing-a-locustfile.html#interrupting-a-taskset for reference.

monhelm
  • 149
  • 6
0

Why not using on_start(self): which runs once whenever a locust created, it can set a global which can be checked whether the locust executes the tasks

class MyTaskSequence(TaskSequence):
    entity_created = false

    def on_start(self):
        self.round += 1
        with self.client.post('/entities', json={}, catch_response=True) as resp:
            if resp.status_code != HTTPStatus.CREATED:
                self.entity_created = true
                resp.failure()

        self.entity_id = resp.json()['data']['entity_id']

    @seq_task(2)
    def get_entity(self):
        if self.entity_created:
            resp = self.client.get(f'/entities/{self.entity_id}')
            ...
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49