Skip to content

Commit dc90faf

Browse files
committed
Poll Order for READY status
1 parent e6e23de commit dc90faf

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/certificaat/acme4j/order.clj

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
(ns certificaat.acme4j.order
22
(:require [certificaat.domain :refer [Certificaat]]
3-
[certificaat.utils :refer [load-url]]
3+
[certificaat.utils :refer [load-url exit]]
4+
[clojure.core.async :as a :refer [<! <!! >!! chan thread go-loop]]
45
[clojure.tools.logging :as log]
56
[clojure.java.io :as io])
67
(:import [org.shredzone.acme4j Order]
78
[org.shredzone.acme4j Status]
8-
[org.shredzone.acme4j.exception AcmeProtocolException]
9+
[org.shredzone.acme4j.exception AcmeException AcmeRetryAfterException AcmeServerException AcmeProtocolException]
910
[java.net URL]))
1011

1112

@@ -36,3 +37,20 @@
3637
(= Status/INVALID (.getStatus this)))
3738
(marshal [this path]
3839
(spit path (.getLocation this))))
40+
41+
(defn ready-to-finalize? [order]
42+
(let [c (chan)]
43+
(a/thread (loop [y 1
44+
ms nil]
45+
(<!! (a/timeout (or ms 15000)))
46+
(log/debug "Retrieving order status, attempt" y ms)
47+
(cond
48+
(d/ready? order) (do (log/debug "Order is ready")
49+
true)
50+
(d/invalid? order) (exit 1 "Order is invalid")
51+
(> y 10) (exit 1 "Too many attempts ")
52+
:else (recur (inc y) (try
53+
(.update order)
54+
(catch AcmeRetryAfterException e
55+
(log/error (.getMessage e))
56+
(.getRetryAfter e)))))))))

src/certificaat/kung_fu.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
csrb (certificate/prepare domain-keypair domains (:organisation options))
123123
csr (.getEncoded csrb)]
124124
(certificate/persist-certificate-request csrb (str config-dir domain "/cert.csr"))
125-
(.execute order csr)))
125+
(when (<!! (order/ready-to-finalize? order)) (.execute order csr))))
126126

127127
(defn certificate-location [{{{path :path enabled :enabled} :copy-to-path} :plugins config-dir :config-dir domain :domain :as options}]
128128
(if enabled path (str config-dir domain "/")))

test/certificaat/kung_fu_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
csrb (certificate/prepare domain-keypair domains (:organisation options))
206206
csr (.getEncoded csrb)]
207207
(certificate/persist-certificate-request csrb (str (:config-dir options) (:domain options) "/cert.csr"))
208-
(.execute order csr)
208+
(when (<!! (order/ready-to-finalize? order)) (.execute order csr))
209209
(.update order)
210210
(is (= true (valid? order)))))
211211

0 commit comments

Comments
 (0)