From 13092d9ad1ae4ee3b0691b459a29e9bb9b8af019 Mon Sep 17 00:00:00 2001 From: Chonlasith Jucksriporn Date: Thu, 24 Oct 2024 15:34:36 +0700 Subject: [PATCH 1/2] feat: Purge only draft that older than 6 hours --- .github/workflows/purgedraft.yml | 18 ++++++++++++++++++ cdmm/app/controllers/cdmm_controller.rb | 21 +++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/purgedraft.yml diff --git a/.github/workflows/purgedraft.yml b/.github/workflows/purgedraft.yml new file mode 100644 index 0000000..8384d25 --- /dev/null +++ b/.github/workflows/purgedraft.yml @@ -0,0 +1,18 @@ +name: "Purge obsolete draft" + +on: + schedule: + - cron: "0 0 * * *" + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: curl + uses: enflo/curl-action@master + with: + curl: {{ CURL ARGUMENTS }} \ No newline at end of file diff --git a/cdmm/app/controllers/cdmm_controller.rb b/cdmm/app/controllers/cdmm_controller.rb index 27b15ab..f72fe4d 100644 --- a/cdmm/app/controllers/cdmm_controller.rb +++ b/cdmm/app/controllers/cdmm_controller.rb @@ -14,8 +14,16 @@ class CdmmController < ApplicationController # draft form will be purged regularly def purge - Evaluation.where(:form_status => :draft).destroy_all - render_empty + deleted_rows = Evaluation.where(:form_status => :draft, :created_at => ...6.hours.ago) + deleted_count = deleted_rows.count + deleted_rows.destroy_all + headers = [ + { + :key => "X-Rows-Affected", + :value => "#{deleted_count}" + } + ] + render_empty headers end def index @@ -699,9 +707,14 @@ def generate_unique_form_key end end - def render_empty + def render_empty(headers = []) respond_to do |format| - format.any { head :ok } + format.any do + headers.each do |header| + response.headers[header[:key]] = header[:value] + end + head :ok + end end end From e0540addc80aa5e3674e178aa8d46a75569bc8b6 Mon Sep 17 00:00:00 2001 From: Chonlasith Jucksriporn Date: Thu, 24 Oct 2024 15:36:46 +0700 Subject: [PATCH 2/2] fix: env name --- .github/workflows/purgedraft.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/purgedraft.yml b/.github/workflows/purgedraft.yml index 8384d25..4c27051 100644 --- a/.github/workflows/purgedraft.yml +++ b/.github/workflows/purgedraft.yml @@ -15,4 +15,4 @@ jobs: - name: curl uses: enflo/curl-action@master with: - curl: {{ CURL ARGUMENTS }} \ No newline at end of file + curl: ${{ env.CURL_ARGUMENTS }}