Site automatically exits read-only mode when enabled by 'discourse enable_readonly'

AI-generated summary

The discussion revolves around the issue of the site automatically exiting read-only mode when enabled by ‘discourse enable_readonly’. The original poster, RhysGoodwin, mentions that when enabling read-only mode via the command line interface, it only stays enabled for approximately 1 minute and 20 seconds, whereas enabling it via the WebUI keeps it enabled until manually disabled.

RhysGoodwin checked the logs but didn’t find anything unusual. pfaffman shared a similar experience and suggested running Discourse.enable_readonly_mode in rails instead. RhysGoodwin considered using an API call or exploring Post Deployment Migration as an alternative.

JammyDodger pointed out that the difference might be due to the type of read-only mode used. The READONLY_MODE_KEY is mainly used for backup and restore processes and does not survive container restarts, whereas the USER_READONLY_MODE_KEY is used when an admin enables read-only mode and survives container restarts.

RhysGoodwin confirmed that using Discourse.enable_readonly_mode(Discourse::USER_READONLY_MODE_KEY) keeps the read-only mode enabled indefinitely, achieving the desired outcome. The solution was implemented using an Ansible shell command, as shown in RhysGoodwin’s updated code snippet.

Hi folks,

I’m setting up some automation for deployment and migration with Ansible but I’ve got an issue with read-only mode.

When I enabled read-only with:
docker exec app discourse enable_readonly
or

./launcher enter app
discourse enable_readonly

After ~1:20 the site exits read-only mode all by itself.

If I enable read-only mode via the WebUI (admin/backups section), it stays enabled until I disable it.

Any thoughts?

Did you check your logs? perhaps you have a background job or plugin somewhere that is interrupting the process.

Hi Lilly,

Thanks for your response. This is a fresh default install and I’ve not installed any plugins or created any jobs. Which logs am I best to look at? Sorry I’m a bit of a noob.

I’ve tried tail -f on:

/var/discourse/shared/standalone/log/rails/unicorn.stdout.log
/var/discourse/shared/standalone/log/rails/unicorn.stderr.log
/var/discourse/shared/standalone/log/rails/production_errors.log
/var/discourse/shared/standalone/log/rails/production.log
/var/discourse/shared/standalone/log/rails/sidekiq.log
/var/discourse/shared/standalone/log/var-log/redis/current

I waited for the event to occur while following the logs but I didn’t see anything that stood out.

It’s odd to me that it works perfectly when activated via the WebUI but not via cli.

I have had trouble using that as well, both for enabling and disabling readonly mode. The last time I ended up running Discourse.enable_readonly_mode in rails.

It’s always an emergency when it happens and I haven’t investigated what the issue might be.

FWIW, in my own Ansible tooling I don’t use read-only mode, but do use Introducing Post Deployment Migration

Thanks Jay. I suspect I’m taking an XY approach to the problem. I’ll check out the Post Deployment Migration. Thanks for that.

I guess the other option might be to activate RO via an API call.

- name: post migrations
  shell: "docker exec  {{ discourse_yml }} bash -c 'rake db:ensure_post_migrations db:migrate'"
  become: yes

so this might work for read-only mode:

- name: post migrations
  shell: "docker exec  {{ discourse_yml }} bash -c 'echo Discourse.enable_readonly_mode | rails c"
  become: yes

Thank you, much appreciated. I’ll give this a go tonight.

Is the difference down to the read-only mode you’re using?

Thanks JammyDodger! The info and link were very helpful. I’m not sure what constitutes a container restart - as far as I know my container is not restarting every ~1:30 which is as long as the RO mode stays enabled when using discourse enable_readonly i.e. Discourse.enable_readonly_mode(Discourse::READONLY_MODE_KEY)

I can confirm however that Discourse.enable_readonly_mode(Discourse::USER_READONLY_MODE_KEY) “sticks” indefinitely, which is what I was trying to achieve :+1:

For completeness:

- name: Set read-only mode for live forum host
  ansible.builtin.shell: |
      docker exec app bash -c 'echo Discourse.enable_readonly_mode\(Discourse::USER_READONLY_MODE_KEY\) | rails c'
  register: enable_readonly_output