2017-07-27 91 views
1

在我的應用程序中,用戶可以安排一條消息(如提醒),時間爲何時,應用程序上會顯示一個彈出窗口。爲此,我創建了一個通知,並通過WebSocket發送。Laravel - 如何刪除作業(通知)

現在,假設使用了插入錯誤的日期(或者簡單地說,想更改提醒日期),我需要在執行此操作之前訪問JOB。

我也嘗試了很多的解決方案,但沒有作品...

我的通知類是:

class RemindNote extends Notification implements ShouldQueue, ShouldBroadcast 
{ 
    use Queueable; 
    use InteractsWithQueue; 
} 

試圖處理方法上RemindNote:

public function handle($event) { 
    $this->delete(); 
} 

php artisan queue:work激活,當我安排通知時,句柄方法從不被調用。

試圖Queue::beforeAppServiceProvider

這種方法被稱爲但是如果我叫$event->job->delete(),該通知是始終點火 - 作業刪除。但是,例如,如果我打電話$event->job->release(),這項工作是重新計劃的

所以,有一種方法來訪問作業有效載荷,並刪除它之前執行?

編輯

\Queue::before(function(JobProcessing $event) { 
    \Log::debug($event->job->isDeleted()); 
    $event->job->delete(); 
}); 

這樣,當處理作業我laravel.log看到這一點:

[2017-07-27 16:44:35] stage.DEBUG: 
[2017-07-27 16:44:36] stage.DEBUG: 

而且,通知始終點火。看起來hook之前的delete()沒有效果。

編輯 這是登錄的隊列中的載荷::之前(方法$事件 - >求職>的有效載荷()):

[2017-07-27 15:00:13] stage.INFO: array (
    'job' => 'Illuminate\\Queue\\[email protected]', 
    'data' => 
    array (
    'commandName' => 'Illuminate\\Notifications\\SendQueuedNotifications', 
    'command' => 'O:48:"Illuminate\\Notifications\\SendQueuedNotifications":6:{s:14:"' . "\0" . '*' . "\0" . 'notifiables";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:26:"App\\Repositories\\User\\User";s:2:"id";a:1:{i:0;i:29;}}s:15:"' . "\0" . '*' . "\0" . 'notification";O:28:"App\\Notifications\\RemindNote":7:{s:4:"body";s:3:"ddf";s:4:"user";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:26:"App\\Repositories\\User\\User";s:2:"id";i:29;}s:2:"id";s:36:"e160802c-e9e5-4d2c-a1e1-d63cbdceb54c";s:10:"connection";N;s:5:"queue";N;s:5:"delay";O:13:"Carbon\\Carbon":3:{s:4:"date";s:26:"2017-07-27 14:31:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:11:"Europe/Rome";}s:6:"' . "\0" . '*' . "\0" . 'job";N;}s:11:"' . "\0" . '*' . "\0" . 'channels";a:1:{i:0;s:9:"broadcast";}s:10:"connection";N;s:5:"queue";N;s:5:"delay";r:14;}', 
), 
) 
[2017-07-27 15:00:13] stage.INFO: array (
    'job' => 'Illuminate\\Broadcasting\\BroadcastEvent', 
    'data' => 
    array (
    'event' => 'O:60:"Illuminate\\Notifications\\Events\\BroadcastNotificationCreated":6:{s:10:"notifiable";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:26:"App\\Repositories\\User\\User";s:2:"id";i:29;}s:12:"notification";O:28:"App\\Notifications\\RemindNote":7:{s:4:"body";s:3:"ddf";s:4:"user";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:26:"App\\Repositories\\User\\User";s:2:"id";i:29;}s:2:"id";s:36:"e160802c-e9e5-4d2c-a1e1-d63cbdceb54c";s:10:"connection";N;s:5:"queue";N;s:5:"delay";O:13:"Carbon\\Carbon":3:{s:4:"date";s:26:"2017-07-27 14:31:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:11:"Europe/Rome";}s:6:"' . "\0" . '*' . "\0" . 'job";N;}s:4:"data";a:1:{s:4:"body";s:3:"ddf";}s:10:"connection";N;s:5:"queue";N;s:5:"delay";N;}', 
), 
) 
+0

您必須將其從隊列中移除。你如何做*取決於你正在使用哪個隊列引擎。 – ceejayoz

+0

我正在使用數據庫隊列 – Mistre83

+0

然後,您可以在數據庫中找到記錄並將其刪除。 – ceejayoz

回答

0

您可以運行在Queue::before事件的回調。

將提醒標記爲已在數據庫中刪除,然後在您的Queue::before回調中檢查作業是否已刪除(如果已刪除),請在處理作業之前從作業中刪除作業。

查看的文檔工作事件:https://laravel.com/docs/5.4/queues#job-events

[編輯] 剛剛看到您的編輯。如果您使用job->delete(),則不應處理該作業是否有可能這是一個延遲作業的錯誤?

此外,當您對類進行更改時,是否停止並重新啓動隊列偵聽器?我陷入了這個陷阱。

+0

我試圖把$ job-> delete()在隊列::之前,但是作業總是被執行。現在,我用isDeleted()放了一個日誌,然後我調用delete(),日誌顯示一個空行。看到我的編輯 – Mistre83

+0

當然,我已經多次重新啓動工人。 – Mistre83

+0

@ Mistre83你有沒有嘗試將整個事件轉儲到日誌中,看看有什麼? – Joe