Megatest

Check-in [e0622d9f3d]
Login
Overview
Comment:Added server:whoami? and make the server signature a pair and not a string.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.65-multi-db
Files: files | file ages | folders
SHA1: e0622d9f3d96c83e832c2e833c3d619e1ce765dd
User & Date: mrwellan on 2019-02-05 18:51:01
Other Links: branch diff | manifest | tags
Context
2019-02-05
22:29
Outlined the server handler loop check-in: 977b907588 user: matt tags: v1.65-multi-db
18:51
Added server:whoami? and make the server signature a pair and not a string. check-in: e0622d9f3d user: mrwellan tags: v1.65-multi-db
2019-02-04
23:07
yada check-in: b28d552c97 user: matt tags: v1.65-multi-db
Changes

Modified server.scm from [98632b547e] to [b1ce6afa26].

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
;;     You should have received a copy of the GNU General Public License
;;     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.
;;

;;======================================================================
;;
;; This is the Megatest specific stuff for starting and maintaining a
;; server. Stuff that talks to the server should go in client.scm.
;; General nanomsg stuff (not Megatest specific) should go in the
;; nmsg-transport.scm file.
;;
;;======================================================================

(require-extension (srfi 18) extras tcp s11n)








|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
;;     You should have received a copy of the GNU General Public License
;;     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.
;;

;;======================================================================
;;
;; This is the Megatest specific stuff for starting and maintaining a
;; server. Anything that talks to the server should go in client.scm (maybe - might get rid of client.scm)
;; General nanomsg stuff (not Megatest specific) should go in the
;; nmsg-transport.scm file.
;;
;;======================================================================

(require-extension (srfi 18) extras tcp s11n)

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
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
;; P K T S   S T U F F 
;;======================================================================

;;======================================================================
;;  N A N O M S G   B A S E D   S E R V E R
;;======================================================================

(defstruct nmsg
  (conn  #f)
  (port  #f)
  (myaddr #f)
  (hosts (make-hash-table))
  pkt
  pktspec
  pktfile


  (mutex    (make-mutex))
  )

;; make it a global? Well, it is local to nmsg module

(define *nmsg-conndat* (make-nmsg))
(nmsg-pktspec-set! *nmsg-conndat*
		   `((server (hostname . h)
			     (port     . p)
			     (pid      . i)
			     )))







;; get a port
;; start the nmsg server
;; look for other servers
;; contact other servers and compile list of servers
;; there are two types of server
;;     main servers - dashboards, runners and dedicated servers - need pkt
;;     passive servers - test executers, step calls, list-runs - no pkt
;;
(define (server:start-nmsg #!optional (force-server-type #f))
  (mutex-lock! (nmsg-mutex *nmsg-conndat*))
  (let* ((server-type  (or force-server-type
			   (if (args:any? "-run" "-server")
			       'main
			       'passive)))
	 (port-num     (portlogger:open-run-close portlogger:find-port))
	 (nmsg-conn    (nmsg:start-server port-num))
	 (pktspec      (nmsg-pktspec *nmsg-conndat*))
	 (mtdir        (or (get-environment-variable "MT_RUN_AREA_HOME")
				 (if (file-exists? "megatest.config")
				     (current-directory)
				     (begin
				       (print "ERROR: We don't appear to be in a megatest area and MT_RUN_AREA_HOME is not set.")
				       #f))))
	 (pktdir       (conc mtdir
			     "/.server-pkts")))
    (if (not mtdir)
	#f
	(begin
	  (if  (not (directory? pktdir))(create-directory pktdir))
	  ;; server is started, now create pkt if needed
	  (if (eq? server-type 'main)
	      (begin
		(nmsg-pkt-set! *nmsg-conndat* 
			       (write-alist->pkt
				pktdir 
				`((hostname . ,(get-host-name))
				  (port     . ,port-num)
				  (pid      . ,(current-process-id)))
				pktspec: pktspec
				ptype:   'server))
		(nmsg-pktfile-set! *nmsg-conndat* (conc pktdir "/" (nmsg-pkt *nmsg-conndat*) ".pkt"))))



	  (nmsg-conn-set! *nmsg-conndat* nmsg-conn)
	  (nmsg-port-set! *nmsg-conndat* port-num)
	  (mutex-unlock! (nmsg-mutex *nmsg-conndat*))
	  #t))))

;; Call this to start the actual server
;;
;; start_server
;;
;;   mode: '
;;
(define (server:launch mode)
  (let ((start-time (current-seconds)))
    (server:start-nmsg mode)
    (let loop ((dead-time (- (current-seconds) start-time)))
      (thread-sleep! 1)
      (if (< dead-time 10)
	  (loop (- (current-seconds) start-time))
	  (print "Timed out. Exiting")))))

(define (server:shutdown)
  (let ((conn (nmsg-conn    *nmsg-conndat*))
	(pktf (nmsg-pktfile *nmsg-conndat*))
	(port (nmsg-port    *nmsg-conndat*)))
    (if conn
	(begin
	  (if pktf (delete-file* pktf))
	  (server:send-all "imshuttingdown")
	  (nmsg:close conn)
	  (portlogger:open-run-close portlogger:release-port port)))))

(define (server:send-all msg)
  #f)









;; look up all pkts and get the server id (the hash), port, host/ip

;;
(define (server:get-all)







  '())

(define (server:get-my-best-address)
  (ip->string (car (filter (lambda (x)
			     (not (eq? (u8vector-ref x 0) 127)))
			   (vector->list (hostinfo-addresses (hostname->hostinfo "zeus")))))))

;; whoami? I am my pkt
;;
(define (server:whoami?)
  (nmsg-pkt *nmsg-conndat*))

;;======================================================================
;; S E R V E R   U T I L I T I E S 
;;======================================================================

;; get a signature for identifing this process
(define (server:get-process-signature)
  (conc (get-host-name) " " (current-process-id)))


;; ;; Get the transport
;; (define (server:get-transport)
;;   (if *transport-type*
;;       *transport-type*
;;       (let ((ttype (string->symbol







|




|


>
>



|

|
|




>
>
>
>
>
>
>









|





|
|
|
<
<
|
|
|









|
|
|
|
|
|
|
|
|
>
>
>
|
|
|


















|
|
|










>
>
>
>
>
>
>
>

>


>
>
>
>
>
>
>









|
|







|







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
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
204
205
206
207
208
209
210
211
212
213
214
;; P K T S   S T U F F 
;;======================================================================

;;======================================================================
;;  N A N O M S G   B A S E D   S E R V E R
;;======================================================================

(defstruct area
  (conn  #f)
  (port  #f)
  (myaddr #f)
  (hosts (make-hash-table))
  pktid  ;; get pkt from hosts table if needed
  pktspec
  pktfile
  pktsdir
  mtrah
  (mutex    (make-mutex))
  )

;; make it a global? Well, it is local to area module

(define *area-conndat* (make-area))
(area-pktspec-set! *area-conndat*
		   `((server (hostname . h)
			     (port     . p)
			     (pid      . i)
			     )))

(define (server:get-mtrah)
  (or (get-environment-variable "MT_RUN_AREA_HOME")
      (if (file-exists? "megatest.config")
	  (current-directory)
	  #f)))

;; get a port
;; start the nmsg server
;; look for other servers
;; contact other servers and compile list of servers
;; there are two types of server
;;     main servers - dashboards, runners and dedicated servers - need pkt
;;     passive servers - test executers, step calls, list-runs - no pkt
;;
(define (server:start-nmsg #!optional (force-server-type #f))
  (mutex-lock! (area-mutex *area-conndat*))
  (let* ((server-type  (or force-server-type
			   (if (args:any? "-run" "-server")
			       'main
			       'passive)))
	 (port-num     (portlogger:open-run-close portlogger:find-port))
	 (area-conn    (nmsg:start-server port-num))
	 (pktspec      (area-pktspec *area-conndat*))
	 (mtdir        (or (server:get-mtrah)


			   (begin
			     (print "ERROR: megatest.config not found and MT_RUN_AREA_HOME is not set.")
			     #f)))
	 (pktdir       (conc mtdir
			     "/.server-pkts")))
    (if (not mtdir)
	#f
	(begin
	  (if  (not (directory? pktdir))(create-directory pktdir))
	  ;; server is started, now create pkt if needed
	  (if (eq? server-type 'main)
	      (begin
		(area-pktid-set! *area-conndat* 
				 (write-alist->pkt
				  pktdir 
				  `((hostname . ,(get-host-name))
				    (port     . ,port-num)
				    (pid      . ,(current-process-id)))
				  pktspec: pktspec
				  ptype:   'server))
		(area-pktfile-set! *area-conndat* (conc pktdir "/" (area-pktid *area-conndat*) ".pkt"))))
	  ;; set all the area info in the 
	  (area-pktsdir-set! *area-conndat* pktdir)
	  (area-mtrah-set!   *area-conndat* mtdir)
	  (area-conn-set!    *area-conndat* area-conn)
	  (area-port-set!    *area-conndat* port-num)
	  (mutex-unlock! (area-mutex *area-conndat*))
	  #t))))

;; Call this to start the actual server
;;
;; start_server
;;
;;   mode: '
;;
(define (server:launch mode)
  (let ((start-time (current-seconds)))
    (server:start-nmsg mode)
    (let loop ((dead-time (- (current-seconds) start-time)))
      (thread-sleep! 1)
      (if (< dead-time 10)
	  (loop (- (current-seconds) start-time))
	  (print "Timed out. Exiting")))))

(define (server:shutdown)
  (let ((conn (area-conn    *area-conndat*))
	(pktf (area-pktfile *area-conndat*))
	(port (area-port    *area-conndat*)))
    (if conn
	(begin
	  (if pktf (delete-file* pktf))
	  (server:send-all "imshuttingdown")
	  (nmsg:close conn)
	  (portlogger:open-run-close portlogger:release-port port)))))

(define (server:send-all msg)
  #f)

;; given a area record look up all the packets
(define (server:get-all-server-pkts rec)
  (let ((all-pkt-files (glob (conc (area-pktsdir rec) "/*.pkt")))
	(pktspec       (area-pktspec rec)))
    (map (lambda (pkt-file)
	   (read-pkt->alist pkt-file pktspec: pktspec))
	 all-pkt-files)))

;; look up all pkts and get the server id (the hash), port, host/ip
;; store this info in the global struct *area-conndat*
;;
(define (server:get-all)
  ;; readll all pkts
  ;; foreach pkt; if it isn't me ping the server; if alive, add to hosts hash, else rm the pkt
  '())

;; send out an "I'm about to exit notice to all known servers"
;;
(define (server:announce-death)
  '())

(define (server:get-my-best-address)
  (ip->string (car (filter (lambda (x)
			     (not (eq? (u8vector-ref x 0) 127)))
			   (vector->list (hostinfo-addresses (hostname->hostinfo "zeus")))))))

;; whoami? I am my pkt
;;
(define (server:whoami? area)
  (hash-table-ref/default (area-hosts area)(area-pktid area) #f))

;;======================================================================
;; S E R V E R   U T I L I T I E S 
;;======================================================================

;; get a signature for identifing this process
(define (server:get-process-signature)
  (cons (get-host-name)(current-process-id)))


;; ;; Get the transport
;; (define (server:get-transport)
;;   (if *transport-type*
;;       *transport-type*
;;       (let ((ttype (string->symbol