Add scheduler process connection test
authorThierry Florac <tflorac@ulthar.net>
Sun, 14 Jan 2018 14:48:58 +0100
changeset 42 8cacad2470d5
parent 41 0b31d2492f15
child 43 aa374e033e84
Add scheduler process connection test
src/pyams_scheduler/scheduler.py
src/pyams_scheduler/zmi/scheduler.py
src/pyams_scheduler/zmi/templates/process-test.pt
--- a/src/pyams_scheduler/scheduler.py	Thu Jan 11 17:07:28 2018 +0100
+++ b/src/pyams_scheduler/scheduler.py	Sun Jan 14 14:48:58 2018 +0100
@@ -80,3 +80,11 @@
             return [501, "No socket handler defined in configuration file"]
         socket.send_json(['get_jobs', {}])
         return zmq_response(socket)
+
+    def test_process(self):
+        """Send test request to scheduler process"""
+        socket = self._get_socket()
+        if socket is None:
+            return [501, "No socket handler defined in configuration file"]
+        socket.send_json(['test', {}])
+        return zmq_response(socket)
--- a/src/pyams_scheduler/zmi/scheduler.py	Thu Jan 11 17:07:28 2018 +0100
+++ b/src/pyams_scheduler/zmi/scheduler.py	Sun Jan 14 14:48:58 2018 +0100
@@ -9,6 +9,11 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_form.schema import CloseButton
+from pyams_skin.interfaces.viewlet import ITableItemColumnActionsMenu
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_utils.text import text_to_html
+from pyams_zmi.control_panel import UtilitiesTable
 
 __docformat__ = 'restructuredtext'
 
@@ -33,7 +38,7 @@
 from zope.component.interfaces import ISite
 
 # import packages
-from pyams_form.form import AJAXEditForm
+from pyams_form.form import AJAXEditForm, AJAXAddForm
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.container import ContainerView
 from pyams_skin.page import DefaultPageHeaderAdapter
@@ -47,12 +52,12 @@
 from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
-from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm
+from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm, AdminDialogAddForm
 from pyams_zmi.view import AdminView
 from pyams_zmq.socket import zmq_socket, zmq_response
 from pyramid.url import resource_url
 from pyramid.view import view_config
-from z3c.form import field
+from z3c.form import field, button
 from z3c.table.column import GetAttrColumn
 from zope.interface import implementer, Interface
 
@@ -400,3 +405,77 @@
     def update(self):
         super(SchedulerJobsViewlet, self).update()
         self.table.update()
+
+
+@viewlet_config(name='test-scheduler-process.menu', context=IScheduler, layer=IAdminLayer,
+                view=UtilitiesTable, manager=ITableItemColumnActionsMenu, permission=MANAGE_SYSTEM_PERMISSION)
+class SchedulerProcessTestMenu(ToolbarMenuItem):
+    """Scheduler process test menu"""
+
+    label = _("Test process connection...")
+    label_css_class = 'fa fa-fw fa-clock-o'
+    url = 'test-scheduler-process.html'
+    modal_target = True
+    stop_propagation = True
+
+
+class ISchedulerProcessTestButtons(Interface):
+    """Scheduler process test buttons"""
+
+    close = CloseButton(name='close', title=_("Close"))
+    test = button.Button(name='test', title=_("Test connection"))
+
+
+@pagelet_config(name='test-scheduler-process.html', context=IScheduler, layer=IPyAMSLayer,
+                permission=MANAGE_SYSTEM_PERMISSION)
+class SchedulerProcessTestForm(AdminDialogAddForm):
+    """Scheduler process test form"""
+
+    @property
+    def title(self):
+        return self.context.__name__
+
+    legend = _("Test scheduler process connection")
+    icon_css_class = 'fa fa-fw fa-clock-o'
+
+    prefix = 'test_form.'
+    fields = field.Fields(Interface)
+    buttons = button.Buttons(ISchedulerProcessTestButtons)
+    ajax_handler = 'test-scheduler-process.json'
+    edit_permission = MANAGE_SYSTEM_PERMISSION
+
+    @property
+    def form_target(self):
+        return '#{0}_test_result'.format(self.id)
+
+    def updateActions(self):
+        super(SchedulerProcessTestForm, self).updateActions()
+        if 'test' in self.actions:
+            self.actions['test'].addClass('btn-primary')
+
+    def createAndAdd(self, data):
+        return self.context.test_process()
+
+
+@viewlet_config(name='test-scheduler-process.suffix', layer=IAdminLayer, manager=IWidgetsSuffixViewletsManager,
+                view=SchedulerProcessTestForm, weight=50)
+@template_config(template='templates/process-test.pt')
+class SchedulerProcessTestSuffix(Viewlet):
+    """Scheduler process test form suffix"""
+
+
+@view_config(name='test-scheduler-process.json', context=IScheduler, request_type=IPyAMSLayer,
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
+class SchedulerProcessAJAXTestForm(AJAXAddForm, SchedulerProcessTestForm):
+    """Scheduler process test form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        status, message = changes
+        if status == 200:
+            return {'status': 'success',
+                    'content': {'html': text_to_html(message)},
+                    'close_form': False}
+        else:
+            return {'status': 'info',
+                    'content': {'html': text_to_html(message)},
+                    'close_form': False}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_scheduler/zmi/templates/process-test.pt	Sun Jan 14 14:48:58 2018 +0100
@@ -0,0 +1,1 @@
+<div class="no-widget-toolbar" tal:attributes="id string:${view.__parent__.id}_test_result"></div>