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

Using external function inside Task #142

Closed
MeerKatDev opened this issue Apr 19, 2021 · 3 comments
Closed

Using external function inside Task #142

MeerKatDev opened this issue Apr 19, 2021 · 3 comments

Comments

@MeerKatDev
Copy link

I can't run a function from Inside the Task:

trace: [{file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Process/ParallelProcess.php",…},…]
0: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Process/ParallelProcess.php",…}
1: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Process/ProcessCallbacks.php",…}
2: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Process/ProcessCallbacks.php",…}
3: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Pool.php", line: 209,…}
4: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Pool.php", line: 323,…}
5: {file: "/Users/pro/Work/lookinglass-backend/vendor/spatie/async/src/Pool.php", line: 154,…}
6: {file: "/Users/pro/Work/lookinglass-backend/app/Traits/Scraper.php", line: 91, function: "wait",…}

I have this simple task:

  class CacheOperation extends Task {
      public function __construct($id, $fun)
      {
          $this->id = $id;
          $this->fun = $fun;
      }

      public function configure(): void { }

      public function run(): void
      {
        $this->fun($this->id);
      }
  }

and I'm trying to use it like that:

$pool->add(new CacheOperation($id, function($id){ return $ref->searchOrGetCustomer($id); }));

where $ref is a reference to the calling class. I wanted to pass $ref inside the task through the constructor, but it was saying that the class is not serializable, so I tried to pass only the function inside, but it still won't run. How is Task supposed to be used?

@MeerKatDev MeerKatDev changed the title Task usage Using external function inside Task Apr 19, 2021
@MeerKatDev MeerKatDev changed the title Using external function inside Task Using external function inside Task Apr 19, 2021
@skrtdev
Copy link

skrtdev commented Apr 21, 2021

You shouldn’t have a closure inside your Task instance, this is the sense of a Task. You have to encapsulate the logic inside the Task

@MeerKatDev
Copy link
Author

As I wrote in the issue, that was my first try, using it normally, but it was throwing back that the class is not serializable.

@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

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

3 participants