odoo-ide / pycharm-odoo

PyCharm plugin for Odoo
https://plugins.jetbrains.com/plugin/13499-odoo
Other
41 stars 5 forks source link

High CPU usage on Pycharm when #475

Open jcfernandez-890825 opened 1 week ago

jcfernandez-890825 commented 1 week ago

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?

trinhanhngoc commented 1 week ago

Hi @jcfernandez-890825 ,

I just tested your code on Macbook Pro M1 16GB. Everything is working fine without any performance issues.

jcfernandez-890825 commented 1 week ago

Hi @trinhanhngoc

Mmmm... this is weird ...

As soon as I disable the plugin the CPU usage goes away, maybe there is a problem when the Odoo plugin interacts with another plugin ior feature? Is there a inspection/analysis setting thing that might be causing this?

trinhanhngoc commented 1 week ago

@jcfernandez-890825 ,

I need more information: PyCharm version? Odoo version? Other plugins?

jcfernandez-890825 commented 6 days ago

@trinhanhngoc

Here is my info:

PyCharm 2024.1.4 (Professional Edition)
Build #PY-241.18034.82, built on June 24, 2024
Licensed to Juan Carlos Fernández
Subscription is active until November 13, 2024.
Runtime version: 17.0.11+1-b1207.24 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.15.0-107-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 10240M
Cores: 16
Non-Bundled Plugins:
  com.jetbrains.space (241.18034.4)
  Statistic (4.2.14)
  dev.meanmail.plugin.nginx-intellij-plugin (2024.3)
  XPathView (241.15989.65)
  net.seesharpsoft.intellij.plugins.csv (3.3.0-241)
  com.github.copilot (1.5.11.5872)
  dev.ngocta.pycharm-odoo (2024.5.1.241)
  com.intellij.bigdatatools.core (241.18034.62)
  com.intellij.bigdatatools.binary.files (241.14494.158)
  com.intellij.bigdatatools.rfs (241.18034.62)
  zielu.gittoolbox (500.2.10+233)
Current Desktop: X-Cinnamon
jcfernandez-890825 commented 5 days ago

@trinhanhngoc

Since this is not a module, but just a python package with DevOps scripts. image

Maybe is indexing constantly all modules because there is no manifest?

jcfernandez-890825 commented 5 days ago

@trinhanhngoc

I just tested your code on Macbook Pro M1 16GB. Everything is working fine without any performance issues.

When you did this did you added the code inside a module folder? When I add a manifest file to xgo_dev_ops. The problem stops.

trinhanhngoc commented 5 days ago

@jcfernandez-890825 ,

No, I created a temp file in the root of the Odoo source code.

image