-
Notifications
You must be signed in to change notification settings - Fork 2
/
hotfix.js
134 lines (122 loc) · 5.45 KB
/
hotfix.js
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
/***** BEGIN LICENCE BLOCK ***************************/
/* The initial developer of the code is Kevin Turner */
/* kpturner.co.uk */
/* */
/* Portions created by Kevin Turner Systems Ltd" are */
/* Copyright (c) 2017 K P Turner Ltd. */
/* All Rights Reserved. */
/***** END LICENCE BLOCK *****************************/
/********************************************************************
/* @Function : hotfix
/* @Description : Chromis hotfix. If you have made changes and want clients to
/* pick up those changes when they next start RNS, you can run this
/* node app to do so: "node hotfix.js"
/* If you want active clients to be prompted to reload,
/* run "node hotfix.js --reload"
/* NOTE: Only impacts clients that connect to the server instance that houses this
/* module and that have access to the same instance of the XVer table.
/* @Author : Kevin Turner
/* @Date : Apr 2016
/*
/********************************************************************
/*
/* Modification log
/* ================
/*
/* Inits Date Modification
/* ===== ==== ============
/*
/********************************************************************/
var sails;
// Override the grunt hook to do nothing and ensure we don't mess with the database
var cfg={
hooks: {
grunt: false,
},
models: {
migrate: "safe"
},
};
// Lift sails with the above config
require('sails').load(cfg,function(err, sailsInstance) {
// Initialise the "sails" global
sails=sailsInstance;
// Increment the sub-version number then trigger an
// event for clients to reload
xver.findOne({bcver:{">":""}}).exec(function(err,v){
// Now we can increment the sub version. NOTE: this
// is a terrible way of doing it, because of course
// somebody else could jump in and change it before
// this instance does. To avoid that we need to do an atomic
// update. However, it is low risk and hardly earth shattering
// in the unlikely event it happens
if (err) {
sails.log.error(err);
process.exit(1);
}
else {
// Increment and update
v.subver++;
xver.update(v.id,v).exec(function(err,updated){
if (err) {
sails.log.error(err)
process.exit(1);
}
else {
// Trigger an event on the server properties datapush listener
var feedId=sails.config.chromis.serverName+"_properties";
// Prompt the clients to reload if the user has specified --reload in the command line args
var reloadClient=false;
if (process.argv.length>=3) {
reloadClient=(process.argv[2].indexOf("--reload")>=0);
}
// Build the event json
var data={
urlargs:"v="+sails.config.chromis.version+"."+v.subver.toString(),
reloadclient:reloadClient,
}
// Trigger the web socket event
WS.triggerEvent(feedId,data,function(){
sails.log.info("Hotfix complete");
process.exit(0);
})
}
})
}
});
});
process.on('uncaughtException', function (err) {
try {
var msg='uncaughtException: '+err.message;
msg+="</br>"+err.stack;
if (sails) {
sails.log.error('uncaughtException:', err.message);
sails.log.error(err.stack);
if (Utility) {
Utility.diagnosticEmail(msg,"Application crash",function(){
process.exit(1);
});
}
else {
process.exit(1);
}
// Make sure we exit if the email sending fails
setTimeout(function(){process.exit(1)},5000)
}
else {
console.log(msg);
process.exit(1); //Forever should restart us;
}
}
catch(e) {
if (sails) {
sails.log.error("Error handling uncaught exception");
sails.log.error(e);
}
else {
console.log("Error handling uncaught exception");
console.log(e);
}
process.exit(1); //Forever should restart us;
}
})