# 当 'discourse enable\_readonly' 启用时，站点自动退出只读模式。

**URL:** https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844
**Category:** Development
**Tags:** read-only
**Created:** [2024年二月27日 05:27 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844 "2024-02-27T05:27:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月27日 05:27 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/1 "2024-02-27T05:27:12Z")

</div>

大家好，

我正在使用 Ansible 设置一些自动化部署和迁移，但遇到了只读模式的问题。

当我通过以下方式启用只读模式时：  
`docker exec app discourse enable_readonly`  
或者

```plaintext
./launcher enter app
discourse enable_readonly

```

大约 1 分 20 秒后，站点会自动退出只读模式。

如果我通过 WebUI（admin/backups 部分）启用只读模式，它会一直保持启用状态，直到我禁用它。

有什么想法吗？

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2024年二月27日 06:31 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/2 "2024-02-27T06:31:21Z")

</div>

您检查过日志吗？也许有某个后台作业或插件正在中断此过程。

---

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月27日 06:56 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/3 "2024-02-27T06:56:39Z")

</div>

嗨 Lilly，

感谢您的回复。这是一个全新的默认安装，我没有安装任何插件，也没有创建任何作业。我应该查看哪些日志？抱歉，我有点新手。

我尝试了 `tail -f`：

```plaintext
/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

```

我一边查看日志一边等待事件发生，但没有看到任何异常之处。

奇怪的是，通过 WebUI 激活时一切正常，但通过 CLI 激活时却不行。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年二月27日 18:39 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/4 "2024-02-27T18:39:36Z")

</div>

我也遇到了使用该功能的问题，包括启用和禁用只读模式。上次我最终在 rails 中运行了 `Discourse.enable_readonly_mode`。

每次发生这种情况都是紧急情况，我还没有调查过问题可能是什么。

供参考，在我自己的 Ansible 工具中，我不使用只读模式，但我使用了 [Introducing Post Deployment Migration](https://meta.discourse.org/t/introducing-post-deployment-migration/98974)

---

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月27日 18:59 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/5 "2024-02-27T18:59:17Z")

</div>

谢谢 Jay。我怀疑我正在采用一种 XY 方法来解决这个问题。我将查看部署后迁移。感谢您提供的信息。

我想另一个选择可能是通过 API 调用激活 RO。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年二月27日 19:11 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/6 "2024-02-27T19:11:43Z")

</div>

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

```

所以这可能适用于只读模式：

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

```

---

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月27日 19:13 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/7 "2024-02-27T19:13:20Z")

</div>

谢谢，非常感谢。我今晚就试试。

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [2024年二月28日 21:30 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/8 "2024-02-28T21:30:51Z")

</div>

您使用的只读模式导致了这种差异吗？

> [@SaraDev](#):
>
> - `READONLY_MODE_KEY` 主要用于备份和恢复过程，并由应用程序本身触发。也可以使用 `discourse enable_readonly` 和 `discourse disable_readonly` 命令从 Discourse 命令行界面启用或禁用此模式。但是，此密钥在容器重启后将失效。
> - `USER_READONLY_MODE_KEY` 用于管理员在管理界面中单击只读按钮时。此密钥的特殊之处在于我们不将其设置为过期密钥，因为用户启用的只读模式需要能够承受容器重启。其他密钥设置的 TTL 为 60 秒，我们有一个线程每 30 秒延长一次过期时间，以确保应用程序永远不会卡在只读模式。

---

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月28日 22:53 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/9 "2024-02-28T22:53:55Z")

</div>

感谢 JammyDodger！这些信息和链接非常有帮助。我不确定什么构成容器重启——据我所知，我的容器并没有每隔大约 1:30 重启一次，这是在使用 `discourse enable_readonly` 时 RO 模式保持启用的时间，即 `Discourse.enable_readonly_mode(Discourse::READONLY_MODE_KEY)`

但我可以确认 `Discourse.enable_readonly_mode(Discourse::USER_READONLY_MODE_KEY)` 可以“无限期”保持，这正是我想要达到的目的 👍

---

<div class="post-metadata">

### Author: ![RhysGoodwin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rhysgoodwin/32/195363_2.png) [@RhysGoodwin](https://meta.discourse.org/u/RhysGoodwin)
#### Post date: [2024年二月29日 00:20 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/10 "2024-02-29T00:20:50Z")

</div>

为了完整起见：

```plaintext
- 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

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2024年三月30日 00:21 UTC](https://meta.discourse.org/t/site-automatically-exits-read-only-mode-when-enabled-by-discourse-enable-readonly/296844/11 "2024-03-30T00:21:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
