Skip to content

Can't store cache lock  #2609

Closed
Closed
@VietTQ8

Description

@VietTQ8
  • Laravel-mongodb Version: #.#.#
  • PHP Version: #.#.#
  • Database Driver & Version:

Description:

I have issue when store data to cache lock with MongoDB driver. error also occurs when dispatch a job implements ShouldBeUnique.

Method acquire Illuminate\Cache\DatabaseLock catch QueryException to update record cache lock table. But with mongodb driver connection throw MongoDB\Driver\Exception\BulkWriteException so can not excute snippet code in catch block

Illuminate\Cache\DatabaseLock

public function acquire()
    {
        $acquired = false;

        try {
            $this->connection->table($this->table)->insert([
                'key' => $this->name,
                'owner' => $this->owner,
                'expiration' => $this->expiresAt(),
            ]);

            $acquired = true;
        } catch (QueryException $e) {
            $updated = $this->connection->table($this->table)
                ->where('key', $this->name)
                ->where(function ($query) {
                    return $query->where('owner', $this->owner)->orWhere('expiration', '<=', time());
                })->update([
                    'owner' => $this->owner,
                    'expiration' => $this->expiresAt(),
                ]);

            $acquired = $updated >= 1;
        }

        if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) {
            $this->connection->table($this->table)->where('expiration', '<=', time())->delete();
        }

        return $acquired;
    }

Steps to reproduce

$lock = Cache::lock('foo', 10);
$lock->block(5);

Job:

class ExampleJob implements ShouldQueue, ShouldBeUnique {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UsesBackoffStrategy;

        public RequestQueue $request;
        public $tries = 2;

        /**
         * The number of seconds the job can run before timing out.
         *
         * @var int
         */
        public $timeout = 600;

        /**
         * Indicate if the job should be marked as failed on timeout.
         *
         * @var bool
         */
        public $failOnTimeout = true;

        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct() {
            //
        }

        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle() {
       }

dispatch job:
ExampleJob ::dispatch();

Logs: Exception message like:

local.ERROR: E11000 duplicate key error collection: mes.cache_locks index: key_1 dup key: { key: "_cachefoo" } {"userId":"admin","exception":"[object] (MongoDB\\Driver\\Exception\\BulkWriteException(code: 11000): E11000 duplicate key error collection: mes.cache_locks index: key_1 dup key: { key: \"_cachefoo\" } at /var/www/vendor/mongodb/mongodb/src/Operation/InsertMany.php:157)

Could you suggest any ways solve this issue ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions