public static function executeInBackground(Task $task, $progressReportedCallback = null, BackgroundTaskStatus $persistentStatus = null)
    {
        if (!$persistentStatus){
            $persistentStatus = new BackgroundTaskStatus();
            $persistentStatus->TaskClass = get_class($task);
            $persistentStatus->save();
        }

        $lastStatus = new TaskStatus();

        $result = false;

        try {
            $result = $task->execute(function(TaskStatus $status) use ($progressReportedCallback, &$lastStatus, &$persistentStatus){

                $lastStatus = $status;

                $persistentStatus->PercentageComplete = $status->percentageComplete;
                $persistentStatus->TaskStatus = $status->status;
                $persistentStatus->Message = $status->message;

                if ($progressReportedCallback){
                    $progressReportedCallback($status);
                }

                $persistentStatus->save();
            });

            $persistentStatus->TaskStatus = "Complete";
            $persistentStatus->save();

            if ($progressReportedCallback){
                // Output a final status of complete.
                $lastStatus->percentageComplete = 100;
                $lastStatus->status = BackgroundTaskStatus::TASK_STATUS_COMPLETE;
                $progressReportedCallback($lastStatus);
            }

        } catch (Exception $er) {
            $persistentStatus->TaskStatus = "Failed";
            $persistentStatus->ExceptionDetails = $er->getMessage() . "\r\n\r\n" . $er->getTraceAsString();
            $persistentStatus->save();

            if ($progressReportedCallback){
                // Output a final status of complete.
                $lastStatus->status = BackgroundTaskStatus::TASK_STATUS_FAILED;

                if ($er instanceof RhubarbException) {
                    $lastStatus->message = $er->getPublicMessage();
                }

                $progressReportedCallback($lastStatus);
            }
        }

        return $result;
    }