@@ -42,6 +42,25 @@ public function size($queue = null)
4242 return 0 ;
4343 }
4444
45+ /**
46+ * Fallback method for Laravel 6x and 7x
47+ *
48+ * @param \Closure|string|object $job
49+ * @param string $payload
50+ * @param string $queue
51+ * @param \DateTimeInterface|\DateInterval|int|null $delay
52+ * @param callable $callback
53+ * @return mixed
54+ */
55+ protected function enqueueUsing ($ job , $ payload , $ queue , $ delay , $ callback )
56+ {
57+ if (method_exists (parent ::class, 'enqueueUsing ' )) {
58+ return parent ::enqueueUsing ($ job , $ payload , $ queue , $ delay , $ callback );
59+ }
60+
61+ return $ callback ($ payload , $ queue , $ delay );
62+ }
63+
4564 /**
4665 * Push a new job onto the queue.
4766 *
@@ -52,9 +71,15 @@ public function size($queue = null)
5271 */
5372 public function push ($ job , $ data = '' , $ queue = null )
5473 {
55- $ this ->pushToCloudTasks ($ queue , $ this ->createPayload (
56- $ job , $ this ->getQueue ($ queue ), $ data
57- ));
74+ return $ this ->enqueueUsing (
75+ $ job ,
76+ $ this ->createPayload ($ job , $ this ->getQueue ($ queue ), $ data ),
77+ $ queue ,
78+ null ,
79+ function ($ payload , $ queue ) {
80+ return $ this ->pushRaw ($ payload , $ queue );
81+ }
82+ );
5883 }
5984
6085 /**
@@ -63,11 +88,11 @@ public function push($job, $data = '', $queue = null)
6388 * @param string $payload
6489 * @param string|null $queue
6590 * @param array $options
66- * @return void
91+ * @return string
6792 */
6893 public function pushRaw ($ payload , $ queue = null , array $ options = [])
6994 {
70- $ this ->pushToCloudTasks ($ queue , $ payload );
95+ return $ this ->pushToCloudTasks ($ queue , $ payload );
7196 }
7297
7398 /**
@@ -81,9 +106,15 @@ public function pushRaw($payload, $queue = null, array $options = [])
81106 */
82107 public function later ($ delay , $ job , $ data = '' , $ queue = null )
83108 {
84- $ this ->pushToCloudTasks ($ queue , $ this ->createPayload (
85- $ job , $ this ->getQueue ($ queue ), $ data
86- ), $ delay );
109+ return $ this ->enqueueUsing (
110+ $ job ,
111+ $ this ->createPayload ($ job , $ this ->getQueue ($ queue ), $ data ),
112+ $ queue ,
113+ $ delay ,
114+ function ($ payload , $ queue , $ delay ) {
115+ return $ this ->pushToCloudTasks ($ queue , $ payload , $ delay );
116+ }
117+ );
87118 }
88119
89120 /**
@@ -92,7 +123,7 @@ public function later($delay, $job, $data = '', $queue = null)
92123 * @param string|null $queue
93124 * @param string $payload
94125 * @param \DateTimeInterface|\DateInterval|int $delay
95- * @return void
126+ * @return string
96127 */
97128 protected function pushToCloudTasks ($ queue , $ payload , $ delay = 0 )
98129 {
@@ -103,12 +134,13 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
103134 $ httpRequest = $ this ->createHttpRequest ();
104135 $ httpRequest ->setUrl ($ this ->getHandler ());
105136 $ httpRequest ->setHttpMethod (HttpMethod::POST );
106- $ httpRequest ->setBody (
107- // Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
108- // Since we are using and expecting the uuid in some places
109- // we will add it manually here if it's not present yet.
110- $ this ->withUuid ($ payload )
111- );
137+
138+ // Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
139+ // Since we are using and expecting the uuid in some places
140+ // we will add it manually here if it's not present yet.
141+ [$ payload , $ uuid ] = $ this ->withUuid ($ payload );
142+
143+ $ httpRequest ->setBody ($ payload );
112144
113145 $ task = $ this ->createTask ();
114146 $ task ->setHttpRequest ($ httpRequest );
@@ -128,9 +160,11 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
128160 $ createdTask = CloudTasksApi::createTask ($ queueName , $ task );
129161
130162 event ((new TaskCreated )->queue ($ queue )->task ($ task ));
163+
164+ return $ uuid ;
131165 }
132166
133- private function withUuid (string $ payload ): string
167+ private function withUuid (string $ payload ): array
134168 {
135169 /** @var array $decoded */
136170 $ decoded = json_decode ($ payload , true );
@@ -139,7 +173,10 @@ private function withUuid(string $payload): string
139173 $ decoded ['uuid ' ] = (string ) Str::uuid ();
140174 }
141175
142- return json_encode ($ decoded );
176+ return [
177+ json_encode ($ decoded ),
178+ $ decoded ['uuid ' ],
179+ ];
143180 }
144181
145182 /**
0 commit comments