|
8 | 8 | import math
|
9 | 9 | import os
|
10 | 10 | import threading
|
11 |
| -import time |
12 | 11 | from logging import getLogger
|
13 | 12 | from multiprocessing.pool import ThreadPool
|
14 | 13 | from typing import Any
|
|
22 | 21 |
|
23 | 22 | import app.services.output_manager.message_handler as mhandler
|
24 | 23 | from app.configs.app_config import AppConfig
|
25 |
| -from app.configs.config import ConfigClass |
26 | 24 | from app.configs.user_config import UserConfig
|
27 | 25 | from app.models.upload_form import generate_on_success_form
|
28 | 26 | from app.services.clients.base_auth_client import BaseAuthClient
|
|
31 | 29 | from app.services.output_manager.error_handler import ECustomizedError
|
32 | 30 | from app.services.output_manager.error_handler import SrvErrorHandler
|
33 | 31 | from app.services.user_authentication.decorator import require_valid_token
|
34 |
| -from app.services.user_authentication.token_manager import SrvTokenManager |
35 | 32 | from app.utils.aggregated import get_file_info_by_geid
|
36 | 33 |
|
37 | 34 | from .exception import INVALID_CHUNK_ETAG
|
@@ -92,7 +89,6 @@ def __init__(
|
92 | 89 | # for tracking the multi-threading chunk upload
|
93 | 90 | self.active_jobs = 0
|
94 | 91 | self.lock = threading.Lock()
|
95 |
| - self.chunk_upload_done = threading.Event() |
96 | 92 |
|
97 | 93 | def generate_meta(self, local_path: str) -> Tuple[int, int]:
|
98 | 94 | """
|
@@ -309,14 +305,15 @@ def stream_upload(self, file_object: FileObject, pool: ThreadPool) -> None:
|
309 | 305 | been uploaded.
|
310 | 306 | """
|
311 | 307 | count = 0
|
312 |
| - semaphore = threading.Semaphore(AppConfig.Env.num_of_jobs) |
| 308 | + semaphore = threading.Semaphore(pool._processes + 1) |
| 309 | + chunk_upload_done = threading.Event() |
313 | 310 |
|
314 | 311 | def on_complete(result):
|
315 | 312 | semaphore.release()
|
316 | 313 | with self.lock:
|
317 | 314 | self.active_jobs -= 1
|
318 | 315 | if self.active_jobs == 0:
|
319 |
| - self.chunk_upload_done.set() |
| 316 | + chunk_upload_done.set() |
320 | 317 |
|
321 | 318 | # process on the file content
|
322 | 319 | f = open(file_object.local_path, 'rb')
|
@@ -355,8 +352,14 @@ def on_complete(result):
|
355 | 352 |
|
356 | 353 | count += 1
|
357 | 354 |
|
| 355 | + # for resumable check ONLY if user resume the upload at 100% |
| 356 | + # just check if there is any active job, if not, set the event |
| 357 | + while not chunk_upload_done.wait(timeout=60): |
| 358 | + if self.active_jobs == 0: |
| 359 | + chunk_upload_done.set() |
| 360 | + logger.warning('Waiting for all the chunks to be uploaded, remaining jobs: %s', file_object.progress) |
| 361 | + |
358 | 362 | f.close()
|
359 |
| - self.chunk_upload_done.wait() |
360 | 363 |
|
361 | 364 | def upload_chunk(self, file_object: FileObject, chunk_number: int, chunk: str, etag: str, chunk_size: int) -> None:
|
362 | 365 | """
|
@@ -466,16 +469,3 @@ def check_status(self, file_object: FileObject) -> bool:
|
466 | 469 |
|
467 | 470 | def set_finish_upload(self):
|
468 | 471 | self.finish_upload = True
|
469 |
| - |
470 |
| - def upload_token_refresh(self, azp: str = ConfigClass.keycloak_device_client_id): |
471 |
| - token_manager = SrvTokenManager() |
472 |
| - DEFAULT_INTERVAL = 2 # seconds to check if the upload is finished |
473 |
| - total_count = 0 # when total_count equals token_refresh_interval, refresh token |
474 |
| - while self.finish_upload is not True: |
475 |
| - if total_count >= AppConfig.Env.token_refresh_interval: |
476 |
| - token_manager.refresh(azp) |
477 |
| - total_count = 0 |
478 |
| - |
479 |
| - # if not then sleep for DEFAULT_INTERVAL seconds |
480 |
| - time.sleep(DEFAULT_INTERVAL) |
481 |
| - total_count = total_count + DEFAULT_INTERVAL |
0 commit comments