Merge lp:~camptocamp/openerp-connector-magento/7.0-next-release-missing-records-1322452 into lp:~openerp-connector-core-editors/openerp-connector-magento/7.0-next-release

Proposed by Guewen Baconnier @ Camptocamp
Status: Merged
Merged at revision: 990
Proposed branch: lp:~camptocamp/openerp-connector-magento/7.0-next-release-missing-records-1322452
Merge into: lp:~openerp-connector-core-editors/openerp-connector-magento/7.0-next-release
Diff against target: 149 lines (+61/-10)
2 files modified
magentoerpconnect/magento_model.py (+44/-9)
magentoerpconnect/sale.py (+17/-1)
To merge this branch: bzr merge lp:~camptocamp/openerp-connector-magento/7.0-next-release-missing-records-1322452
Reviewer Review Type Date Requested Status
OpenERP Connector Core Editors Pending
Review via email: mp+220910@code.launchpad.net

Commit message

Introduce a 30 seconds buffer between 2 batches of imports to avoid to miss records.

The 'created_at' date on Magento is set at the beginning of a transaction, and the transaction may be committed seconds after. The buffer will allow to include these records in the imports.

Description of the change

To post a comment you must log in.
Revision history for this message
David BEAL (ak) (davidbeal) wrote :

Hi Guewen,

You tell line 44:
          # but this is not a big deal because they will be skipped when
45 + # the last `sync_date` is the same

few lines later the condition is not with sync_date but with :

144 + if self.binder.to_openerp(self.magento_id):
145 + return _('Already imported')

sorry to see this only now

good solution i think.

Thanks

PS : "Magento define the create date at the begining of the transaction" : LOL, Magento is a ... app, no word to qualify it.

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

On Mon, May 26, 2014 at 11:55 AM, David BEAL <email address hidden> wrote:
> Hi Guewen,
>
> You tell line 44:
> # but this is not a big deal because they will be skipped when
> 45 + # the last `sync_date` is the same
>
> few lines later the condition is not with sync_date but with :
>
> 144 + if self.binder.to_openerp(self.magento_id):
> 145 + return _('Already imported')

