How to setup a custom timeout for a search ?
desaintflorent opened this issue ยท 8 comments
I created a DigitalOcean droplet following your great tutorial, and I'm using it successfully with Laravel ๐
The only problem I have is when the server is down ( stopped manually or CPU at more than 100% ).
I can't find where I could set a short timeout so I could display an error ?
Right now it's loading indefinitely waiting for a response from the dead server.
Hello @desaintflorent, if I'm not wrong this package does not provide an option to set a timeout. I'm not a laravel user so I cannot provide any workaround with laravel.
If someone knows a workaround, feel free to share it on this issue ๐
Glad to read you liked the DO tutorial on our docs, we do our best to provide integrations with nice documentation!
I believe this would have to be done at the Meilisearch PHP Client level by passing a custom GuzzleHTTP instance to the client: https://github.com/meilisearch/meilisearch-php#customize-your-http-client
There isn't a way to do this with the currently. Maybe we could allow the user to pass a custom Meilisearch PHP Client in the config
This would be a great addition. However, according to this issue where I explain the context, this repo will not have any new addition that will be not consistent with larvel/scout
We might create a new repo that will be more a meilisearch-laravel-scout-extended plugin, and we will integrate this kind of possibility into it. I let this issue open since it's a good idea of improvement for the next repo (or the next version of this repo).
Hello @desaintflorent,
I'm not sure this is what you asked for, but based on what @hi019 said, you can override the MeilisearchServiceProvider to use a custom http client.
example:
php artisan make:provider MeilisearchServiceProvider
<?php
namespace App\Providers;
use MeiliSearch\Client;
class MeilisearchServiceProvider extends \Meilisearch\Scout\MeilisearchServiceProvider
{
public function register()
{
parent::register();
$this->app->singleton(Client::class, function () {
return new Client(config('meilisearch.host'), config('meilisearch.key'), new \GuzzleHttp\Client(['timeout' => 0.01]));
});
}
}
In you config/app.php
'providers' => [
// Other Service Providers
App\Providers\MeilisearchServiceProvider::class,
],
Then, you should disable Meilisearch from being auto-discovered by adding the following to your composer.json
...
"extra": {
"laravel": {
"dont-discover": [
"meilisearch/meilisearch-laravel-scout"
]
}
},
...
And if I don't miss something you should be good, the timeout will throw a MeiliSearch\Exceptions\CommunicationException
try {
$movies = Movie::search('b')->get();
} catch (\MeiliSearch\Exceptions\CommunicationException $e) {
// do something
}
If someone has a better option, I'll be glad to read it ๐
@shokme you should probably register the provider in app.php instead: https://laravel.com/docs/8.x/providers#registering-providers
@shokme you should probably register the provider in app.php instead: https://laravel.com/docs/8.x/providers#registering-providers
Indeed, I took the example from telescope local installation but you are right, I also think this is better.
Thank's all for your help !
@shokme your example is working perfectly, I understood your code but It would have taken me lots of time to figure this out :) So thank's a lot for your help.
Juste one thing, when I disable Meilisearch from being auto-discovered I had an error when running a search ( "Driver [meilisearch] not supported." )
But by not disabling it, it's working on my local environment. Will try in production soon.
Why it's important to disable it ?
I tried to reproduce your error and the only way was by not extending \Meilisearch\Scout\MeilisearchServiceProvider
from App\Providers\MeilisearchServiceProvider
.
But, the code I showed to you is mostly from telescope package. So "dont-discover" will not register the package by default and allow you to load it when needed. my mistake, you can avoid this.
Now about App\Providers\MeilisearchServiceProvider
, If I'm right and you don't extend the provider from the package, it works, good. But to be honest I'm not sure how it will be handled behind the scene.
My guess is Meilisearch package will be registered and then the custom provider will be registered and override the Client singleton. Your choice to extend or not on that one as I'm not able to give a clear answer.
By the way, today laravel-scout 9 has been released with the support of meilisearch as first party package, you might want to use it, the code of this package has been merge inside scout so it won't change anything for you.