def create_km_triggered_task_instances(community_id, km):
    """
    Checks if there are new task instances that have to be created and creates them.
    :param community_id: Community to check the task instance creation for.
    :param km: Current km clock.
    """
    tasks = TaskModel.find_by_community(community_id)

    # Iterate over all km triggered tasks
    for task in [t for t in tasks if t.km_interval]:

        try:
            # If current km are higher then trigger, add a new task instance
            if km >= task.km_next_instance:
                # Create and persist task instance
                new_task_instance = TaskInstanceModel()
                new_task_instance.task = task
                new_task_instance.km_created_at = km
                new_task_instance.is_open = True
                new_task_instance.community = task.community
                new_task_instance.persist()

                # Update km trigger and persist task
                task.km_next_instance += task.km_interval
                if task.km_next_instance <= km:
                    task.km_next_instance = km + 1
                task.persist()
        except:
            # Don't fail on all just because one instance is bad
            pass
    def get(self, community_id):
        tasks: List[TaskModel] = TaskModel.find_by_community(community_id)
        community: CommunityModel = CommunityModel.find_by_id(community_id)
        community_member_ids = [m.id for m in community.users]
        user = UserModel.find_by_username(get_jwt_identity())

        if user.id not in community_member_ids:
            abort(401, message=UNAUTHORIZED)

        set_km_to_next_instance(tasks)

        return tasks, 200