Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39539
1
##############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2017, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
16
#
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
###
23
Hub Registration (recording number of clients)
24
###
25
26
winston = require('winston')
27
misc = require('smc-util/misc')
28
{defaults, required} = misc
29
30
# Global variables
31
database_is_working = false
32
the_database = undefined
33
the_host = undefined
34
the_port = undefined
35
the_interval = undefined
36
the_clients = []
37
38
number_of_clients = () ->
39
return (C for id,C of the_clients when not C._destroy_timer? and not C.closed).length
40
41
exports.number_of_clients = () ->
42
if not the_database?
43
throw new Error("database not yet set")
44
return number_of_clients()
45
46
register_hub = (cb) ->
47
if not the_database?
48
cb?("database not yet set")
49
return
50
the_database.register_hub
51
host : the_host
52
port : the_port
53
clients : number_of_clients()
54
ttl : 3*the_interval
55
cb : (err) ->
56
if err
57
database_is_working = false
58
winston.debug("Error registering with database - #{err}")
59
else
60
database_is_working = true
61
cb?(err)
62
63
exports.database_is_working = ->
64
return database_is_working
65
66
exports.start = (opts) ->
67
opts = defaults opts,
68
database : required
69
clients : required
70
host : required
71
port : required
72
interval_s : required
73
the_database = opts.database
74
the_clients = opts.clients
75
the_host = opts.host
76
the_port = opts.port
77
the_interval = opts.interval_s
78
register_hub()
79
setInterval(register_hub, the_interval*1000)
80
81