55from odoo import fields , models
66
77
8- class PatternChunk (models .Model ):
9- _name = "pattern.chunk"
10- _description = "Pattern Chunk"
8+ class ChunkItem (models .Model ):
9+ _inherit = "collection.base"
10+ _name = "chunk.item"
11+ _description = "Chunk Item"
1112 _order = "start_idx"
1213 _rec_name = "start_idx"
1314
14- pattern_file_id = fields .Many2one (
15- "pattern.file " , "Pattern File " , required = True , ondelete = "cascade"
15+ group_id = fields .Many2one (
16+ "chunk.group " , "Chunk Group " , required = True , ondelete = "cascade"
1617 )
1718 start_idx = fields .Integer ()
1819 stop_idx = fields .Integer ()
@@ -32,33 +33,31 @@ class PatternChunk(models.Model):
3233 ]
3334 )
3435
35- def run_import (self ):
36- model = self .pattern_file_id .pattern_config_id .model_id .model
37- res = (
38- self .with_context (pattern_config = {"model" : model , "record_ids" : []})
39- .env [model ]
40- .load ([], self .data )
41- )
42- self .write (self ._prepare_chunk_result (res ))
43- config = self .pattern_file_id .pattern_config_id
44- priority = config .job_priority
45- if not config .process_multi :
36+ def manual_run (self ):
37+ """ Run the import without try/except, easier for debug """
38+ return self ._run ()
39+
40+ def _run (self ):
41+ with self .work_on (self .group_id .apply_on_model ) as work :
42+ processor = work .component (usage = self .group_id .usage )
43+ processor .run ()
44+ if not self .group_id .process_multi :
4645 next_chunk = self .get_next_chunk ()
4746 if next_chunk :
48- next_chunk .with_delay (priority = priority ).run ()
47+ next_chunk .with_delay (priority = self . group_id . job_priority ).run ()
4948 else :
5049 self .with_delay (priority = 5 ).check_last ()
5150 else :
5251 self .with_delay (priority = 5 ).check_last ()
5352
5453 def run (self ):
55- """Process Import of Pattern Chunk """
54+ """Process Chunk Item in a savepoint """
5655 cr = self .env .cr
5756 try :
5857 self .state = "started"
5958 cr .commit () # pylint: disable=invalid-commit
6059 with cr .savepoint ():
61- self .run_import ()
60+ self ._run ()
6261 except Exception as e :
6362 self .write (
6463 {
@@ -70,6 +69,7 @@ def run(self):
7069 self .with_delay ().check_last ()
7170 return "OK"
7271
72+ # TODO move this in pattern-import
7373 def _prepare_chunk_result (self , res ):
7474 # TODO rework this part and add specific test case
7575 nbr_error = len (res ["messages" ])
@@ -98,23 +98,19 @@ def _prepare_chunk_result(self, res):
9898 }
9999
100100 def get_next_chunk (self ):
101- return self .search (
102- [
103- ("pattern_file_id" , "=" , self .pattern_file_id .id ),
104- ("state" , "=" , "pending" ),
105- ],
106- limit = 1 ,
101+ return fields .first (
102+ self .group_id .item_ids .filtered (lambda s : s .state == "pending" )
107103 )
108104
109105 def is_last_job (self ):
110- return not self .pattern_file_id . chunk_ids .filtered (
106+ return not self .group_id . item_ids .filtered (
111107 lambda s : s .state in ("pending" , "started" )
112108 )
113109
114110 def check_last (self ):
115111 """Check if all chunk have been processed"""
116112 if self .is_last_job ():
117- self .pattern_file_id . set_import_done ()
118- return "Pattern file is done"
113+ self .group_id . set_done ()
114+ return "Chunk group is done"
119115 else :
120116 return "There is still some running chunk"
0 commit comments