Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
redirect to running build if one exists for same artifact
  • Loading branch information
martinklepsch committed Aug 29, 2018
1 parent c31b6f8 commit 4a2f0f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
23 changes: 19 additions & 4 deletions src/cljdoc/server/build_log.clj
Expand Up @@ -3,7 +3,7 @@
[clojure.java.jdbc :as sql]
[clojure.tools.logging :as log]
[cljdoc.util.telegram :as telegram])
(:import (java.time Instant)))
(:import (java.time Instant Duration)))

(defn- now []
(str (Instant/now)))
Expand All @@ -18,7 +18,8 @@
(api-imported! [_ build-id])
(completed! [_ build-id git-result])
(get-build [_ build-id])
(recent-builds [_ limit]))
(recent-builds [_ limit])
(running-build [_ group-id artifact-id version]))

(defrecord SQLBuildTracker [db-spec]
IBuildTracker
Expand Down Expand Up @@ -61,7 +62,21 @@
(get-build [_ build-id]
(first (sql/query db-spec ["SELECT * FROM builds WHERE id = ?" build-id])))
(recent-builds [_ limit]
(sql/query db-spec ["SELECT * FROM builds ORDER BY id DESC LIMIT ?" limit])))
(sql/query db-spec ["SELECT * FROM builds ORDER BY id DESC LIMIT ?" limit]))
(running-build [_ group-id artifact-id version]
(first
(sql/query db-spec [(str "select * from builds where error is null "
"and import_completed_ts is null "
"and group_id = ? and artifact_id = ? and version = ? "
;; HACK; this datetime scoping shouldn't be required but
;; in practice it happens that some webhooks don't reach
;; the cljdoc api and builds end up in some sort of limbo
;; where neither an error nor completion has occurred
"and datetime(analysis_requested_ts) > datetime(?) "
"order by id desc "
"limit 1")
group-id artifact-id version
(str (.minus (Instant/now) (Duration/ofMinutes 10)))]))))

(defmethod ig/init-key :cljdoc/build-tracker [_ db-spec]
(log/info "Starting BuildTracker")
Expand All @@ -78,7 +93,7 @@

(def bt (->SQLBuildTracker (cljdoc.config/build-log-db)))

(recent-builds bt 1)
(running-build bt "amazonica" "amazonica" "0.3.132")

(doseq [v [:success :success-no-git :git-problem :fail :kicked-off :analysis-received :api-imported]]
(let [id (analysis-requested! bt (name v) (name v) "0.8.0")]
Expand Down
27 changes: 17 additions & 10 deletions src/cljdoc/server/pedestal.clj
Expand Up @@ -119,19 +119,26 @@
(keep identity)
(vec)))

(defn redirect-to-build-page
[ctx build-id]
{:pre [(some? build-id)]}
(assoc ctx :response {:status 303 :headers {"Location" (str "/builds/" build-id)}}))

(defn request-build
[{:keys [analysis-service build-tracker]}]
{:name ::request-build
:enter (fn [ctx]
(let [build-id (api/initiate-build
{:analysis-service analysis-service
:build-tracker build-tracker
:project (get-in ctx [:request :form-params :project])
:version (get-in ctx [:request :form-params :version])})]
(assoc ctx
:response
{:status 303
:headers {"Location" (str "/builds/" build-id)}})))})
:enter (fn request-build-handler [ctx]
(if-let [running (build-log/running-build build-tracker
(-> ctx :request :form-params :project util/group-id)
(-> ctx :request :form-params :project util/artifact-id)
(-> ctx :request :form-params :version))]
(redirect-to-build-page ctx (:id running))
(let [build-id (api/initiate-build
{:analysis-service analysis-service
:build-tracker build-tracker
:project (-> ctx :request :form-params :project)
:version (-> ctx :request :form-params :version)})]
(redirect-to-build-page ctx build-id))))})

(defn full-build
[{:keys [storage build-tracker]}]
Expand Down

0 comments on commit 4a2f0f4

Please sign in to comment.