Megatest

Diff
Login

Differences From Artifact [46f3492646]:

To Artifact [ed8ceb5dcd]:


41
42
43
44
45
46
47
48


49
50
51
52
53
54
55
41
42
43
44
45
46
47

48
49
50
51
52
53
54
55
56







-
+
+







	)

(defstruct srv
  (areapath #f)
  (host     #f)
  (pid      #f)
  (type     #f)
  (dir      #f)
  (sdir     #f) ;; .server directory
  (hdir     #f) ;; .server/host.pid directory
  (incoming #f)
  (dbstruct #f)
  (handler  #f)
  (obj-to-str #f)
  (str-to-obj #f)
  )

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

158

159
160
161
162
163
164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191



192
193
194
195
196
197
198
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168
169
170
171
172
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

194
195
196
197
198
199
200
201
202
203







+

-
-
-
-
+
+
+
+

-
+



















-
+
+


-
-
-
+
+
+







-
+
-




















+
-
+












-
+



















-
+
+
+







;; NOTE: This will need to be gated by write-access
;;
(define (server:setup areapath)
  (let* ((srvdat (make-srv
		  areapath: areapath
		  host:     (get-host-name) ;; likely need to replace with ip address
		  pid:      (current-process-id)
		  sdir:     (conc areapath"/.server") ;; put server artifacts here
		  ))
	 (srvdir (conc areapath"/.server/"(get-host.pid srvdat))))
    (srv-dir-set! srvdat srvdir)
    (srv-incoming-set! srvdat (conc srvdir"/incoming"))
    (create-directory srvdir #t)
	 (hdir   (conc (srv-sdir srvdat)"/"(get-host.pid srvdat))))
    (srv-hdir-set! srvdat hdir)
    (srv-incoming-set! srvdat (conc hdir"/incoming"))
    (create-directory hdir #t)
    (for-each (lambda (d)
		(create-directory (conc srvdir"/"d)))
		(create-directory (conc hdir"/"d)))
	      '("incoming" "responses"))
    srvdat))

(define *server-keep-running* #f)

;; to cleanly shut the server down set *server-keep-running* to #f
;;
(define (server:run srvdat)
  ;; create server arf
  ;; put arf in srvdat-dir
  ;; forever
  ;;    scan incoming dir
  ;;    foreach arf
  ;;       bundle into with-transaction, no-transaction
  ;;    foreach bundle
  ;;       process the request
  ;;       create results arf and write it to clients dir
  ;;       remove in-arf from incoming
  (let* ((areapath  (srv-areapath srvdat))
	 (srvdir    (srv-dir srvdat)) ;; (server:get-servinfo-dir areapath))
	 (sdir      (srv-sdir srvdat))
	 (hdir      (srv-hdir srvdat))
	 (myarf     `((h . ,(srv-host srvdat))
		      (i . ,(srv-pid  srvdat))
		      (d . ,srvdir))) ;; (srv->alist srvdat))
	 (myuuid    (write-alist->artifact srvdir myarf ptype: 'S))
	 (arf-fname (get-artifact-fname srvdir myuuid))
		      (d . ,hdir)))
	 (myuuid    (write-alist->artifact sdir myarf ptype: 'S))
	 (arf-fname (get-artifact-fname sdir myuuid))
	 (dbstruct  (srv-dbstruct srvdat)))
    (set! *server-keep-running* #t)
    (let loop ((last-access (current-seconds)))
      (let* ((start (current-milliseconds))
	     (res   (server:process-incoming srvdat))
	     (delta (- (current-milliseconds) start))
	     (timed-out (> (- (current-seconds) last-access)
			   60)) ;; accessed in last 60 seconds
			   60))) ;; accessed in last 60 seconds
	     )
	(if timed-out
	    (begin
	      (print "INFO: server has not been accessed in 60 seconds, exiting shortly.")
	      (set! *server-keep-running* #f))
	    (thread-sleep! (if (> delta 500)
			       0.1
			       0.9)))
	(if (or (> res 0) ;; res is the number of requests that were found and processed
		*server-keep-running*)
	    (loop (if (> res 0)
		      (current-seconds)
		      last-access)
		  ))))
    (delete-file arf-fname)
    ))

;; read arfs from incoming, process them and put result arfs in proper dirs
;; return number requests found and processed
;;
(define	(server:process-incoming srvdat)
  (let* ((sdir   (srv-sdir srvdat))
  (let* ((srvdir (srv-dir srvdat))
	 (hdir   (srv-hdir srvdat))
	 (indir  (srv-incoming srvdat))
	 (arfs   (glob (conc indir"/*.artifacts")))
	 (handler (srv-handler srvdat))
	 (obj->string (srv-obj-to-str srvdat))
	 (dbstruct (srv-dbstruct srvdat)))
    (let loop ((rem arfs))
      (if (not (null? arfs))
	  (let* ((arf  (car rem))
		 (dat  (read-artifact->alist arf))
		 (ruuid (alist-ref 'Z dat))
		 (host (alist-ref 'h dat))
		 (pid  (alist-ref 'i dat))
		 (dest (conc srvdir"/"host"."pid"/responses"))
		 (dest (conc sdir"/"host"."pid"/responses")) ;; the calling host area
		 (cmd  (alist-ref 'c dat))
		 (params (alist-ref 'p dat))
		 (res  (handler dbstruct cmd params))
		 (narf `((r . ,(obj->string res))
			 (P . ,ruuid))))
	    (delete-file arf) ;; add ability to save in bundles in archive area
	    (write-alist->artifact dest narf ptype: 'Q)
	    (loop (cdr rem)))))
    (length arfs)))
	  
;; start a server process (NOT start server in this process)
;;
;; maybe check load before calling this?
(define (server:launch areapath)
  (let* ((logd (conc areapath"/logs"))
	 (logf (conc logd"/from-"(get-host.pid #f)".log")))
    (if (not (file-exists? logd))(create-directory logd #t))
    (setenv "NBFAKE_LOG" logf)
    (system (conc "nbfake mtserve -start-dir "areapath))))
    



;;======================================================================
;; OLD SERVER STUFF BELOW HERE
;;======================================================================

;; ;; servers start by setting up fs transport
;; ;; and put a flag file for that ASAP.
;; ;; they then set up tcp and put a flag file for