Closed tobz-nz closed 3 years ago
This is probably the JsonSettingStore
keeping the settings in memory, not a caching issue. This library was very much written with HTTP handling in mind, so it kind of assumes that once settings are read into memory, that memory will be wiped in no more than a few seconds.
I don't know what the appopriate way to hook this into the Laravel task framework is, possibly $schedule->before(...)
, but what you'd want to do is find a way to call Settings::load(true)
to force a reload at the beginning of any task run.
Ideally we should add an unload()
function which just forces settings to be reloaded next time you try to get or set any settings, because forcing a load shouldn't really be necessary.
Ah-ha, Settings::load(true)
looks like it might solve the issue.
Would be good to add this method to the documentation for cases such as mine.
Happy to PR that if you want.
A PR would be really nice :)
I had a similar issue and I looked at the Settings::load(true)
solution, but that would require running it in everything that goes to the queue and uses settings, which is not just jobs, but also emails, notifications, some events and some other jobs added by vendor code that I don't want to change.
So instead I decided to create a custom setting store that restarts the queue after saving settings. Something similar would work for json as well.
Here's my code:
new class:
use anlutro\LaravelSettings\DatabaseSettingStore as Base;
use Artisan;
use Illuminate\Database\Connection;
class CustomDatabaseSettingStore extends Base
{
public function __construct(Connection $connection)
{
parent::__construct(
$connection,
config('settings.table'),
config('settings.keyColumn'),
config('settings.valueColumn')
);
}
protected function write(array $data)
{
parent::write($data);
// restart queue workers to force them to read new settings.
Artisan::call('queue:restart');
}
}
in AppServiceProvider::boot()
Setting::extend('customDatabaseSettingStore', function($app) {
return $app->make(CustomDatabaseSettingStore::class);
});
and set 'store' => 'customDatabaseSettingStore'
in settings config.
I'm been debugging an issue in my app that seems to be coming from the fact that caching does not seem to be disabled in settings.
By this I mean fetching a value from the settings store twice in the save process does not refect changes that may have happened between those 2 calls.
In my case this is a scheduled task that refreshes an API access token as it expires - but any queued jobs (run with horizon) do not get the new value until the horizon process is restarted.
My quick fix for now is to manually fetch the required data in my queued jobs(in a container binding).
I'm using the JSON store.
My settings:
The cache is disabled and even tried setting the TTL to 0.
laravel v6.18.2 settings v0.11.0