@celery.task()
def finalize_launch(launch_id, state=FINISHED, timeout=30, tries=5):
    log.info("Finalize launch {}".format(launch_id))
    launch = Launch.objects.get(pk=launch_id)
    log.info("Current launch: {}".format(launch.__dict__))
    launch.finished = datetime.now()
    launch.calculate_counts()
    launch.state = state
    log.info("Launch for update: {}".format(launch.__dict__))
    launch.save(force_update=True)
    if state not in [STOPPED, FINISHED]:
        for i in range(0, tries):
            log.info("Waiting for {} seconds, before next try".format(timeout))
            sleep(timeout)
            launch = Launch.objects.get(pk=launch_id)
            if launch.state == state:
                break
            log.info("Launch state not finished, try to save again.")
            launch.finished = datetime.now()
            launch.state = state
            launch.save()
    log.info(
        "Updated launch: {}".format(Launch.objects.get(pk=launch_id).__dict__))
dataset/ETHPy150Open 2gis/badger-api/testreport/tasks.py/finalize_launch/original