Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions dead_mans_switch_client/models/dead_mans_switch_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import os
try:
import psutil
except ImportError:
except ImportError: # pragma: no cover
psutil = None
import urllib2
from openerp import api, models
from openerp.tools.config import config

SEND_TIMEOUT = 60

Expand All @@ -26,15 +27,25 @@ def _get_data(self):
if psutil:
process = psutil.Process(os.getpid())
# psutil changed its api through versions
if process.parent:
processes = [process]
if config.get('workers') and process.parent: # pragma: no cover
if hasattr(process.parent, '__call__'):
process = process.parent()
else:
process = process.parent
if hasattr(process, 'memory_percent'):
ram = process.memory_percent()
if hasattr(process, 'cpu_percent'):
cpu = process.cpu_percent()
if hasattr(process, 'children'):
processes += process.children(True)
elif hasattr(process, 'get_children'):
processes += process.get_children(True)
for process in processes:
if hasattr(process, 'memory_percent'):
ram += process.memory_percent()
else: # pragma: no cover
ram = None
if hasattr(process, 'cpu_percent'):
cpu += process.cpu_percent()
else: # pragma: no cover
cpu = None
user_count = 0
if 'im_chat.presence' in self.env.registry:
user_count = len(self.env['im_chat.presence'].search([
Expand Down Expand Up @@ -71,7 +82,7 @@ def alive(self):
{
'Content-Type': 'application/json',
}),
timeout)
timeout=timeout)

@api.model
def _install_default_url(self):
Expand Down
39 changes: 30 additions & 9 deletions dead_mans_switch_server/models/dead_mans_switch_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class DeadMansSwitchInstance(models.Model):
_inherit = ['mail.thread']
_inherit = ['mail.thread', 'ir.needaction_mixin']
_name = 'dead.mans.switch.instance'
_description = 'Instance to monitor'
_order = 'state, partner_id'
Expand All @@ -25,7 +25,8 @@ class DeadMansSwitchInstance(models.Model):
description = fields.Char('Description')
log_ids = fields.One2many(
'dead.mans.switch.log', 'instance_id', string='Log lines')
alive = fields.Boolean('Alive', compute='_compute_alive')
alive = fields.Boolean(
'Alive', compute='_compute_alive', search='_search_alive')
alive_max_delay = fields.Integer(
'Alive delay', help='The amount of seconds without notice after which '
'the instance is considered dead', default=600)
Expand Down Expand Up @@ -98,6 +99,22 @@ def _compute_alive(self):
],
limit=1))

@api.model
def _search_alive(self, operator, value):
alive = True if operator == '=' and value or\
operator == '!=' and not value else False
self.env.cr.execute(
'select i.id from dead_mans_switch_instance i '
'left join (select instance_id, max(create_date) create_date '
'from dead_mans_switch_log group by instance_id) l '
'on l.instance_id=i.id '
"where coalesce(l.create_date, '1970-01-01'::timestamp) %s "
"now() at time zone 'utc' - "
"(2 * alive_max_delay || 'seconds')::interval "
"group by i.id " %
(alive and '>=' or '<'))
return [('id', 'in', [i for i, in self.env.cr.fetchall()])]

@api.multi
def _compute_last_log(self):
for this in self:
Expand All @@ -122,20 +139,24 @@ def _compute_last_log(self):
@api.model
def check_alive(self):
"""handle cronjob"""
for this in self.search([('state', '=', 'active')]):
if this.alive:
continue
last_post = fields.Datetime.from_string(this.message_last_post)
if not last_post or datetime.utcnow() - timedelta(
seconds=this.alive_max_delay * 2) > last_post:
this.panic()
for this in self.search(self._needaction_domain_get()):
this.panic()

@api.multi
def panic(self):
"""override for custom handling"""
self.ensure_one()
last_post = fields.Datetime.from_string(self.message_last_post)
if last_post and last_post >= datetime.utcnow() - 3 * timedelta(
seconds=self.alive_max_delay):
# don't nag too often
return
self.message_post(
type='comment', subtype='mt_comment',
subject=_('Dead man\'s switch warning: %s') %
self.display_name, content_subtype='plaintext',
body=_('%s seems to be dead') % self.display_name)

@api.model
def _needaction_domain_get(self):
return [('alive', '=', False), ('state', '=', 'active')]