rails-letsencrypt icon indicating copy to clipboard operation
rails-letsencrypt copied to clipboard

Implement ActiveSupport notifications

Open espen opened this issue 2 years ago • 5 comments

Is there a recommend way of getting notified of failed renewals? I see that https://github.com/elct9620/rails-letsencrypt/blob/master/lib/tasks/letsencrypt_tasks.rake#L15 just puts out "Could not renew domain". What do you think of implementing ActiveSupport notifications here? Then a developer could subscribe to the event and handle this case (for example alert the admin or remove the domain). Or any alternative way to listen to such events?

espen avatar Mar 04 '22 10:03 espen

I think ActiveSupport notification is a good idea, but we may need to consider to it is running inside the Rake task. The notification may not be correctly subscribed. I think we can add ActiveJob support as a scheduler job.

elct9620 avatar Mar 04 '22 13:03 elct9620

Should not be a problem running in a rake task, it is already in the Rails env.

I think just adding ActiveSupport::Notifications.instrument either in the model (certificate.renew) or related concern should do the trick. But I have not looked at the code too closely to say where it is most appropriate to trigger any event for error/status.

espen avatar Mar 07 '22 09:03 espen

Let's add this feature before the next release.

elct9620 avatar Nov 07 '23 05:11 elct9620

Or is there any alternative way of handling it? The goal here is to be able to handle renewal failures. One option is to just copy the rake task and modify it to handle failures. Another option is for renew to raise an error when not succeeding.

espen avatar Nov 07 '23 10:11 espen

We need some refactoring for it. However in the first phase to make it available, I plan to put some hook when Certificate#renew is called so that we can subscribe to it.

For example:

def renew
  ActiveSupport::Notifications.instrument('letsencrypt.renew', domain: domain) do
    verify && issue
  end
end
ActiveSupport::Notifications.subscribe('letsencrypt.renew') do |*args|
  # filter by args.payload[:exception]
end

elct9620 avatar Nov 07 '23 10:11 elct9620