Skip to content

Actions that are triggered as jobs from within another action lose their properties #317

@jaulz

Description

@jaulz

It seems that if you want to trigger a job from another action (that runs for example as a controller) the job loses it's properties (like uniqueFor etc.). The reason seems to be that the action instance will still be decorated using the ControllerDecorator (though it was dispatched via dispatch) and then the following call fails because the action instance is actually the decorator and not the action class:

$this->uniqueFor = (int) $this->fromActionWithParameters('getJobUniqueFor', 'jobUniqueFor', 0);

I'm not sure if there is any smart solution for that. My workaround is to have a custom design pattern which does nothing (but it prevents other design pattern from being matched):

<?php

namespace App\Support\Actions\DesignPatterns\Job;

use Lorisleiva\Actions\BacktraceFrame;
use Lorisleiva\Actions\Concerns\AsJob;
use Lorisleiva\Actions\Decorators\JobDecorator;
use Lorisleiva\Actions\DesignPatterns\DesignPattern;

class JobDesignPattern extends DesignPattern
{
  public function getTrait(): string
  {
    return AsJob::class;
  }

  public function recognizeFrame(BacktraceFrame $frame): bool
  {
    return $frame->instanceOf(JobDecorator::class);
  }

  public function decorate($instance, BacktraceFrame $frame)
  {
    return $instance;
  }
}

I'm not sure if it's by design or if this should be considered in the library itself? Should the pattern run at all twice if it was executed once?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions