               response)))))
     (do
       (log/info "Not logging modifying requests.")
-      (fn [request]
-        (next-handler request)))))
+      next-handler)))
 
 (def audit-logs-file-format)
+
+(def default-audit-flush-millis (* 5 60 1000))
 
 (defn store-audit-logs!
   "Stores the audit logs in an S3 bucket."
   [audit-logs bucket]
   (let [previous-logs (empty-logs audit-logs)]
-    (try
-      (let [file-content (string/join "\n" (map json/write-str previous-logs))
-            file-stream (-> file-content (.getBytes "UTF-8") (ByteArrayInputStream.))
-            content-length (count file-content)
-            file-name (t/now)]
-        (s3/put-object :bucket-name bucket
-                       :key file-name
-                       :input-stream file-stream
-                       :metadata {:content-length content-length}))
-      (catch Throwable t
-        (add-logs audit-logs previous-logs)
-        (log/warn "Could not store audit logs because of %s." (str t))))))
+    (when (seq previous-logs)
+      (try
+        (let [file-content (string/join "\n" (map json/write-str previous-logs))
+              file-stream (-> file-content (.getBytes "UTF-8") (ByteArrayInputStream.))
+              content-length (count file-content)
+              file-name (str (t/now))]
+          (s3/put-object :bucket-name bucket
+                         :key file-name
+                         :input-stream file-stream
+                         :metadata {:content-length content-length}))
+        (catch Throwable t
+          (add-logs audit-logs previous-logs)
+          (log/warn "Could not store audit logs because of %s." (str t)))))))
+
+(defn schedule-audit-log-flusher [bucket audit-logs configuration]
+  (when bucket
+    (let [flush-interval (or (:audit-flush-millis configuration) default-audit-flush-millis)
+          pool (at/mk-pool :cpu-count 1)]
+      (at/every flush-interval #(store-audit-logs! audit-logs bucket) pool :initial-delay flush-interval)
+      pool)))
+
+(defn stop-audit-log-flusher [bucket audit-logs pool]
+  (when bucket
+    (dosync
+      (at/stop-and-reset-pool! pool :strategy :kill)
+      (store-audit-logs! audit-logs bucket))))
 
 (defn start-component
   "Starts the http component."

  (fn [request]
    (next-handler (assoc request :configuration configuration))))

(defcommand fetch-tokeninfo
  [tokeninfo-url access-token]
  (let [response (client/get tokeninfo-url
                             {:query-params     {:access_token access-token}

                        (s1st/ring collect-audit-logs audit-logs audit-logs-bucket)
                        (s1st/executor :resolver resolver-fn))]

        (merge component {:audit-logs-bucket audit-logs-bucket
                          :audit-logs        audit-logs})

        (if (:no-listen? configuration)
          (merge component {:httpd                nil
                            :hystrix-httpd        nil
                            :handler              handler
                            :audit-log-flush-pool nil})
          (merge component {:httpd                (jetty/run-jetty handler (merge configuration
                                                                                  {:join? false}))
                            :hystrix-httpd        (run-hystrix-jetty (merge configuration {:join? false
                                                                                           :port  7979}))
                            :handler              handler
                            :audit-log-flush-pool (schedule-audit-log-flusher audit-logs-bucket audit-logs configuration)}))))))

(defn stop-component
  "Stops the http component."
