Explorar el Código

Handler cases

Innocenty Enikeew hace 11 años
padre
commit
9058c7b1db
Se han modificado 5 ficheros con 37 adiciones y 24 borrados
  1. 3 1
      src/financisto.lisp
  2. 8 6
      src/foursquare.lisp
  3. 10 8
      src/locations.lisp
  4. 8 6
      src/twitter.lisp
  5. 8 3
      src/web.lisp

+ 3 - 1
src/financisto.lisp

@@ -180,4 +180,6 @@
               (db.insert "events" new-doc)))))))
 
 (defun on-cron ()
-  (import-financisto-events (find-last-backup)))
+  (handler-case
+      (import-financisto-events (find-last-backup))
+    (error (e) (princ e))))

+ 8 - 6
src/foursquare.lisp

@@ -50,9 +50,11 @@
          when doc collect doc))))
 
 (defun on-cron ()
-  (let* ((last-checkin (first (docs (db.sort "events" ($ "type" "checkin") :field "ts"
-                                             :asc nil :limit 1))))
-         (from (and last-checkin (ms->ts (cl-mongo::raw (get-element "ts" last-checkin)))))
-         (checkins (load-checkins from)))
-    (format t "Got ~A checkins from ~A~%" (length checkins) from)
-    (save-events checkins)))
+  (handler-case
+      (let* ((last-checkin (first (docs (db.sort "events" ($ "type" "checkin") :field "ts"
+                                                 :asc nil :limit 1))))
+             (from (and last-checkin (ms->ts (cl-mongo::raw (get-element "ts" last-checkin)))))
+             (checkins (load-checkins from)))
+        (format t "Got ~A checkins from ~A~%" (length checkins) from)
+        (save-events checkins))
+    (error (e) (princ e))))

+ 10 - 8
src/locations.lisp

@@ -194,11 +194,13 @@
              (get-element "timestampMs" less)))))))
 
 (defun on-cron ()
-  (let* ((last-loc (first (docs (db.sort "locations" :all :field "timestampMs"
-                                         :asc nil :limit 1))))
-         (from (or (and last-loc (doc->ts last-loc))
-                   (local-time:timestamp- (today) 3 :day)))
-         (to (local-time:timestamp-minimum
-              (local-time:timestamp+ from 3 :day)
-              (local-time:now))))
-    (import-location-events from to)))
+  (handler-case
+      (let* ((last-loc (first (docs (db.sort "locations" :all :field "timestampMs"
+                                             :asc nil :limit 1))))
+             (from (or (and last-loc (doc->ts last-loc))
+                       (local-time:timestamp- (today) 3 :day)))
+             (to (local-time:timestamp-minimum
+                  (local-time:timestamp+ from 3 :day)
+                  (local-time:now))))
+        (import-location-events from to))
+    (error (e) (princ e))))

+ 8 - 6
src/twitter.lisp

@@ -49,9 +49,11 @@
      append (mapcar #'make-twitter-doc data)))
 
 (defun on-cron ()
-  (let* ((last-twit (first (docs (db.sort "events" ($ "type" "twitter") :field "ts"
-                                          :asc nil :limit 1))))
-         (since-id (if last-twit (get-element "twitter.id" last-twit) 1))
-         (twits (load-twits since-id)))
-    (format t "Got ~A twits from ~A~%" (length twits) since-id)
-    (save-events twits)))
+  (handler-case
+      (let* ((last-twit (first (docs (db.sort "events" ($ "type" "twitter") :field "ts"
+                                              :asc nil :limit 1))))
+             (since-id (if last-twit (get-element "twitter.id" last-twit) 1))
+             (twits (load-twits since-id)))
+        (format t "Got ~A twits from ~A~%" (length twits) since-id)
+        (save-events twits))
+    (error (e) (princ e))))

+ 8 - 3
src/web.lisp

@@ -5,7 +5,8 @@
 
 (in-package :cl-user)
 (restas:define-module #:timeliner.web
-  (:use :cl :parenscript #:timeliner.utils))
+  (:use :cl :parenscript #:timeliner.utils)
+  (:export #:*mongo-db* #:*mongo-host*))
 (in-package #:timeliner.web)
 
 (restas::register-pkgmodule-traits 'timeliner.web
@@ -21,12 +22,16 @@
   "List of cron functions with their schedules")
 (defvar *cron-timers* nil)
 
+;; db settings
+(defvar *mongo-host* nil "mongo db host name")
+(defvar *mongo-db* nil "mongo db database name")
+
 (defmethod restas:initialize-module-instance :before ((module (eql #.*package*)) context)
   (restas:with-context context
     (alexandria:when-let (file (probe-file "config.lisp"))
       (load file))
-    (cl-mongo:mongo :host "10.8.0.6")
-    (cl-mongo:db.use "timeline")
+    (cl-mongo:mongo :host *mongo-host*)
+    (cl-mongo:db.use *mongo-db*)
     (when *run-cron*
       (restas:context-add-variable
        context '*cron-timers*