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;
    }
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;
    }
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 ; }
Executes a task and provisions a BackgroundTaskStatus model to store a persistent record of the state.
@param Task $task The task to execute
@param callable|null $progressReportedCallback An optional call back to receive progress events
@param BackgroundTaskStatus|null $persistentStatus If you have a pre-provisioned BackgroundTaskStatus model it can be updated instead.
Executes a task and provisions a BackgroundTaskStatus model to store a persistent record of the state .