33
44use Barryvdh \Queue \Models \Job ;
55use Illuminate \Queue \SyncQueue ;
6- use Symfony \ Component \ Process \ Process ;
6+ use Barryvdh \ Queue \ Jobs \ AsyncJob ;
77
88class AsyncQueue extends SyncQueue
99{
@@ -27,12 +27,11 @@ public function __construct(array $config)
2727 *
2828 * @return int
2929 */
30- public function push ($ job , $ data = '' , $ queue = null )
30+ public function push ($ job , $ data= '' , $ queue= null )
3131 {
32- $ id = $ this ->storeJob ($ job , $ data , 0 );
33- $ this ->startProcess ($ id , 0 );
34-
35- return $ id ;
32+ $ id = $ this ->storeJob ($ job , $ data , $ queue );
33+ //$this->startProcess($id, 0);
34+ return 0 ;
3635 }
3736
3837 /**
@@ -42,17 +41,19 @@ public function push($job, $data = '', $queue = null)
4241 *
4342 * @param string $job
4443 * @param mixed $data
44+ * @param string|null $queue
4545 * @param int $delay
4646 *
4747 * @return int
4848 */
49- public function storeJob ($ job , $ data , $ delay = 0 )
49+ private function storeJob ($ job , $ data , $ queue = null , $ delay = 0 )
5050 {
5151 $ payload = $ this ->createPayload ($ job , $ data );
5252
5353 $ job = new Job ();
5454 $ job ->status = Job::STATUS_OPEN ;
5555 $ job ->delay = $ delay ;
56+ if ($ queue ) $ job ->queue = $ queue ;
5657 $ job ->payload = $ payload ;
5758 $ job ->save ();
5859
@@ -63,36 +64,31 @@ public function storeJob($job, $data, $delay = 0)
6364 * Make a Process for the Artisan command for the job id.
6465 *
6566 * @param int $jobId
66- * @param int $delay
6767 *
6868 * @return void
6969 */
70- public function startProcess ($ jobId, $ delay = 0 )
70+ public function startProcess ($ jobId )
7171 {
72- $ command = $ this ->getCommand ($ jobId , $ delay );
73- $ cwd = $ this ->container ['path.base ' ];
74-
75- $ process = new Process ($ command , $ cwd );
76- $ process ->run ();
72+ chdir ($ this ->container ['path.base ' ]);
73+ exec ($ this ->getCommand ($ jobId ));
7774 }
7875
7976 /**
8077 * Get the Artisan command as a string for the job id.
8178 *
8279 * @param int $jobId
83- * @param int $delay
8480 *
8581 * @return string
8682 */
87- protected function getCommand ($ jobId, $ delay = 0 )
83+ protected function getCommand ($ jobId )
8884 {
89- $ cmd = '%s artisan queue:async %d --env=%s --delay=%d ' ;
85+ $ cmd = '%s artisan queue:async %d --env=%s ' ;
9086 $ cmd = $ this ->getBackgroundCommand ($ cmd );
9187
9288 $ binary = $ this ->getPhpBinary ();
9389 $ environment = $ this ->container ->environment ();
9490
95- return sprintf ($ cmd , $ binary , $ jobId , $ environment, $ delay );
91+ return sprintf ($ cmd , $ binary , $ jobId , $ environment );
9692 }
9793
9894 /**
@@ -102,11 +98,7 @@ protected function getCommand($jobId, $delay = 0)
10298 */
10399 protected function getPhpBinary ()
104100 {
105- $ path = $ this ->config ['binary ' ];
106- if (!defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
107- $ path = escapeshellarg ($ path );
108- }
109-
101+ $ path = escapeshellarg ($ this ->config ['binary ' ]);
110102 $ args = $ this ->config ['binary_args ' ];
111103 if (is_array ($ args )){
112104 $ args = implode (' ' , $ args );
@@ -133,13 +125,38 @@ protected function getBackgroundCommand($cmd)
133125 *
134126 * @return int
135127 */
136- public function later ($ delay , $ job , $ data = '' , $ queue = null )
128+ public function later ($ delay , $ job , $ data= '' , $ queue= null )
137129 {
138130 $ delay = $ this ->getSeconds ($ delay );
139- $ id = $ this ->storeJob ($ job , $ data , $ delay );
140- $ this ->startProcess ($ id , $ delay );
141-
142- return $ id ;
131+ $ id = $ this ->storeJob ($ job , $ data , $ queue , $ delay );
132+ //$this->startProcess($id);
133+ return 0 ;
143134 }
144-
135+
136+ /**
137+ * Pop the next job off of the queue.
138+ *
139+ * @param string|null $queue
140+ * @return \Illuminate\Queue\Jobs\Job|null
141+ */
142+ public function pop ($ queue =null ) {
143+ //TODO Il faudrait peut-être tenir compte de la colonne delay (en tt cas si on compte utiliser cette notion pour déterminer quel est le job suivant...)
144+ if ($ queue )
145+ $ item =Job::where ('queue ' , '= ' , $ queue )->orderBy ('id ' , 'asc ' )->first ();
146+ else
147+ $ item =Job::orderBy ('id ' , 'asc ' )->first ();
148+ if ($ item )
149+ return new AsyncJob ($ this ->container , $ item );
150+ return null ;
151+ }
152+
153+ /**
154+ *
155+ * {@inheritDoc}
156+ * @see \Illuminate\Queue\SyncQueue::pushRaw()
157+ */
158+ public function pushRaw ($ payload , $ queue =null , array $ options = array ()) {
159+ $ payload =json_decode ($ payload ,true );
160+ return $ this ->storeJob ($ payload ['job ' ], $ payload ['data ' ], $ queue );
161+ }
145162}
0 commit comments