Skip to content

Commit

Permalink
Merge pull request #266 from ixcat/issue-251
Browse files Browse the repository at this point in the history
+= +dj/kill_quick.m: programmatic kill function (#251)
  • Loading branch information
guzman-raphael authored Nov 12, 2020
2 parents 273dfee + 693749d commit d7f7223
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 32 deletions.
69 changes: 37 additions & 32 deletions +dj/kill.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,50 @@

function kill(restriction, connection, order_by)

if nargin < 3
order_by = {};
end
if nargin < 3
order_by = {};
end

if nargin < 2
connection = dj.conn;
end
if nargin < 2
connection = dj.conn;
end

qstr = 'SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()';
qstr = 'SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()';

if nargin && ~isempty(restriction)
qstr = sprintf('%s AND (%s)', qstr, restriction);
end
if nargin && ~isempty(restriction)
qstr = sprintf('%s AND (%s)', qstr, restriction);
end

if isempty(order_by)
qstr = sprintf('%s ORDER BY id', qstr);
else
if iscell(order_by)
qstr = sprintf('%s ORDER BY %s', qstr, strjoin(order_by, ','));
if isempty(order_by)
qstr = sprintf('%s ORDER BY id', qstr);
else
qstr = sprintf('%s ORDER BY %s', qstr, order_by);
if iscell(order_by)
qstr = sprintf('%s ORDER BY %s', qstr, strjoin(order_by, ','));
else
qstr = sprintf('%s ORDER BY %s', qstr, order_by);
end
end
end

while true
query(connection, qstr)
id = input('process to kill (''q''-quit, ''a''-all) > ', 's');
if ischar(id) && strncmpi(id, 'q', 1)
break
elseif ischar(id) && strncmpi(id, 'a', 1)
res = query(connection, qstr);
id = double(res.ID)';
for i = id
query(connection, 'kill {Si}', i)
while true
query(connection, qstr)
id = input('process to kill (''q''-quit, ''a''-all) > ', 's');
if ischar(id) && strncmpi(id, 'q', 1)
break
elseif ischar(id) && strncmpi(id, 'a', 1)
res = query(connection, qstr);

res = cell2struct(struct2cell(res), lower(fieldnames(res)));

id = double(res.id)';
for i = id
query(connection, 'kill {Si}', i)
end
break
end
id = sscanf(id,'%d');
if ~isempty(id)
query(connection, 'kill {Si}', id(1))
end
break
end
id = sscanf(id,'%d');
if ~isempty(id)
query(connection, 'kill {Si}', id(1))
end

end
37 changes: 37 additions & 0 deletions +dj/kill_quick.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
% Kill database connections without prompting.
% dj.kill_quick() kills MySQL server connections matching 'restriction',
% returning the number of terminated connections.
%
% Restrictions are specified as strings and can involve any of the attributes
% of information_schema.processlist: ID, USER, HOST, DB, COMMAND, TIME,
% STATE, INFO.
%
% Examples:
% dj.kill_quick('HOST LIKE "%compute%"') terminates connections from hosts
% containing "compute" in their hostname.
%
% dj.kill_quick('TIME > 600') terminates all connections older than 10
% minutes.

function nkill = kill_quick(restriction, connection)

if nargin < 2
connection = dj.conn;
end

qstr = 'SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()';

if nargin && ~isempty(restriction)
qstr = sprintf('%s AND (%s)', qstr, restriction);
end

res = query(connection, qstr);

res = cell2struct(struct2cell(res), lower(fieldnames(res)));

nkill = 0;
for id = double(res.id)'
query(connection, 'kill {Si}', id);
nkill = nkill + 1;
end
end

0 comments on commit d7f7223

Please sign in to comment.