Problem with asynchronous Laravel Audit Logging When Using Redis Queue Connection

I installed Laravel audit with the following config:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
use AppServicesAuditingResolversUserResolver;
use ModulesUserEntitiesAudit;
return [
'enabled' => env('AUDITING_ENABLED', true),
/*
|--------------------------------------------------------------------------
| Audit Implementation
|--------------------------------------------------------------------------
|
| Define which Audit model implementation should be used.
|
*/
'implementation' => Audit::class,
/*
|--------------------------------------------------------------------------
| User Morph prefix & Guards
|--------------------------------------------------------------------------
|
| Define the morph prefix and authentication guards for the User resolver.
|
*/
'user' => [
'morph_prefix' => 'operator',
'guards' => [
'web',
'api'
],
'resolver' => UserResolver::class
],
/*
|--------------------------------------------------------------------------
| Audit Resolvers
|--------------------------------------------------------------------------
|
| Define the IP Address, User Agent and URL resolver implementations.
|
*/
'resolvers' => [
'ip_address' => OwenItAuditingResolversIpAddressResolver::class,
'user_agent' => OwenItAuditingResolversUserAgentResolver::class,
'url' => OwenItAuditingResolversUrlResolver::class
],
/*
|--------------------------------------------------------------------------
| Audit Events
|--------------------------------------------------------------------------
|
| The Eloquent events that trigger an Audit.
|
*/
'events' => [
'created',
'updated',
'deleted',
'restored'
],
'strict' => false,
'exclude' => [],
'empty_values' => true,
'allowed_empty_values' => [
'retrieved'
],
'allowed_array_values' => false,
'timestamps' => true,
'threshold' => 0,
'driver' => 'database',
'drivers' => [
'database' => [
'table' => 'audits',
'connection' => null,
],
],
'queue' => [
'enable' => true,
'connection' => 'redis',
'queue' => 'audit',
'delay' => 0,
],
'console' => false,
];
</code>
<code><?php use AppServicesAuditingResolversUserResolver; use ModulesUserEntitiesAudit; return [ 'enabled' => env('AUDITING_ENABLED', true), /* |-------------------------------------------------------------------------- | Audit Implementation |-------------------------------------------------------------------------- | | Define which Audit model implementation should be used. | */ 'implementation' => Audit::class, /* |-------------------------------------------------------------------------- | User Morph prefix & Guards |-------------------------------------------------------------------------- | | Define the morph prefix and authentication guards for the User resolver. | */ 'user' => [ 'morph_prefix' => 'operator', 'guards' => [ 'web', 'api' ], 'resolver' => UserResolver::class ], /* |-------------------------------------------------------------------------- | Audit Resolvers |-------------------------------------------------------------------------- | | Define the IP Address, User Agent and URL resolver implementations. | */ 'resolvers' => [ 'ip_address' => OwenItAuditingResolversIpAddressResolver::class, 'user_agent' => OwenItAuditingResolversUserAgentResolver::class, 'url' => OwenItAuditingResolversUrlResolver::class ], /* |-------------------------------------------------------------------------- | Audit Events |-------------------------------------------------------------------------- | | The Eloquent events that trigger an Audit. | */ 'events' => [ 'created', 'updated', 'deleted', 'restored' ], 'strict' => false, 'exclude' => [], 'empty_values' => true, 'allowed_empty_values' => [ 'retrieved' ], 'allowed_array_values' => false, 'timestamps' => true, 'threshold' => 0, 'driver' => 'database', 'drivers' => [ 'database' => [ 'table' => 'audits', 'connection' => null, ], ], 'queue' => [ 'enable' => true, 'connection' => 'redis', 'queue' => 'audit', 'delay' => 0, ], 'console' => false, ]; </code>
<?php

use AppServicesAuditingResolversUserResolver;
use ModulesUserEntitiesAudit;

