2020
2121use function Safe \json_decode ;
2222use function Safe \json_encode ;
23+ use function Safe \preg_replace ;
2324
2425class CloudTasksQueue extends LaravelQueue implements QueueContract
2526{
26- /**
27- * @var CloudTasksClient
28- */
29- private $ client ;
30-
31- public array $ config ;
32-
33- public function __construct (array $ config , CloudTasksClient $ client , $ dispatchAfterCommit = false )
27+ public function __construct (public array $ config , public CloudTasksClient $ client , public $ dispatchAfterCommit = false )
3428 {
35- $ this ->client = $ client ;
36- $ this ->config = $ config ;
37- $ this ->dispatchAfterCommit = $ dispatchAfterCommit ;
29+ //
3830 }
3931
4032 /**
4133 * Get the size of the queue.
4234 *
4335 * @param string|null $queue
44- * @return int
4536 */
46- public function size ($ queue = null )
37+ public function size ($ queue = null ): int
4738 {
4839 // It is not possible to know the number of tasks in the queue.
4940 return 0 ;
@@ -61,7 +52,7 @@ public function push($job, $data = '', $queue = null)
6152 {
6253 return $ this ->enqueueUsing (
6354 $ job ,
64- $ this ->createPayload ($ job , $ this -> getQueue ( $ queue) , $ data ),
55+ $ this ->createPayload ($ job , $ queue , $ data ),
6556 $ queue ,
6657 null ,
6758 function ($ payload , $ queue ) {
@@ -97,7 +88,7 @@ public function later($delay, $job, $data = '', $queue = null)
9788 {
9889 return $ this ->enqueueUsing (
9990 $ job ,
100- $ this ->createPayload ($ job , $ this -> getQueue ( $ queue) , $ data ),
91+ $ this ->createPayload ($ job , $ queue , $ data ),
10192 $ queue ,
10293 $ delay ,
10394 function ($ payload , $ queue , $ delay ) {
@@ -116,7 +107,7 @@ function ($payload, $queue, $delay) {
116107 */
117108 protected function pushToCloudTasks ($ queue , $ payload , $ delay = 0 )
118109 {
119- $ queue = $ this ->getQueue ( $ queue) ;
110+ $ queue = $ queue ?: $ this ->config [ ' queue ' ] ;
120111 $ queueName = $ this ->client ->queueName ($ this ->config ['project ' ], $ this ->config ['location ' ], $ queue );
121112 $ availableAt = $ this ->availableAt ($ delay );
122113
@@ -127,9 +118,13 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
127118 // value and need to manually set and update the number of times a task has been attempted.
128119 $ payload = $ this ->withAttempts ($ payload );
129120
130- $ task = $ this ->createTask ();
121+ $ payload = $ this ->withQueueName ($ payload , $ queue );
122+
123+ $ task = new Task ();
131124 $ task ->setName ($ this ->taskName ($ queue , $ payload ));
132125
126+ $ payload = $ this ->withTaskName ($ payload , $ task ->getName ());
127+
133128 if (! empty ($ this ->config ['app_engine ' ])) {
134129 $ path = \Safe \parse_url (route ('cloud-tasks.handle-task ' ), PHP_URL_PATH );
135130
@@ -144,7 +139,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
144139 }
145140 $ task ->setAppEngineHttpRequest ($ appEngineRequest );
146141 } else {
147- $ httpRequest = $ this -> createHttpRequest ();
142+ $ httpRequest = new HttpRequest ();
148143 $ httpRequest ->setUrl ($ this ->getHandler ());
149144 $ httpRequest ->setHttpMethod (HttpMethod::POST );
150145
@@ -189,7 +184,7 @@ private function taskName(string $queueName, array $payload): string
189184 );
190185 }
191186
192- private function sanitizeTaskName (string $ taskName )
187+ private function sanitizeTaskName (string $ taskName ): string
193188 {
194189 // Remove all characters that are not -, letters, numbers, or whitespace
195190 $ sanitizedName = preg_replace ('![^-\pL\pN\s]+!u ' , '- ' , $ taskName );
@@ -209,6 +204,20 @@ private function withAttempts(array $payload): array
209204 return $ payload ;
210205 }
211206
207+ private function withQueueName (array $ payload , string $ queueName ): array
208+ {
209+ $ payload ['internal ' ]['queue ' ] = $ queueName ;
210+
211+ return $ payload ;
212+ }
213+
214+ private function withTaskName (array $ payload , string $ taskName ): array
215+ {
216+ $ payload ['internal ' ]['taskName ' ] = $ taskName ;
217+
218+ return $ payload ;
219+ }
220+
212221 /**
213222 * Pop the next job off of the queue.
214223 *
@@ -217,35 +226,12 @@ private function withAttempts(array $payload): array
217226 */
218227 public function pop ($ queue = null )
219228 {
220- // TODO: Implement pop() method.
221- }
222-
223- private function getQueue (?string $ queue = null ): string
224- {
225- return $ queue ?: $ this ->config ['queue ' ];
226- }
227-
228- private function createHttpRequest (): HttpRequest
229- {
230- return app (HttpRequest::class);
229+ //
231230 }
232231
233232 public function delete (CloudTasksJob $ job ): void
234233 {
235- $ config = $ this ->config ;
236-
237- $ queue = $ job ->getQueue () ?: $ this ->config ['queue ' ]; // @todo: make this a helper method somewhere.
238-
239- $ headerTaskName = request ()->headers ->get ('X-Cloudtasks-Taskname ' )
240- ?? request ()->headers ->get ('X-AppEngine-TaskName ' );
241- $ taskName = $ this ->client ->taskName (
242- $ config ['project ' ],
243- $ config ['location ' ],
244- $ queue ,
245- (string ) $ headerTaskName
246- );
247-
248- CloudTasksApi::deleteTask ($ taskName );
234+ CloudTasksApi::deleteTask ($ job ->getTaskName ());
249235 }
250236
251237 public function release (CloudTasksJob $ job , int $ delay = 0 ): void
@@ -259,11 +245,6 @@ public function release(CloudTasksJob $job, int $delay = 0): void
259245 $ this ->pushRaw ($ payload , $ job ->getQueue (), $ options );
260246 }
261247
262- private function createTask (): Task
263- {
264- return app (Task::class);
265- }
266-
267248 public function getHandler (): string
268249 {
269250 return Config::getHandler ($ this ->config ['handler ' ]);
0 commit comments