Reputation: 5334
Can anyone please suggest any solution for gateway timeout 504 error when running cron job on shared hosing. I have tried sleep function but it didn't work, I have following function for cron job -
public function checkOrderStatus(){
$orders = Order::select('id')
->whereNotIn('status', ['COMPLETED', 'CANCELLED', 'PARTIAL', 'REFUNDED'])
->where('api_order_id', '!=', null)
->orderBy('id', 'desc')
->pluck('id')
->toArray();
$collection = collect($orders);
$chunks = $collection->chunk(20);
$request = new \Illuminate\Http\Request();
foreach($chunks as $ids){
foreach($ids as $id){
$request->replace(['id' => $id]);
$rep = $this->getOrderStatusFromAPI($request);
}
sleep(10);
}
}
getOrderStatusFromAPI() function calls 3rd party API to fetch some records. checkOrderStatus() function currently fetching around 300 records in each cron call. Please suggest any solution other than server upgrade. Thanks much!!
Upvotes: 1
Views: 1599
Reputation: 541
There are multiple solutions to your problem. If you're using NGINX with FastCGI try:
Changes in php.ini
Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):
max_execution_time = 150
Changes in PHP-FPM
Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):
request_terminate_timeout = 150
Changes in Nginx Config
Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:
location ~* \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_read_timeout 150;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Reload PHP-FPM and Nginx:
service php–fpm restart
service nginx restart
Upvotes: 1