I'm having trouble with an application where i do a lot of api calls in the backend. Because of this the application gets a timeout error.
Someone suggested I should use Queues. I have tried to do so whit redis. The function goes in the job and uses the handler but I expected the page to load with the data I give it without the data from the api while teh api-call is going through on the background. Instead it just goed through like when i hadn't used the queue. I tried to follow a tutorial to do this but they weren't doing exactly the same and I couldn't adjust it so it would work for me.
For info about what i do in the job. I get comments out a csv and the I use the numbers in the comments to call to the api and i get a json of like 8-10 fields. I need to call the api about 650 times so it takes o long when i want to save the data to the database. I use one insert at a time to usee "caching" so i wont do the same call twice.
This is the controller where I call on the job.
class ImportController extends Controller
{
public function checkErrors(Request $request)
{
$this->checkAgainstDocuments($csv_id);
$supplierErrorIds=$this->checkSupplierErrors($parameters, $company , $csv_id);
$timesheetErrors=TsData::whereIn('id', $supplierErrorIds)->sortable()->paginate(20);
return view('show_errors', compact('timesheetErrors', 'company', 'csv_id'));
}
public function checkAgainstDocuments($csv_id)
{
GetFromDocumentsAPI::dispatch($csv_id)->delay(now()->addMinutes(10));
}
}
This is the job I use:
class GetFromDocumentsAPI implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $csv_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($csv_id)
{
//
$this->csv_id=$csv_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$comments = TsData::select('id', 'comments')->where('csv_id', $this->csv_id)->get()->toArray();
$commentIDs = array();
foreach ($comments as $comment) {
preg_match_all('/(\d{5,})/', $comment['comments'], $out, PREG_PATTERN_ORDER);
foreach ($out as $item) {
$commentIDs[$comment['id']] = $item;
}
}
$commentIDs = array_filter($commentIDs);
$apiKey=config('app.apiKey');
$documentsResponse = array();
Issue::truncate();
$arrayTest=[];
foreach ($commentIDs as $key => $commentID) {
foreach ($commentID as $item) {
$issue = Issue::where('id', $item)->first();
if ($issue === null) {
try {
$url = file_get_contents('https://documents.calibrate.be/issues/' . $item . '.json?key=' . $apiKey);
$json = json_decode($url, true);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "
";
}
$issue = Issue::Create(['id'=>$item, 'name'=>$json['issue']['subject'], 'projectId'=>$json['issue']['project']['id'], 'priority'=>$json['issue']['priority']['id']]);
}
}
}
}
config/queue.php
'default' => env('QUEUE_DRIVER', 'redis'),
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],