nimblehq / nimble-survey-web

Nimble Survey API for Mobile Challenge and Certification
0 stars 3 forks source link

Add a rake task to clean expired access tokens #114

Open hoangmirs opened 11 months ago

hoangmirs commented 11 months ago

Why

namespace :token do
  desc "Clean expired tokens"
  task clean: :environment do
    delete_before = 1.days.ago
    expire = [
      "(revoked_at IS NOT NULL AND revoked_at < :delete_before) OR " +
      "(expires_in IS NOT NULL AND (created_at + expires_in * INTERVAL '1 second') < :delete_before)",
      { delete_before: delete_before }
    ]

    Doorkeeper::AccessToken.where(expire).delete_all
  end
end

Who Benefits?

Maintainers

olivierobert commented 11 months ago

One way to run such a task without setting up anything additional on Heroku (see: https://devcenter.heroku.com/articles/scheduler) is to use a GitHub Actions workflow with the trigger schedule: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule.

In both cases, there is a cost of running it. However, on GH Actions, it would be within our free tier of 3,000 minutes and by using a workflow, it can be trigged manually when needed.

IMO I doubt it would be needed to be run daily. Weekly might be sufficient 💡