|
9 | 9 | use Illuminate\Support\Facades\Process;
|
10 | 10 | use Illuminate\Support\Str;
|
11 | 11 | use InvalidArgumentException;
|
12 |
| -use Laravel\SerializableClosure\SerializableClosure; |
13 | 12 | use LogicException;
|
14 | 13 | use loophp\phposinfo\OsInfo;
|
15 | 14 | use RuntimeException;
|
16 | 15 |
|
| 16 | +use function Opis\Closure\{serialize, unserialize}; |
| 17 | + |
17 | 18 | /**
|
18 | 19 | * The common handler of an AsyncTask; this can be a closure (will be wrapped inside AsyncTask) or an interface instance.
|
19 | 20 | */
|
20 | 21 | class AsyncTask
|
21 | 22 | {
|
22 | 23 | /**
|
23 | 24 | * The task to be executed in the background.
|
24 |
| - * @var SerializableClosure|AsyncTaskInterface |
| 25 | + * @var Closure|AsyncTaskInterface |
25 | 26 | */
|
26 |
| - private SerializableClosure|AsyncTaskInterface $theTask; |
| 27 | + private Closure|AsyncTaskInterface $theTask; |
27 | 28 |
|
28 | 29 | /**
|
29 | 30 | * The user-specified ID of the current task. (Null means user did not specify any ID).
|
@@ -105,17 +106,31 @@ class AsyncTask
|
105 | 106 | */
|
106 | 107 | public function __construct(Closure|AsyncTaskInterface $theTask, string|null $taskID = null)
|
107 | 108 | {
|
108 |
| - if ($theTask instanceof Closure) { |
109 |
| - // convert to serializable closure first |
110 |
| - $theTask = new SerializableClosure($theTask); |
111 |
| - } |
| 109 | + // opis/closure allows direct storage of closure |
112 | 110 | $this->theTask = $theTask;
|
113 | 111 | if ($taskID === "") {
|
114 | 112 | throw new InvalidArgumentException("AsyncTask ID cannot be empty.");
|
115 | 113 | }
|
116 | 114 | $this->taskID = $taskID;
|
117 | 115 | }
|
118 | 116 |
|
| 117 | + public function __serialize(): array |
| 118 | + { |
| 119 | + // serialize only the necessary info to reduce runner cmd length |
| 120 | + return [ |
| 121 | + 'theTask' => $this->theTask, |
| 122 | + 'timeLimit' => $this->timeLimit, |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + public function __unserialize($data): void |
| 127 | + { |
| 128 | + [ |
| 129 | + 'theTask' => $this->theTask, |
| 130 | + 'timeLimit' => $this->timeLimit, |
| 131 | + ] = $data; |
| 132 | + } |
| 133 | + |
119 | 134 | /**
|
120 | 135 | * Inside an available PHP process, runs this AsyncTask instance.
|
121 | 136 | *
|
@@ -159,10 +174,8 @@ public function run(): void
|
159 | 174 | }
|
160 | 175 |
|
161 | 176 | // then, execute the task itself
|
162 |
| - if ($this->theTask instanceof SerializableClosure) { |
163 |
| - $innerClosure = $this->theTask->getClosure(); |
164 |
| - $innerClosure(); |
165 |
| - unset($innerClosure); |
| 177 | + if ($this->theTask instanceof Closure) { |
| 178 | + ($this->theTask)(); |
166 | 179 | } else {
|
167 | 180 | // must be AsyncTaskInterface
|
168 | 181 | $this->theTask->execute();
|
|
0 commit comments