5
5
import asyncio
6
6
from typing import List
7
7
from abc import ABC , abstractmethod
8
- import queue
8
+ from datetime import datetime
9
9
10
10
from requests .models import HTTPError
11
11
@@ -110,19 +110,25 @@ def __init__(self,object:SalesforceObject,connection:BulkAPIConnection) -> None:
110
110
self .object = object
111
111
self .id = None
112
112
113
- def status (self ):
114
- req = requests .get (f"{ self .connection .instance_url } /services/data/{ self .connection .settings .api_version } /jobs/query/{ self .id } " ,headers = self .connection .headers )
113
+ async def status (self ):
114
+ loop = asyncio .get_event_loop ()
115
+ f3 = loop .run_in_executor (None ,lambda :requests .get (f"{ self .connection .instance_url } /services/data/{ self .connection .settings .api_version } /jobs/query/{ self .id } " ,headers = self .connection .headers ))
116
+ req = await f3
115
117
req .raise_for_status ()
116
118
return req .json ()['state' ]
117
119
118
120
119
121
async def start (self ):
120
122
print (f'Starting job for { self .object .name } ' )
121
- req = requests .post (
123
+
124
+ loop = asyncio .get_event_loop ()
125
+ f1 = loop .run_in_executor (None ,lambda : requests .post (
122
126
f"{ self .connection .instance_url } /services/data/{ self .connection .settings .api_version } /jobs/query" ,
123
127
data = json .dumps (self .body ),
124
128
headers = self .connection .headers
125
- )
129
+ ))
130
+ req = await f1
131
+
126
132
try :
127
133
req .raise_for_status ()
128
134
except HTTPError as e :
@@ -136,11 +142,21 @@ async def start(self):
136
142
raise
137
143
138
144
self .id = req .json ()['id' ]
145
+ delay_iter = iter ([1 ,1 ,10 ,30 ,60 ])
146
+
139
147
while True :
140
- await asyncio .sleep (1 )
141
- status = self .status ()
148
+ try :
149
+ delay = next (delay_iter )
150
+ except StopIteration :
151
+ delay = delay
152
+
153
+ await asyncio .sleep (delay )
154
+
155
+ status = await self .status ()
156
+ print (f'{ self .object .name } : { status } ' )
157
+
142
158
if status == 'JobComplete' :
143
- self .on_complete (f"{ self .connection .instance_url } /services/data/{ self .connection .settings .api_version } /jobs/query/{ self .id } /results" ,self )
159
+ await self .on_complete (f"{ self .connection .instance_url } /services/data/{ self .connection .settings .api_version } /jobs/query/{ self .id } /results" ,self )
144
160
print (f'Finished job for { self .object .name } ' )
145
161
break
146
162
return 0
@@ -187,13 +203,18 @@ def __init__(self,result_url:str,job:BulkAPIJob) -> None:
187
203
self .batch_number = 0
188
204
self .job = job
189
205
190
- def fetch (self ):
191
- result = requests .get (f"{ self .result_url } ?maxRecords=10000" ,headers = self .job .connection .headers )
206
+ async def fetch (self ):
207
+ loop = asyncio .get_event_loop ()
208
+ f1 = loop .run_in_executor (None ,lambda : requests .get (f"{ self .result_url } ?maxRecords=50000" ,headers = self .job .connection .headers ))
209
+
210
+ self .datetime_start_fetch = datetime .now ()
211
+ result = await f1
192
212
self .handle (result )
193
213
while 'sforce-locator' in result .headers .keys ():
194
214
if (result .headers ['sforce-locator' ]!= 'NA' ) & (result .headers ['sforce-locator' ]!= 'null' ):
195
215
self .batch_number += 1
196
- result = requests .get (f"{ self .result_url } ?locator={ result .headers ['sforce-locator' ]} &maxRecords=10000" ,headers = self .job .connection .headers )
216
+ f2 = loop .run_in_executor (None ,lambda : requests .get (f"{ self .result_url } ?locator={ result .headers ['sforce-locator' ]} &maxRecords=50000" ,headers = self .job .connection .headers ))
217
+ result = await f2
197
218
self .handle (result )
198
219
else :
199
220
break
@@ -205,10 +226,10 @@ def handle(self,data):
205
226
206
227
class JobCompleteEvent (List [BulkAPIResultHandler ]):
207
228
208
- def __call__ (self , * args , ** kwargs ):
229
+ async def __call__ (self , * args , ** kwargs ):
209
230
for c in self :
210
231
i = c (* args ,* kwargs )
211
- i .fetch ()
232
+ await i .fetch ()
212
233
213
234
def __repr__ (self ):
214
235
return "Event(%s)" % list .__repr__ (self )
@@ -230,5 +251,4 @@ async def run_all(self):
230
251
)
231
252
else :
232
253
break
233
-
234
-
254
+
0 commit comments