Browse Source

Merge pull request #302 from hbrunn/8.0-dead_mans_switch

[ADD] support needaction
pull/340/merge
Pedro M. Baeza 9 years ago
parent
commit
59bc36bab3
  1. 25
      dead_mans_switch_client/models/dead_mans_switch_client.py
  2. 39
      dead_mans_switch_server/models/dead_mans_switch_instance.py

25
dead_mans_switch_client/models/dead_mans_switch_client.py

@ -7,10 +7,11 @@ import logging
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
@ -26,15 +27,25 @@ class DeadMansSwitchClient(models.AbstractModel):
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([
@ -71,7 +82,7 @@ class DeadMansSwitchClient(models.AbstractModel):
{
'Content-Type': 'application/json',
}),
timeout)
timeout=timeout)
@api.model
def _install_default_url(self):

39
dead_mans_switch_server/models/dead_mans_switch_instance.py

@ -7,7 +7,7 @@ from openerp import _, api, fields, models
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'
@ -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)
@ -98,6 +99,22 @@ class DeadMansSwitchInstance(models.Model):
],
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:
@ -122,20 +139,24 @@ class DeadMansSwitchInstance(models.Model):
@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')]
Loading…
Cancel
Save