huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

[QUESTION] Regular errors with cache in Sidekiq jobs #256

Open Floppy opened 1 week ago

Floppy commented 1 week ago

In my Sidekiq jobs, I get a lot of these failures:

Errno::ENOENT: No such file or directory @ rb_file_s_rename - (/usr/src/app/tmp/cache/.rails-settings-cached2Fv120241107-214-skp40x, /usr/src/app/tmp/cache/983/850/rails-settings-cached%2Fv1)

I assume this is because of multiple workers accessing the file at once, renaming it, etc. I'm clearing the cache before each job to make sure the jobs don't use stale settings from the main app, which is think might be causing a race condition where one worker has cleared the cache being used by another one.

  before_perform do |job|
    SiteSettings.clear_cache
  end

Is there a way to make the workers use separate cache files? They shouldn't really be removing something used by a different one.

Unfortunately I don't get a useful backtrace to identify exactly where it's happening. Has anyone seen this and solved it, or am I doing something unique and weird?

Floppy commented 1 week ago

Some possible approaches:

  1. Use a dynamic cache key generated randomly per process, like Mastodon: https://github.com/mastodon/mastodon/blob/bde0f1239ab016a6dfc8229ba579c5e2cf96a8e6/app/models/setting.rb#L75
  2. Disable cache for sidekiq workers (not sure if this is possible)
  3. Use a different cache store for sidekiq workers, like a pure memory store so there's no disk race condition; each process would inherently get its own cache then as well.