5
5
from odoo import fields , models
6
6
7
7
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"
11
12
_order = "start_idx"
12
13
_rec_name = "start_idx"
13
14
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"
16
17
)
17
18
start_idx = fields .Integer ()
18
19
stop_idx = fields .Integer ()
@@ -32,33 +33,31 @@ class PatternChunk(models.Model):
32
33
]
33
34
)
34
35
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 :
46
45
next_chunk = self .get_next_chunk ()
47
46
if next_chunk :
48
- next_chunk .with_delay (priority = priority ).run ()
47
+ next_chunk .with_delay (priority = self . group_id . job_priority ).run ()
49
48
else :
50
49
self .with_delay (priority = 5 ).check_last ()
51
50
else :
52
51
self .with_delay (priority = 5 ).check_last ()
53
52
54
53
def run (self ):
55
- """Process Import of Pattern Chunk """
54
+ """Process Chunk Item in a savepoint """
56
55
cr = self .env .cr
57
56
try :
58
57
self .state = "started"
59
58
cr .commit () # pylint: disable=invalid-commit
60
59
with cr .savepoint ():
61
- self .run_import ()
60
+ self ._run ()
62
61
except Exception as e :
63
62
self .write (
64
63
{
@@ -70,6 +69,7 @@ def run(self):
70
69
self .with_delay ().check_last ()
71
70
return "OK"
72
71
72
+ # TODO move this in pattern-import
73
73
def _prepare_chunk_result (self , res ):
74
74
# TODO rework this part and add specific test case
75
75
nbr_error = len (res ["messages" ])
@@ -98,23 +98,19 @@ def _prepare_chunk_result(self, res):
98
98
}
99
99
100
100
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" )
107
103
)
108
104
109
105
def is_last_job (self ):
110
- return not self .pattern_file_id . chunk_ids .filtered (
106
+ return not self .group_id . item_ids .filtered (
111
107
lambda s : s .state in ("pending" , "started" )
112
108
)
113
109
114
110
def check_last (self ):
115
111
"""Check if all chunk have been processed"""
116
112
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"
119
115
else :
120
116
return "There is still some running chunk"
0 commit comments