return [

    'enabled' => env('AUDITING_ENABLED', true),

    /*
    |--------------------------------------------------------------------------
    | Audit Implementation
    |--------------------------------------------------------------------------
    |
    | Define which Audit model implementation should be used.
    |
    */

    'implementation' => Audit::class,

    /*
    |--------------------------------------------------------------------------
    | User Morph prefix & Guards
    |--------------------------------------------------------------------------
    |
    | Define the morph prefix and authentication guards for the User resolver.
    |
    */

    'user' => [
        'morph_prefix' => 'operator',
        'guards' => [
            'web',
            'api'
        ],
        'resolver' => UserResolver::class
    ],

    /*
    |--------------------------------------------------------------------------
    | Audit Resolvers
    |--------------------------------------------------------------------------
    |
    | Define the IP Address, User Agent and URL resolver implementations.
    |
    */
    'resolvers' => [
        'ip_address' => OwenItAuditingResolversIpAddressResolver::class,
        'user_agent' => OwenItAuditingResolversUserAgentResolver::class,
        'url' => OwenItAuditingResolversUrlResolver::class
    ],

    /*
    |--------------------------------------------------------------------------
    | Audit Events
    |--------------------------------------------------------------------------
    |
    | The Eloquent events that trigger an Audit.
    |
    */

    'events' => [
        'created',
        'updated',
        'deleted',
        'restored'
    ],

    'strict' => false,


    'exclude' => [],

    'empty_values' => true,

    'allowed_empty_values' => [
        'retrieved'
    ],

    'allowed_array_values' => false,

    'timestamps' => true,

    'threshold' => 0,

    'driver' => 'database',

    'drivers' => [
        'database' => [
            'table' => 'audits',
            'connection' => null,
        ],
    ],

    'queue' => [
        'enable' => true,
        'connection' => 'redis',
        'queue' => 'audit',
        'delay' => 0,
    ],

    'console' => false,
];

Two models, Booking and Task, are equipped with the Auditable trait for logging purposes via Laravel Audit package.

When a Booking is created, an asynchronous listener triggered by an event is responsible for creating a Task record in the database. The issue arises when the Laravel Queue Connection is set to redis. Despite successfully inserting records for both Booking and Task into the database, only the log for Booking gets recorded in the audits table. Strangely, the log for Task fails to be recorded. The horizon doesn’t even show that

OwenItAuditingListenersProcessDispatchAudit

is being fired for recording the log of the Task. Thus, I’m sure that the event for inserting the log of the Task is not fired in this case.

Interestingly, when the Laravel Queue Connection is switched to sync, everything functions correctly. Both the creation of Booking and Task instances are effectively logged in the audits table.

How can I solve the issue?

I don’t know why, but

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>App::runningInConsole()
</code>
<code>App::runningInConsole() </code>
App::runningInConsole()

returns true and the following function prevents it from running

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static function isAuditingEnabled(): bool
{
if (App::runningInConsole()) {
return Config::get('audit.enabled', true) && Config::get('audit.console', false);
}
return Config::get('audit.enabled', true);
}
</code>
<code>public static function isAuditingEnabled(): bool { if (App::runningInConsole()) { return Config::get('audit.enabled', true) && Config::get('audit.console', false); } return Config::get('audit.enabled', true); } </code>
public static function isAuditingEnabled(): bool
{
    if (App::runningInConsole()) {
        return Config::get('audit.enabled', true) && Config::get('audit.console', false);
    }

    return Config::get('audit.enabled', true);
}

So I set

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>'console' => true,
</code>
<code>'console' => true, </code>
'console' => true,

and it worked. I would appreciate it if anybody tells me why App::runningInConsole() is True when an asynchrony listener is running.

The URL is log is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>artisan horizon:work redis --name=default --supervisor=5803372c5219-vUam:task --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=task --sleep=3 --timeout=60 --tries=3 --rest=0
</code>
<code>artisan horizon:work redis --name=default --supervisor=5803372c5219-vUam:task --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=task --sleep=3 --timeout=60 --tries=3 --rest=0 </code>
artisan horizon:work redis --name=default --supervisor=5803372c5219-vUam:task --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=task --sleep=3 --timeout=60 --tries=3 --rest=0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật