Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldBeUnique trait is ignored when generating status rows in database #70

Open
oobi opened this issue May 4, 2021 · 0 comments
Open

Comments

@oobi
Copy link

oobi commented May 4, 2021

When queueing/monitoring a job with the ShouldBeUnique trait, the dispatch should not result in duplicate status rows in the DB.

I made a quick and dirty modification to Trackable.php based on a comment in the Lumen repo which sorts it out, but there's probably a cleaner way through:

protected function prepareStatus(array $data = [])
    {
         // > New condition
         if ($this instanceof ShouldBeUnique) {

            $uniqueId = method_exists($this, 'uniqueId')
                    ? $this->uniqueId()
                    : ($this->uniqueId ?? '');

            $cache = method_exists($this, 'uniqueVia')
                        ? $this->uniqueVia()
                        : Container::getInstance()->make(Cache::class);

            $uniqueFor = $this->uniqueFor ?? 0;

            $lock = $cache->lock( 'laravel_unique_job:' . get_class($this) . $uniqueId, $uniqueFor);

            // this job is already queued and should be unique. Do not track.
            if (true !== $lock->get()) {
                $this->shouldTrack = false;
            } else {
                // release the lock if we locked it, otherwise it won't queue
                $lock->release();
            }
        }
        // <

        if (!$this->shouldTrack) {
            return;
        }

        /** @var JobStatus */
        $entityClass = app(config('job-status.model'));

        $data = array_merge(['type' => $this->getDisplayName()], $data);
        /** @var JobStatus */
        $status = $entityClass::query()->create($data);

        $this->statusId = $status->getKey();

        return;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant