| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- (in-package #:chatikbot)
- (defvar *backoff-start* 1 "Initial back-off")
- (defvar *backoff-max* 64 "Maximum back-off delay")
- (defun loop-with-error-backoff (func)
- (let ((backoff *backoff-start*))
- (loop
- do
- (handler-case
- (progn
- (funcall func)
- (setf backoff *backoff-start*))
- (error (e)
- (log:error e)
- (log:info "Backing off for" backoff)
- (sleep backoff)
- (setf backoff (min *backoff-max*
- (* 2 backoff))))
- (bordeaux-threads:timeout (e)
- (log:error e)
- (log:info "Backing off for" backoff)
- (sleep backoff)
- (setf backoff (min *backoff-max*
- (* 2 backoff))))))))
- (defun replace-all (string part replacement &key (test #'char=))
- "Returns a new string in which all the occurences of the part
- is replaced with replacement."
- (with-output-to-string (out)
- (loop with part-length = (length part)
- for old-pos = 0 then (+ pos part-length)
- for pos = (search part string
- :start2 old-pos
- :test test)
- do (write-string string out
- :start old-pos
- :end (or pos (length string)))
- when pos do (write-string replacement out)
- while pos)))
- (defmacro aget (key alist)
- `(cdr (assoc ,key ,alist :test #'equal)))
- (defun mappend (fn &rest lists)
- "Apply fn to each element of lists and append the results."
- (apply #'append (apply #'mapcar fn lists)))
- (defun random-elt (choices)
- "Choose an element from a list at random."
- (elt choices (random (length choices))))
- (defun flatten (the-list)
- "Append together elements (or lists) in the list."
- (mappend #'(lambda (x) (if (listp x) (flatten x) (list x))) the-list))
- (defun make-circular (items)
- "Make items list circular"
- (setf (cdr (last items)) items))
- (defmacro push-circular (obj circ)
- "Move circ list and set head to obj"
- `(progn
- (pop ,circ)
- (setf (car ,circ) ,obj)))
- (defmacro peek-circular (circ)
- "Get head of circular list"
- `(car ,circ))
- (defmacro pop-circular (circ)
- "Get head of circular list"
- `(pop ,circ))
- (defun flat-circular (circ)
- "Flattens circular list"
- (do ((cur (cdr circ) (cdr cur))
- (head circ)
- result)
- ((eq head cur)
- (nreverse (push (car cur) result)))
- (push (car cur) result)))
- ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/
- ;; leads to bad cutoff)
- (in-package #:local-time)
- (defun reread-timezone-repository (&key (timezone-repository *default-timezone-repository-path*))
- (check-type timezone-repository (or pathname string))
- (multiple-value-bind (valid? error)
- (ignore-errors
- (truename timezone-repository)
- t)
- (unless valid?
- (error "REREAD-TIMEZONE-REPOSITORY was called with invalid PROJECT-DIRECTORY (~A). The error is ~A."
- timezone-repository error)))
- (let* ((root-directory timezone-repository)
- (cutoff-position (length (princ-to-string root-directory))))
- (flet ((visitor (file)
- (handler-case
- (let* ((full-name (subseq (princ-to-string file) cutoff-position))
- (name (pathname-name file))
- (timezone (%realize-timezone (make-timezone :path file :name name))))
- (setf (gethash full-name *location-name->timezone*) timezone)
- (map nil (lambda (subzone)
- (push timezone (gethash (subzone-abbrev subzone)
- *abbreviated-subzone-name->timezone-list*)))
- (timezone-subzones timezone)))
- (invalid-timezone-file () nil))))
- (setf *location-name->timezone* (make-hash-table :test 'equal))
- (setf *abbreviated-subzone-name->timezone-list* (make-hash-table :test 'equal))
- (cl-fad:walk-directory root-directory #'visitor :directories nil :follow-symlinks nil
- :test (lambda (file)
- (not (find "Etc" (pathname-directory file) :test #'string=))))
- (cl-fad:walk-directory (merge-pathnames "Etc/" root-directory) #'visitor :directories nil))))
- (local-time:reread-timezone-repository :timezone-repository "/usr/share/zoneinfo/")
|