Parcourir la source

Fix mongo connections

Innocenty Enikeew il y a 11 ans
Parent
commit
f4b07acc13
1 fichiers modifiés avec 22 ajouts et 14 suppressions
  1. 22 14
      src/web.lisp

+ 22 - 14
src/web.lisp

@@ -28,21 +28,27 @@
 
 (defmethod restas:initialize-module-instance :before ((module (eql #.*package*)) context)
   (restas:with-context context
-    (alexandria:when-let (file (probe-file "config.lisp"))
+    (alexandria:when-let (file (probe-file
+                                (merge-pathnames "src/config.lisp"
+                                                 (asdf:component-pathname
+                                                  (asdf:find-system '#:timeliner)))))
       (load file))
-    (cl-mongo:mongo :host *mongo-host*)
-    (cl-mongo:db.use *mongo-db*)
     (when *run-cron*
       (restas:context-add-variable
        context '*cron-timers*
        (loop for (function schedule) in *crons*
           do (log:info "Starting cron" function schedule)
           collect
-            (clon:schedule-function function
-                                    (clon:make-scheduler
-                                     (apply #'clon:make-typed-cron-schedule schedule)
-                                     :allow-now-p t)
-                                    :thread t))))))
+            (clon:schedule-function
+             (let ((f function))
+               (lambda ()
+                 (let ((cl-mongo::*mongo-registry* nil)) ;; make connections thread-local
+                   (cl-mongo:with-mongo-connection (:host *mongo-host* :db *mongo-db*)
+                     (funcall f)))))
+             (clon:make-scheduler
+              (apply #'clon:make-typed-cron-schedule schedule)
+              :allow-now-p t)
+             :thread t))))))
 
 (defmethod restas:finalize-module-instance :after ((module (eql #.*package*)) context)
   (let ((timers (restas:context-symbol-value context '*cron-timers*)))
@@ -187,16 +193,18 @@
          (end (or (ignore-errors (ms->ts (parse-integer (hunchentoot:get-parameter "end"))))
                   (local-time:timestamp+ start 1 :day))))
     (with-output-to-string (stream)
-      (yason:encode
-       (cl-mongo:docs
-        (cl-mongo:db.sort "events" ($between "ts" start end) :field "ts" :limit 2000))
-       stream))))
+      (cl-mongo:with-mongo-connection (:host *mongo-host* :db *mongo-db*)
+        (yason:encode
+         (cl-mongo:docs
+          (cl-mongo:db.sort "events" ($between "ts" start end) :field "ts" :limit 2000))
+         stream)))))
 
 (restas:define-route api/events/delete ("api/events/:id"
                                         :method :DELETE
                                         :content-type "application/json")
-  (cl-mongo:db.delete "events" (cl-mongo:$ "_id" (cl-mongo::make-bson-oid
-                                                  :oid (crypto:hex-string-to-byte-array id))))
+  (cl-mongo:with-mongo-connection (:host *mongo-host* :db *mongo-db*)
+    (cl-mongo:db.delete "events" (cl-mongo:$ "_id" (cl-mongo::make-bson-oid
+                                                    :oid (crypto:hex-string-to-byte-array id)))))
   "OK")
 
 (defparameter +timeliner.css+