pycharm-odoo icon indicating copy to clipboard operation
pycharm-odoo copied to clipboard

High CPU usage on PyCharm

Open jcfernandez-890825 opened this issue 8 months ago • 14 comments

While I was trying to create a script to do auto-upgrades, my laptop CPU usage went of the chart all of a sudden, and now is very hard to add just 1 line of code.

I have a 32GB of RAM and a 12th Gen Intel Core i7-1270P with 12 cores, so I don't think this is a hardware issue.

Here is the code

# -*- coding: utf-8 -*-
# set server timezone in UTC before time module imported
__import__('os').environ['TZ'] = 'UTC'

import logging
import sys

import odoo
from odoo.tools import config


# Get the logger
_logger = logging.getLogger(__name__)


def _upd_db(cr, db_nm):
    # Get the superuser ID
    uid = odoo.SUPERUSER_ID
    # Get the environment context
    ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
    # Get the environment
    env = odoo.api.Environment(cr, uid, ctx)
    # Get the module model
    Mdl = env['ir.module.module']
    # Update module list
    Mdl.update_list()
    # Find module `xgo_mdl_auto_upd`
    xgo_mdl_auto_upd = Mdl.search([('name', '=', 'xgo_mdl_auto_upd')])
    # If module `xgo_mdl_auto_upd` does not exist:
    if not xgo_mdl_auto_upd:
        # Log a message and continue
        _logger.error('Module `xgo_mdl_auto_upd` does not exist')
        return
    # If module `xgo_mdl_auto_upd` is uninstalled
    if xgo_mdl_auto_upd.state == 'uninstalled':
        # Install module `xgo_mdl_auto_upd`
        xgo_mdl_auto_upd.button_immediate_install()
        # Since the module `xgo_mdl_auto_upd` was installed.
        # We need to remove the saved checksums to force the update of all modules.
        from odoo.addons.module_auto_update.models.module import PARAM_INSTALLED_CHECKSUMS
        env["ir.config_parameter"].set_param(PARAM_INSTALLED_CHECKSUMS, "{}")
        # Commit the changes
        env.cr.commit()
        # Return False to indicate that the database was not processed
        return False
    # If module `xgo_mdl_auto_upd` installation was unfinished
    elif xgo_mdl_auto_upd.state == 'to install':
        # Cancel the installation
        xgo_mdl_auto_upd.button_install_cancel()
        # Commit the changes
        env.cr.commit()
        # Return False to indicate that the database was not processed
        return False
    # If module `xgo_mdl_auto_upd` is not installed
    elif xgo_mdl_auto_upd.state != 'installed':
        # Get module `xgo_mdl_auto_upd` state
        xgo_mdl_auto_upd_state = xgo_mdl_auto_upd.state
        # Get the module states as a dictiona
        mdl_sts_dct = dict(Mdl._fields['state']._description_selection(Mdl.env))
        # Log a message and continue
        _logger.error(
            'Module `xgo_mdl_auto_upd` state is "%s" instead of "%s"',
            mdl_sts_dct[xgo_mdl_auto_upd_state], mdl_sts_dct['installed']
        )
        return
    # Update all modules with changes checksums
    getattr(Mdl, 'upgrade_changed_checksum')()
    # Commit the changes
    env.cr.commit()
    # Log a message
    _logger.info('Database "%s" processed successfully', db_nm)


def main():
    # Get the logger
    global _logger
    # Get the arguments
    args = sys.argv[1:]
    # Set the program name in the configuration parser
    config.parser.prog = 'xgo_cli_auto_upd'
    # Parse the configuration
    config.parse_config(args)
    # Report the configuration of the server
    odoo.cli.server.report_configuration()
    # Get the database list
    db_lst = odoo.service.db.list_dbs(True)
    # Get a list of incompatible databases
    bad_db_lst = odoo.service.db.list_db_incompatible(db_lst)
    # Filter-out the incompatible databases
    db_lst = list(set(db_lst) - set(bad_db_lst))
    # Sort the database list
    db_lst.sort()
    # For each database in the list
    for db_nm in db_lst:
        # Add 50 lines to the log
        _logger.info('\n' * 50)
        # Log a message
        _logger.info('Processing database "%s"', db_nm)
        # Unfinished variable
        unfinished = True
        while unfinished:
            # Get model registry
            registry = odoo.registry(db_nm)
            # Get the cursor
            with registry.cursor() as cr:
                try:
                    # Perform the database update
                    # If the database was not processed (False is returned)
                    unfinished = _upd_db(cr, db_nm) is False
                except Exception as e:
                    # Log the exception
                    _logger.exception('Failed to update database:\n%s', e)
                    # Rollback the transaction
                    cr.rollback()
                    # Break the loop
                    break


if __name__ == "__main__":
    main()

Could you please add this code into a file and try editing it?

jcfernandez-890825 avatar Jun 24 '24 15:06 jcfernandez-890825