-
Notifications
You must be signed in to change notification settings - Fork 0
add functions to ali_sync.py and modify ali_sync.yaml #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
ee8744f
9d032c1
1a84c1b
ba88f5b
1fcd1ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| from botocore.client import Config | ||
|
|
||
| from pykit import jobq | ||
| from pykit import http | ||
| from pykit import awssign | ||
|
|
||
| report_state_lock = threading.RLock() | ||
|
|
||
|
|
@@ -341,6 +343,52 @@ def compare_file(result, th_status): | |
| return True | ||
|
|
||
|
|
||
| def upload_file(resp_object, result): | ||
|
||
| uri = '/%s/%s' % (cnf['BAISHAN_BUCKET_NAME'], result['s3_key']) | ||
| verb = 'PUT' | ||
|
|
||
| endpoint = cnf['BAISHAN_ENDPOINT'] | ||
| if 'http://' in endpoint: | ||
| Host = endpoint[len('http://'):] | ||
| else: | ||
| Host = endpoint[len('https://'):] | ||
|
|
||
| headers = { | ||
| 'Content-Length': resp_object.content_length, | ||
| 'Host': Host, | ||
| } | ||
|
|
||
| request = { | ||
| 'verb': verb, | ||
| 'uri': uri, | ||
| 'headers': headers, | ||
| } | ||
|
|
||
| sign = awssign.Signer(cnf['BAISHAN_ACCESS_KEEY'], cnf['BAISHAN_SECRET_KEY']) | ||
| sign.add_auth(request, query_auth=False, expires=120) | ||
|
|
||
| cli = http.Client(Host, port=80) | ||
| cli.send_request(request['uri'], verb, request['headers']) | ||
|
|
||
| send_size = 0 | ||
| while True: | ||
| start_time = time.time() | ||
|
||
| buf = resp_object.read(1024 * 1024) | ||
| end_time = time.time() | ||
| expect_time = send_size / cnf['SYNC_SPEED'] | ||
|
||
| act_time = end_time - start_time | ||
| time_diff = expect_time - act_time | ||
|
|
||
| if time_diff > 0: | ||
| time.sleep(time_diff) | ||
|
|
||
| cli.send_body(buf) | ||
| send_size += 1024 * 1024 | ||
|
|
||
| if buf == '': | ||
|
||
| break | ||
|
|
||
|
|
||
| def pipe_file(result, th_status): | ||
| result['piped'] = True | ||
| th_status['piped_n'] = th_status.get('piped_n', 0) + 1 | ||
|
|
@@ -349,13 +397,14 @@ def update_pipe_progress(done_bytes, total_bytes): | |
| th_status['pipe_progress'] = (done_bytes, total_bytes) | ||
|
|
||
| file_object = result['file_object'] | ||
| upload_file(file_object, result) | ||
|
||
|
|
||
| try: | ||
| resp_object = oss2_bucket.get_object( | ||
| file_object.key, progress_callback=update_pipe_progress) | ||
|
|
||
| ali_file_info = validate_and_extract_ali_file_info(resp_object, result) | ||
| if ali_file_info == None: | ||
| if ali_file_info is None: | ||
|
|
||
| result['pipe_failed'] = True | ||
| th_status['pipe_failed_n'] = th_status.get('pipe_failed_n', 0) + 1 | ||
|
|
@@ -498,7 +547,7 @@ def update_sync_stat(result): | |
| elif 'default_not_override' in result: | ||
| ali_sync_state['default_not_override'] += 1 | ||
|
|
||
| if not 'piped' in result: | ||
| if 'piped' not in result: | ||
| return | ||
|
|
||
| ali_sync_state['piped'] += 1 | ||
|
|
@@ -528,7 +577,7 @@ def update_sync_stat(result): | |
| ali_sync_state['pipe_succeed'] += 1 | ||
| ali_sync_state['pipe_succeed_bytes'] += file_object.size | ||
|
|
||
| if not 'compared' in result: | ||
| if 'compared' not in result: | ||
| return | ||
|
|
||
| if 'compare_failed' in result: | ||
|
|
@@ -697,7 +746,7 @@ def sync(): | |
| try: | ||
| report_sess = {'stop': False} | ||
| report_th = _thread(report, (report_sess,)) | ||
| jobq.run(iter_files(), [(sync_one_file, 3), | ||
| jobq.run(iter_files(), [(sync_one_file, cnf['THREADS_NUM_FOR_SYNC']), | ||
| (update_sync_stat, 1), | ||
| ]) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没看到在哪使用了这个配置