Skip to content

Commit

Permalink
Prevent unnecessary cache invalidation
Browse files Browse the repository at this point in the history
We use 2 scheduled actions with a high frequency:
- check conversion queue (every 2 minutes)
- unlock folders (every 10 minutes)

The issue is that the job processing triggers a cache invalidation which
impacts performances.

The conversion queue is now dynamically activated when we run a
converter. It is automatically deactivated once a day if no converter is
running.

The folder unlocking is now executed every 3 hours. Indeed, the 'Unlock'
button is now made more visible, so it can be done maunally. We still
keep the cron to avoid the library not being scanned for ages.
  • Loading branch information
DocMarty84 committed Aug 16, 2020
1 parent 1eb42bf commit 18ec98f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
16 changes: 15 additions & 1 deletion data/oomusic_converter_data.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Cron to build image cache -->
<!-- Cron to launch conversion -->
<record id="oomusic_convert" model="ir.cron">
<field name="name">oomusic.convert</field>
<field name="active" eval="True"/>
Expand All @@ -14,5 +14,19 @@
<field name="state">code</field>
<field name="code">model.cron_convert()</field>
</record>

<!-- Cron to deactivate above cron -->
<record id="oomusic_convert_toggle" model="ir.cron">
<field name="name">oomusic.convert.toggle</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
<field name="model_id" ref="oomusic.model_oomusic_converter"/>
<field name="state">code</field>
<field name="code">model.cron_toggle()</field>
</record>
</data>
</odoo>
4 changes: 2 additions & 2 deletions data/oomusic_folder_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<field name="name">oomusic.unlock.folder</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">10</field>
<field name="interval_type">minutes</field>
<field name="interval_number">3</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
<field name="model_id" ref="oomusic.model_oomusic_folder"/>
Expand Down
14 changes: 14 additions & 0 deletions models/oomusic_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def action_run(self):
{"state": "waiting"}
)

# Activate the cron if necessary
cron = self.env.ref("oomusic.oomusic_convert", raise_if_not_found=False)
if cron and not cron.active:
cron.try_write({"active": True})

def action_draft(self):
self.write({"state": "draft"})
self.env["oomusic.converter.line"].search([("converter_id", "in", self.ids)]).write(
Expand All @@ -210,6 +215,15 @@ def action_convert(self):
def cron_convert(self):
self.search([("state", "=", "running")]).action_convert()

def cron_toggle(self):
# Deactivate the cron automatically if there is no job running. This prevents unnecessary
# cache clearing.
if not self.sudo().search([("state", "=", "running")]):
cron = self.env.ref("oomusic.oomusic_convert", raise_if_not_found=False)
if cron and cron.active:
cron.try_write({"active": False})
return


class MusicConverterLine(models.Model):
_name = "oomusic.converter.line"
Expand Down

0 comments on commit 18ec98f

Please sign in to comment.