aboutsummaryrefslogtreecommitdiff
path: root/nar-herder/recent-changes.scm
blob: 5a6c2a0e69180aade804b757410993e3cc3ae187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;;; Nar Herder
;;;
;;; Copyright © 2021 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (nar-herder recent-changes)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (logging logger)
  #:use-module (web uri)
  #:use-module (guix narinfo)
  #:use-module (nar-herder database)
  #:export (start-recent-change-removal-and-database-dump-fiber
            start-recent-change-listener-fiber))

(define (start-recent-change-removal-and-database-dump-fiber database
                                                             database-dump-filename
                                                             check-interval
                                                             recent-changes-limit)
  (define (update-database-dump)
    (let ((temp-database-dump-filename
           (string-append database-dump-filename ".tmp")))

      (when (file-exists? temp-database-dump-filename)
        (delete-file temp-database-dump-filename))

      (dump-database database temp-database-dump-filename)

      (rename-file temp-database-dump-filename
                   database-dump-filename)

      (simple-format (current-error-port)
                     "updated database dump\n")))

  (spawn-fiber
   (lambda ()
     (while #t
       (with-exception-handler
           (lambda (exn)
             (simple-format
              (current-error-port)
              "exception in recent change removal thread: ~A\n"
              exn))
         (lambda ()
           (let ((recent-changes-id-for-deletion
                  (database-get-recent-changes-id-for-deletion database
                                                               recent-changes-limit)))
             (when recent-changes-id-for-deletion
               (when database-dump-filename
                 (update-database-dump))

               (let ((deleted-recent-changes
                      (database-call-with-transaction
                       database
                       (lambda _
                         (database-delete-recent-changes-with-id-below
                          database
                          recent-changes-id-for-deletion)))))
                 (simple-format (current-error-port)
                                "deleted ~A recent changes\n"
                                deleted-recent-changes)))

             (sleep check-interval)))
         #:unwind? #t)))))

(define (start-recent-change-listener-fiber database
                                            mirror-channel
                                            removal-channel)
  (define (process-addition-change change-details)
    (let ((narinfo
           (call-with-input-string
               (assq-ref change-details 'data)
             (lambda (port)
               (read-narinfo port
                             "https://narherderdummyvalue")))))
      (for-each
       (lambda (uri)
         (log-msg 'DEBUG "processing recent change triggered fetch of " (uri-path uri))
         (put-message mirror-channel (list 'fetch (uri-path uri))))
       (narinfo-uris narinfo))))

  (define (process-removal-change change-details)
    (log-msg 'DEBUG "processing recent change triggered removal of "
             (assq-ref change-details 'data))
    (put-message removal-channel
                 (list 'remove (assq-ref change-details 'data))))

  (spawn-fiber
   (lambda ()
     (log-msg 'DEBUG "starting to listen for recent changes")
     (let ((after-initial
            (database-select-latest-recent-change-datetime database)))
       (let loop ((after after-initial)
                  (last-processed-recent-changes
                   (database-select-recent-changes database after-initial)))
         (sleep 10)

         (match
             (with-exception-handler
                 (lambda (exn)
                   (log-msg 'ERROR "exception in recent change listener " exn)
                   #f)
               (lambda ()
                 (let ((recent-changes
                        (database-select-recent-changes database after)))
                   (for-each
                    (lambda (change-details)
                      (when (not (member change-details
                                         last-processed-recent-changes))
                        (let ((change (assq-ref change-details 'change)))
                          (cond
                           ((string=? change "addition")
                            (process-addition-change change-details))
                           ((string=? change "removal")
                            (process-removal-change change-details))
                           (else #f)))))
                    recent-changes)

                   recent-changes))
               #:unwind? #t)
           (#f (loop after '()))
           (recent-changes
            (loop (assq-ref (last recent-changes)
                            'datetime)
                  recent-changes))))))))