Wednesday, November 26, 2014

Process events, functions in Background in Odoo / OpenERP by using cron

In Odoo / OpenERP, sometime we have to wait for a while when we press on a button. We have to wait after all processes completed. Like a Send Mail button, after pressing Send Mail button we have to wait program to send out the email, as long as work is in not done the openerp UI is blocked.
We can escape this waitting. In Odoo / OpenERP, we can have our processes run in background, by using ir.cron of OpenERP like this way:

Example:
In function _let_process_in_backrgound which is invoked by our button, we use a cron task, the cron task will call method:  'function': 'do_something', of model: 'model': 'hr_timesheet_sheet.sheet' (for example), agurments are:


def _let_process_in_backrgound(self, cr, uid, data, context = {}):
    timesheet_id = 1
    str_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    nextcall = parser.parse(str_now) + datetime.timedelta(seconds = 60)
    pool.get('ir.cron').create(cr, uid, {
                'name': 'Cron job to do something that take very long time',
                'user_id': uid,
                'model': 'hr_timesheet_sheet.sheet',
                'function': 'do_something',
                'nextcall': nextcall,
                'args': repr([timesheet_id])
            })
    return {}


In Odoo / OpenERP, we have the method do_something like this:

def do_something(self, cr, uid, timesheet_id):
    ....
    ....
    return True

By this way we created a cron task in OpenERP to be executed at nextcall time.

Tuesday, November 11, 2014

Automatic start Xen guests after Dom0 reboot

We can start virtual machines using "xm create". However after rebooting the xen host machine, the virtual machines (xen guests) are not started automatically. 
We can make a xen guest start automatically after xen host reboot. 
Let's say the configuration for the virtual machine is in /etc/xen/XenGuest1.cfg

So we can do like this:

mkdir -p /etc/xen/auto
cd /etc/xen/auto
ln -s /etc/xen/XenGuest1.cfg .
cd -

This creates a link to autostart the virtual machine.
and "XenGuest1.cfg" file must contain the following entries:

on_xend_stop  = 'shutdown'
on_xend_start = 'start'