Yeah that's right, the import of sales orders is a special case since
the subsequent imports are just skipped. For all the other records,
the comment applies. I will change this comment in the part regarding
the sales orders.
>
>
> sorry to see this only now
>
> good solution i think.
>
> Thanks
>
>
> PS : "Magento define the create date at the begining of the transaction" : LOL, Magento is a ... app, no word to qualify it.
>
> --
> https://code.launchpad.net/~camptocamp/openerp-connector-magento/7.0-next-release-missing-records-1322452/+merge/220910
> Your team Camptocamp is subscribed to branch lp:~openerp-connector-core-editors/openerp-connector-magento/7.0-next-release.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'magentoerpconnect/magento_model.py'
2--- magentoerpconnect/magento_model.py 2014-05-06 09:33:40 +0000
3+++ magentoerpconnect/magento_model.py 2014-05-26 07:37:58 +0000
4@@ -21,7 +21,7 @@
5 ##############################################################################
6
7 import logging
8-from datetime import datetime
9+from datetime import datetime, timedelta
10 from openerp.osv import fields, orm
11 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
12 import openerp.addons.connector as connector
13@@ -44,6 +44,8 @@
14
15 _logger = logging.getLogger(__name__)
16
17+IMPORT_DELTA_BUFFER = 30 # seconds
18+
19
20 class magento_backend(orm.Model):
21 _name = 'magento.backend'
22@@ -230,7 +232,7 @@
23 ids = [ids]
24 self.check_magento_structure(cr, uid, ids, context=context)
25 session = ConnectorSession(cr, uid, context=context)
26- import_start_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
27+ import_start_time = datetime.now()
28 for backend in self.browse(cr, uid, ids, context=context):
29 from_date = getattr(backend, from_date_field)
30 if from_date:
31@@ -240,8 +242,18 @@
32 from_date = None
33 import_batch.delay(session, model,
34 backend.id, filters={'from_date': from_date})
35- self.write(cr, uid, ids,
36- {from_date_field: import_start_time})
37+ # Records from Magento are imported based on their `created_at`
38+ # date. This date is set on Magento at the beginning of a
39+ # transaction, so if the import is run between the beginning and
40+ # the end of a transaction, the import of a record may be
41+ # missed. That's why we add a small buffer back in time where
42+ # the eventually missed records will be retrieved. This also
43+ # means that we'll have jobs that import twice the same records,
44+ # but this is not a big deal because they will be skipped when
45+ # the last `sync_date` is the same.
46+ next_time = import_start_time - timedelta(seconds=IMPORT_DELTA_BUFFER)
47+ next_time = next_time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
48+ self.write(cr, uid, ids, {from_date_field: next_time}, context=context)
49
50 def import_product_categories(self, cr, uid, ids, context=None):
51 self._import_from_date(cr, uid, ids, 'magento.product.category',
52@@ -344,7 +356,7 @@
53 if not hasattr(ids, '__iter__'):
54 ids = [ids]
55 session = ConnectorSession(cr, uid, context=context)
56- import_start_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
57+ import_start_time = datetime.now()
58 for website in self.browse(cr, uid, ids, context=context):
59 backend_id = website.backend_id.id
60 if website.import_partners_from_date:
61@@ -357,8 +369,19 @@
62 session, 'magento.res.partner', backend_id,
63 {'magento_website_id': website.magento_id,
64 'from_date': from_date})
65- self.write(cr, uid, ids,
66- {'import_partners_from_date': import_start_time})
67+ # Records from Magento are imported based on their `created_at`
68+ # date. This date is set on Magento at the beginning of a
69+ # transaction, so if the import is run between the beginning and
70+ # the end of a transaction, the import of a record may be
71+ # missed. That's why we add a small buffer back in time where
72+ # the eventually missed records will be retrieved. This also
73+ # means that we'll have jobs that import twice the same records,
74+ # but this is not a big deal because they will be skipped when
75+ # the last `sync_date` is the same.
76+ next_time = import_start_time - timedelta(seconds=IMPORT_DELTA_BUFFER)
77+ next_time = next_time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
78+ self.write(cr, uid, ids, {'import_partners_from_date': next_time},
79+ context=context)
80 return True
81
82
83@@ -493,7 +516,7 @@
84
85 def import_sale_orders(self, cr, uid, ids, context=None):
86 session = ConnectorSession(cr, uid, context=context)
87- import_start_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
88+ import_start_time = datetime.now()
89 for storeview in self.browse(cr, uid, ids, context=context):
90 backend_id = storeview.backend_id.id
91 if storeview.import_orders_from_date:
92@@ -509,7 +532,19 @@
93 {'magento_storeview_id': storeview.magento_id,
94 'from_date': from_date},
95 priority=1) # executed as soon as possible
96- self.write(cr, uid, ids, {'import_orders_from_date': import_start_time})
97+ # Records from Magento are imported based on their `created_at`
98+ # date. This date is set on Magento at the beginning of a
99+ # transaction, so if the import is run between the beginning and
100+ # the end of a transaction, the import of a record may be
101+ # missed. That's why we add a small buffer back in time where
102+ # the eventually missed records will be retrieved. This also
103+ # means that we'll have jobs that import twice the same records,
104+ # but this is not a big deal because they will be skipped when
105+ # the last `sync_date` is the same.
106+ next_time = import_start_time - timedelta(seconds=IMPORT_DELTA_BUFFER)
107+ next_time = next_time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
108+ self.write(cr, uid, ids, {'import_orders_from_date': next_time},
109+ context=context)
110 return True
111
112
113
114=== modified file 'magentoerpconnect/sale.py'
115--- magentoerpconnect/sale.py 2014-05-06 08:36:26 +0000
116+++ magentoerpconnect/sale.py 2014-05-26 07:37:58 +0000
117@@ -22,8 +22,9 @@
118 import logging
119 import xmlrpclib
120 from datetime import datetime, timedelta
121+import openerp.addons.decimal_precision as dp
122 from openerp.osv import fields, orm
123-import openerp.addons.decimal_precision as dp
124+from openerp.tools.translate import _
125 from openerp.addons.connector.connector import ConnectorUnit
126 from openerp.addons.connector.exception import (NothingToDoJob,
127 FailedJobError,
128@@ -372,6 +373,21 @@
129 self._mapper = self.environment.get_connector_unit(SaleOrderImportMapper)
130 return self._mapper
131
132+ def _must_skip(self):
133+ """ Hook called right after we read the data from the backend.
134+
135+ If the method returns a message giving a reason for the
136+ skipping, the import will be interrupted and the message
137+ recorded in the job (if the import is called directly by the
138+ job, not by dependencies).
139+
140+ If it returns None, the import will continue normally.
141+
142+ :returns: None | str | unicode
143+ """
144+ if self.binder.to_openerp(self.magento_id):
145+ return _('Already imported')
146+
147 def _clean_magento_items(self, resource):
148 """
149 Method that clean the sale order line given by magento before importing it

Subscribers

People subscribed via source and target branches

to all changes: