xxxxxxxxxx
1
@api.model
2
def sync_wechat(self, account_id):
3
"""
4
同步执行改为异步执行
5
:param account_id:
6
:return:
7
"""
8
t = threading.Thread(target=self._sync_wechat, args=(account_id, ))
9
t.start()
10
11
@api.model
12
def _sync_wechat(self, account_id):
13
'''
14
同步企业微信部门帐号信息
15
:param account_id:
16
:return:
17
'''
18
# 由于主线程env已销毁,创建新env
19
with api.Environment.manage():
20
with self.pool.cursor() as new_cr:
21
self = self.with_env(self.env(cr=new_cr))
22
# 根据传入的account_id去查询对应的cropId和secretKey
23
start_time = datetime.datetime.now()
24
account = self.browse([account_id])
25
client = account.get_contact_client(account)
26
log_model = self.env['cdtct.wechat.log']
27
try:
28
# 同步部门信息
29
self.env['cdtct.wechat.department'].sync_wechat_department(account, client)
30
# 同步用户信息
31
self.env['cdtct.wechat.user'].sync_wechat_users(account, client)
32
except WeChatClientException as e:
33
log_model.log_info(u'同步失败', '错误信息:{}'.format(str(e)))
34
except Exception as e:
35
log_model.log_info(u'同步失败', '错误信息:{}'.format(str(e)))
36
37
end_time = datetime.datetime.now()
38
self.env['cdtct.wechat.log']\
39
.log_info(u'同步成功, 用时{use_time}'
40
.format(use_time=(end_time-start_time).total_seconds()))
已复制