من از Redis و Supervisor برای Queue ها استفاده میکنم، سوپر وایزر در پردازش های سیستمی دیده میشه و درحال اجراست، ردیس هم کلید هارو به خوبی میسازه و مشکلی نداره
کلید ها در ردیس redis-cli keys *
:
1) "queues:medium"
2) "MyWebsite_cache:u-1-downloaded-file-at"
....
کانفیگ سوپر وایزر:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel/artisan queue:work
autostart=true
autorestart=true
user=root
numprocs=1
redirect_:stderr=true
stdout_logfile=/var/www/laravel/worker.log
جاب من به صورت زیر هست:
<?php
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateSupportFacadesCache;
use IlluminateSupportFacadesAuth;
use AppHttpControllersWEBFiles;
use IlluminateHttpUploadedFile;
use CarbonCarbon;
class DownloadFiles implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 300;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 3;
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
ini_set('memory_limit', '-1');
$data = $this->data;
// Download File
self::DownloadFile($data);
// Upload File
$uploadedFile = new UploadedFile($data['full_path'], "{$data['name']}.{$data['extension']}", $data['mime-type'], null, true);
Files::saveFile($uploadedFile);
}
// Start Download
static public function DownloadFile($data)
{
set_time_limit(0); // to infinity for example
// This is the file where we save the information
$file_open = fopen($data['full_path'], 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $data['user-agent']); // User-Agent
curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // Maximum of redirection
curl_setopt($ch, CURLOPT_TIMEOUT, 400); // Timeout in Seconds
curl_setopt($ch, CURLOPT_FILE, $file_open); // write curl response to file
curl_setopt($ch, CURLOPT_URL, $data['url']);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($file_open);
}
}
و اونو به این صورت "میفرستمش" یا دیسپچ میکنمش:
.....
// Start downloading
DownloadFiles::dispatch($data)->onQueue('medium');
return response()->json(['message' => 'Download Started.'], 200);
نمیدونم به چه علت کار نمیکنه با درایور ردیس، با درایور سینک کار میکنه به خوبی ولی خب زمان زیادی برای اجرا میگیره تا تموم بشه.
فایل ها و حق دسترسی ها:
drwxrwxrwx 12 www-data www-data 4096 Oct 11 02:34 app
-rwxrwxrwx 1 www-data www-data 1686 Oct 2 18:09 artisan
drwxrwxrwx 3 www-data www-data 4096 Oct 2 17:40 bootstrap
-rwxrwxrwx 1 www-data www-data 1814 Oct 19 11:24 composer.json
-rwxrwxrwx 1 www-data www-data 189481 Oct 20 14:39 composer.lock
drwxrwxrwx 2 www-data www-data 4096 Oct 19 09:56 config
drwxrwxrwx 5 www-data www-data 4096 Oct 2 17:40 database
-rwxrwxrwx 1 www-data www-data 1004 Oct 2 17:39 package.json
-rwxrwxrwx 1 www-data www-data 1134 Oct 2 17:39 phpunit.xml
drwxrwxrwx 8 www-data www-data 4096 Oct 12 06:26 public
drwxrwxrwx 5 www-data www-data 4096 Oct 11 05:17 resources
drwxrwxrwx 2 www-data www-data 4096 Oct 2 17:39 routes
-rwxrwxrwx 1 www-data www-data 563 Oct 2 17:39 server.php
drwxrwxrwx 5 www-data www-data 4096 Oct 2 18:50 storage
drwxrwxrwx 4 www-data www-data 4096 Oct 2 17:52 tests
drwxrwxrwx 49 www-data www-data 4096 Oct 20 14:39 vendor
-rwxrwxrwx 1 www-data www-data 1036 Oct 2 17:39 webpack.mix.js
-rwxrwxrwx 1 www-data www-data 0 Oct 21 08:13 worker.log
-rwxrwxrwx 1 www-data www-data 0 Oct 2 17:39 yarn-error.log
لاگ فایل worker.log هم خالی!
حل شد، رفتم onQueue() رو حذف کردم، تازه جاب اجرا شد، بعد اونم یادم رفته بود از Auth استفاده نکنم تو Job هام، اونم حذف کردم، بعد دست پنجه نرم کردن با Permission ها آخر حل شد.
سلام این بخش از کدتون رو متوجه نمیشم به چه خاطریه ؟
DownloadFiles::dispatch($data)
منظور اینکه از کجا میاد اگر داخل یک کنترلر هست لازم به این همه سختی نیست با چنین کدی راحت میتونید چیزی که مد نظر دارید dispatch کنید مثل زیر مثلا
$this->dispatch(new SendWelcomeEmail());
برای مثلا ما job ای با عنوان SendWelcomeEmail رو در بالا dispatch کردیم
اون DownloadFiles یک جاب هستش، اونم کلاسش که متد استاتیک دیسپچ صدا میکنم بره تو صف، همین! اونجایی هم که دیسپچ میکنم کنترلرم هست که یک لینک میگیره و صف اجرا میکنه از کلاس جاب که تو پوشه Jobs هست.
متد مثلا DownloadFile:
// Start downloading
DownloadFiles::dispatch($data)->onQueue('medium');
return response()->json(['message' => 'Download Started.'], 200);
php artisan make:job DownloadFiles
اون DownloadFiles هم در مسر app/Jobs هست
var/log/supervisor:
-rw------- 1 root root 0 Oct 21 08:29 laravel-worker_00-stderr---supervisor-eRvrf6.log
-rw-r--r-- 1 root root 1425647 Oct 21 08:29 supervisord.log
مورد اول خالی، مورد دوم هم:
2018-10-11 02:35:44,533 CRIT Supervisor running as root (no user in config file)
2018-10-11 02:35:44,533 WARN No file matches via include "/etc/supervisor/conf.d/*.conf"
2018-10-11 02:35:44,542 INFO RPC interface 'supervisor' initialized
2018-10-11 02:35:44,543 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2018-10-11 02:35:44,543 INFO supervisord started with pid 1023
2018-10-11 02:48:48,887 INFO spawned: 'laravel-worker_00' with pid 1207
2018-10-11 02:48:48,888 INFO spawned: 'laravel-worker_01' with pid 1208
2018-10-11 02:48:48,892 INFO spawned: 'laravel-worker_02' with pid 1209
2018-10-11 02:48:48,893 INFO spawned: 'laravel-worker_03' with pid 1210
2018-10-11 02:48:48,896 INFO spawned: 'laravel-worker_04' with pid 1211
2018-10-11 02:48:48,934 INFO spawned: 'laravel-worker_05' with pid 1212
2018-10-11 02:48:48,935 INFO spawned: 'laravel-worker_06' with pid 1213
2018-10-11 02:48:48,937 INFO spawned: 'laravel-worker_07' with pid 1214
2018-10-11 02:48:49,944 INFO success: laravel-worker_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_03 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_04 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_05 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_06 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-11 02:48:49,944 INFO success: laravel-worker_07 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-12 03:49:36,273 INFO waiting for laravel-worker_00, laravel-worker_01, laravel-worker_02, laravel-worker_03, laravel-worker_04, laravel-worker_05, laravel-worker_06, laravel-worker_07 to die
2018-10-12 03:49:36,284 INFO stopped: laravel-worker_00 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_01 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_02 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_03 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_04 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_05 (terminated by SIGKILL)
2018-10-12 03:49:36,285 INFO stopped: laravel-worker_07 (terminated by SIGKILL)
2018-10-12 03:49:36,286 INFO stopped: laravel-worker_06 (terminated by SIGKILL)
2018-10-12 03:50:52,067 CRIT Supervisor running as root (no user in config file)
2018-10-12 03:50:52,076 INFO Included extra file "/etc/supervisor/conf.d/laravel-worker.conf" during parsing
2018-10-12 03:50:52,161 INFO RPC interface 'supervisor' initialized
2018-10-12 03:50:52,162 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2018-10-12 03:50:52,162 INFO supervisord started with pid 597
2018-10-12 03:50:53,180 INFO spawned: 'laravel-worker_00' with pid 774
2018-10-12 03:50:53,183 INFO spawned: 'laravel-worker_01' with pid 775
2018-10-12 03:50:53,185 INFO spawned: 'laravel-worker_02' with pid 776
2018-10-12 03:50:53,187 INFO spawned: 'laravel-worker_03' with pid 777
2018-10-12 03:50:53,189 INFO spawned: 'laravel-worker_04' with pid 778
2018-10-12 03:50:53,191 INFO spawned: 'laravel-worker_05' with pid 779
2018-10-12 03:50:53,193 INFO spawned: 'laravel-worker_06' with pid 780
2018-10-12 03:50:53,198 INFO spawned: 'laravel-worker_07' with pid 781
2018-10-12 03:50:54,200 INFO success: laravel-worker_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-12 03:50:54,200 INFO success: laravel-worker_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-10-12 03:50:54,200 INFO success: laravel-worker_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
راستی، artisan queue:work هم کار نکرد!
هر دلیلی هست فهمیدم که متد "هندل" هیچ وقت اجرا نمیشه!
حل شد، رفتم onQueue() رو حذف کردم، تازه جاب اجرا شد، بعد اونم یادم رفته بود از Auth استفاده نکنم تو Job هام، اونم حذف کردم، بعد دست پنجه نرم کردن با Permission ها آخر حل شد.
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