Skip to content

Instantly share code, notes, and snippets.

@johansolve
Last active August 29, 2015 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johansolve/afd58ea1a22a352977db to your computer and use it in GitHub Desktop.
Save johansolve/afd58ea1a22a352977db to your computer and use it in GitHub Desktop.
[
/*
Look for net->wait failure and restart Lasso site if it stops working
to prevent issues with POP and SMTP.
Put this file in web root and watch_net_failure_startup.lasso in LassoStartup
Johan Sölve 2015-08-05
http://lasso.2283332.n4.nabble.com/SMTP-and-POP-problems-on-8-6-3-Mac-caused-by-net-gt-wait-td4644107.html
Requires showduration
https://gist.github.com/johansolve/1851bbeab7e0c552d429
*/
auth_admin;
!global_defined('g_watch_net_failure_status') ? global('g_watch_net_failure_status'='waiting for startup');
!global_defined('g_watch_net_failurecount') ? global('g_watch_net_failurecount'=0);
local('failurecount'=@$g_watch_net_failurecount);
protect;
local('net' = net);
handle;
// make sure we close the connection in the end, no matter what
if(error_code != 0);
$g_watch_net_failure_status += ' ' + error_msg;
log_critical('net->wait error ' + error_code + ' ' + error_msg);
/if;
#net -> close;
local_remove('net');
/handle;
local('connect' = #net -> connect('localhost', 80));
if(#connect == Net_ConnectOK);
local('waitresult' = #net -> Wait(2, net_waitwrite));
null(#net -> Write('GET / HTTP/1.0r\n\r\n'));
null(#net -> Wait(2, net_waitread));
local('result'=string(#net -> Read(32768)) -> substring(1,8));
if(#waitresult == net_waittimeout
&& #result -> beginswith('HTTP/'));
// we got a timeout status but still got a valid response, that's not right
// Increment error counter
#failurecount += 1;
log_critical('net->wait failurecount ' + #failurecount);
else;
// reset counter
#failurecount > 0
? log_critical('net->wait success with '
+ (map(0='net_waittimeout', 1='net_waitread', 2='net_waitwrite') -> find(#waitresult))
+ ' ' + #result
+ ' , resetting failurecount');
#failurecount = 0;
/if;
$g_watch_net_failure_status = date -> format('%Q %T') + ' failurecount ' + #failurecount + ' ' + #result;
else;
$g_watch_net_failure_status = date -> format('%Q %T') + ' Connect failed';
/if;
if(#failurecount >= 3);
log_critical('net->wait failure detected, restarting site');
log_critical('Lasso uptime: ' + showduration(lasso_uptime));
// Give log_critial time to write to errors database before restarting
sleep(1000);
// Restart site
site_restart;
/if;
/protect;
]
[
{
/*
Look for net->wait failure and restart Lasso site if it stops working
to prevent issues with POP and SMTP.
Put this file in LassoStartup
Johan Sölve 2015-08-05
http://lasso.2283332.n4.nabble.com/SMTP-and-POP-problems-on-8-6-3-Mac-caused-by-net-gt-wait-td4644107.html
*/
!global_defined('g_watch_net_failure_status') ? global('g_watch_net_failure_status'='waiting for startup');
!global_defined('g_watch_net_failurecount') ? global('g_watch_net_failurecount'=0);
sleep(30*1000);
local('failurecount'=@$g_watch_net_failurecount);
log_critical('net->wait failure detection active');
while(true);
protect;
handle;
if(error_code != 0);
$g_watch_net_failure_status += ' ' + error_msg;
log_critical('net->wait startup error ' + error_code + ' ' + error_msg);
/if;
/handle;
// Perform check and restart site if necessary
// Administrator privileges are required for site_restart
include_url('http://127.0.0.1/watch_net_failure.lasso',
-username='siteadmin',
-password='*****');
// Sleep a short while if failure has been detected previously, otherwise sleep normally
sleep((#failurecount > 0 ? 2 | 15) * 1000);
/protect;
/while;
} -> asasync(-name=response_localpath + ': watch net->wait failure');
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment