Skip to content
Snippets Groups Projects
Commit edafc10c authored by Allen Gilliland's avatar Allen Gilliland
Browse files

use the events system for triggering a database sync when a :database-create event is triggered.

parent f13c45ab
Branches
Tags
No related merge requests found
......@@ -5,6 +5,7 @@
[metabase.api.common :refer :all]
[metabase.db :refer :all]
[metabase.driver :as driver]
[metabase.events :as events]
(metabase.models common
[hydrate :refer [hydrate]]
[database :refer [Database]]
......@@ -31,10 +32,7 @@
;; TODO - we should validate the contents of `details` here based on the engine
(check-superuser)
(let-500 [new-db (ins Database :name name :engine engine :details details)]
;; kick off background job to gather schema metadata about our new db
(future (driver/sync-database! new-db))
;; make sure we return the newly created db object
new-db))
(events/publish-event :database-create new-db)))
(defendpoint GET "/form_input"
"Values of options for the create/edit `Database` UI."
......
(ns metabase.events.sync-database
(:require [clojure.core.async :as async]
[clojure.tools.logging :as log]
[metabase.config :as config]
[metabase.db :as db]
[metabase.driver :as driver]
[metabase.events :as events]
[metabase.models.database :refer [Database]]))
(def sync-database-topics
"The `Set` of event topics which are subscribed to for use in database syncing."
#{:database-create})
(def ^:private sync-database-channel
"Channel for receiving event notifications we want to subscribe to for database sync events."
(async/chan))
;;; ## ---------------------------------------- EVENT PROCESSING ----------------------------------------
(defn process-sync-database-event
"Handle processing for a single event notification received on the revisions-channel"
[sync-database-event]
;; try/catch here to prevent individual topic processing exceptions from bubbling up. better to handle them here.
(try
(log/info "holla")
(when-let [{topic :topic object :item} sync-database-event]
(when-let [database (db/sel :one Database :id (events/object->model-id topic object))]
(log/info "kicking off" database)
;; just kick off a sync on another thread
(future (driver/sync-database! database))))
(catch Throwable e
(log/warn (format "Failed to process sync-database event. %s" (:topic sync-database-event)) e))))
;;; ## ---------------------------------------- LIFECYLE ----------------------------------------
;; this is what actually kicks off our listener for events
(when (config/is-prod?)
(log/info "Starting database sync events listener")
(events/start-event-listener sync-database-topics sync-database-channel process-sync-database-event))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment