Commit 9e362707 authored by jssuzanne's avatar jssuzanne
Browse files

update scripts

--HG--
branch : 8.0
parent 3ca8a9518bba
.pyc$
syntax: glob
*.swp
*.orig
*.pyc
*.pyo
*.log
*\#
*.\#*
*~
anybus.scripts.egg-info/
# flake8: noqa
import connection
import consumer
import message
......@@ -40,7 +40,6 @@
'security/ir.model.access.csv',
'view/menu.xml',
'view/connection.xml',
'view/consumer.xml',
'view/message.xml',
],
'demo_xml': [
......
......@@ -31,9 +31,9 @@ class anybusConnection(models.Model):
broker_connection = None
def action_purge(self, queue):
pass
# TODO
def get_consumers(self):
# (queue name, model, method name)
return [('erp_ping', self._name, 'ping')]
def publish(self, exchange, routing_key, data, contenttype):
pass
......
# -*- coding: utf-8 -*-
from openerp import models, fields
class AnybusConsumer(models.Model):
_name = 'anybus.consumer'
_description = 'Anybus Consumer'
name = fields.Char(required=True)
queue = fields.Char(required=True)
model = fields.Many2one('res.model', required=True)
consumer_method = fields.Char(required=True)
def process(self):
pass
......@@ -10,22 +10,27 @@ class AnybusMessage(models.Model):
]
_order = 'sequence, id'
consumer = fields.Many2one('anybus.consumer', readonly=True)
name = fields.Char(compute="get_name")
sequence = fields.Integer(required=True, default=100)
exchange = fields.Char(readonly=True)
binding_key = fields.Char(readonly=True)
sequence = fields.Integer(required=True, default=100)
queue = fields.Char(readonly=True)
model = fields.Many2one('res.model')
method = fields.Char()
message = fields.Binary(required=True)
contenttype = fields.Char(required=True)
error = fields.Text(required=True)
name = fields.Char(compute="get_name")
def _needaction_domain_get(self, *a, **kw):
return [('id', '!=', 0)]
def get_name(self):
for this in self:
if this.consumer:
this.name = this.consumer.name
if this.queue:
this.name = 'consume %s' % this.queue
else:
this.name = "%s => %s" % (this.exchange, this.binding_key)
......
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_anybus_consumer_tree" model="ir.ui.view">
<field name="name">anybus.consumer.tree</field>
<field name="model">anybus.consumer</field>
<field name="type">tree</field>
<field name="priority" eval="8"/>
<field name="arch" type="xml">
<tree string="Consumers" editable="bottom">
<field name="name"/>
<field name="queue"/>
<field name="consumer_method"/>
</tree>
</field>
</record>
<record id="view_anybus_consumer_search" model="ir.ui.view">
<field name="name">anybus.consumer.search</field>
<field name="model">anybus.consumer</field>
<field name="type">search</field>
<field name="priority" eval="8"/>
<field name="arch" type="xml">
<search string="Consumers">
<field name="name"/>
<field name="queue"/>
<field name="consumer_method"/>
</search>
</field>
</record>
<record model="ir.actions.act_window" id="act_open_anybus_consumer_view">
<field name="name">Consumers</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">anybus.consumer</field>
<field name="view_type">form</field>
<field name="view_mode">tree</field>
<field name="search_view_id" ref="view_anybus_consumer_search"/>
<field name="domain">[]</field>
<field name="context">{}</field>
</record>
<record model="ir.actions.act_window.view" id="act_open_anybus_consumer_view_tree">
<field name="act_window_id" ref="act_open_anybus_consumer_view"/>
<field name="sequence" eval="10"/>
<field name="view_mode">tree</field>
<field name="view_id" ref="view_anybus_consumer_tree"/>
</record>
<menuitem id="menu_anybus_consumer"
sequence="20"
parent="menu_anybus_general"
action="act_open_anybus_consumer_view"/>
</data>
</openerp>
......@@ -36,8 +36,18 @@
<field name="error"/>
</page>
<page string="Entité">
<label for="consumer"/>
<field name="consumer"/>
<label for="queue"/>
<field name="queue"/>
<group
colspan="4"
attrs="{
'invisible': [('queue', '=', '')],
'required': [('queue', '!=', '')],
}"
>
<field name="model"/>
<field name="method"/>
</group>
<label for="exchange"/>
<field name="exchange"/>
<label for="binding_key"/>
......@@ -57,12 +67,12 @@
<search string="Messages a traiter">
<field name="contenttype"/>
<field name="error" nolabel="1"/>
<field name="consumer"/>
<field name="queue"/>
<field name="exchange"/>
<field name="binding_key"/>
<group string="Group by...">
<filter name="Content types" context="{'group_by': 'contenttype'}" />
<filter name="Consumer" context="{'group_by': 'consumer'}" />
<filter name="queue" context="{'group_by': 'queue'}" />
<filter name="Exchange" context="{'group_by': 'exchange'}" />
<filter name="Binding key" context="{'group_by': 'binding_key'}" />
</group>
......@@ -97,7 +107,7 @@
<menuitem id="menu_anybus_message"
parent="menu_anybus_general"
sequence="30"
sequence="20"
action="act_open_anybus_message_view"/>
</data>
</openerp>
from pika import SelectConnection, URLParameters
import logging
from time import sleep
from uuid import uuid1
logger = logging.getLogger(__name__)
......@@ -35,7 +37,8 @@ class Consumer:
logger.info('Channel opened')
self._channel = channel
self._channel.add_on_close_callback(self.on_channel_closed)
self.declare_consumers()
for queue, model, method in self.odoo_connection.get_consumers():
self.declare_consumer(queue, model, method)
def on_channel_closed(self, channel, reply_code, reply_text):
logger.warning('Channel %i was closed: (%s) %s',
......@@ -74,6 +77,10 @@ class Consumer:
for consumer_tag in self._consumer_tags:
self._channel.basic_cancel(self.on_cancelok, consumer_tag)
def on_cancelok(self, unused_frame):
logger.info('RabbitMQ acknowledged the cancellation of the consumer')
self._channel.close()
def run(self):
self._connection = self.connect()
self._connection.ioloop.start()
......@@ -85,10 +92,17 @@ class Consumer:
self._connection.ioloop.start()
logger.info('Connexion stopped')
def declare_consumer(self):
for consumer in self.session.env['anybus.consumer'].search([]):
self._consumer_tags.append(self._channel.basic_consume(
None, queue=queue, passive=True
)
if not res:
raise Exception("No existing queue %r" % queue)
def declare_consumer(self, queue, model, method):
print(' ==> ', queue, model, method)
def on_message(unused_channel, basic_deliver, properties, body):
uuid = uuid1()
print('received', uuid)
sleep(2)
self._channel.basic_ack(basic_deliver.delivery_tag)
sleep(2)
print('ack', uuid)
self._consumer_tags.append(self._channel.basic_consume(
on_message, queue=queue, arguments=dict(model=model, method=method)
))
......@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
version = '1.0'
setup(
name='advanced.attachment.scripts',
name='anybus.scripts',
version=version,
description="Odoo / AnyBus scripts",
long_description=""" """,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment