public function waitFor($task_id, $sleep = 5, $max_wait_time = 0)
    {
        while (1) {
            $details = $this->getTaskDetails($task_id);

            if ($details->status != 'queued' && $details->status != 'running') {
                return $details;
            }
            if ($max_wait_time > 0) {
                $max_wait_time -= $sleep;
                if ($max_wait_time <= 0) {
                    return false;
                }
            }

            sleep($sleep);
        }
        return false;
    }