|
6 | 6 | # Copyright © 2010-2012 Matteo Boscariol <[email protected]>
|
7 | 7 | # Copyright © 2013-2015 Luca Wehrstedt <[email protected]>
|
8 | 8 | # Copyright © 2016 Luca Versari <[email protected]>
|
| 9 | +# Copyright © 2021 Fabian Gundlach <[email protected]> |
9 | 10 | #
|
10 | 11 | # This program is free software: you can redistribute it and/or modify
|
11 | 12 | # it under the terms of the GNU Affero General Public License as
|
@@ -70,24 +71,35 @@ def precache_files(self, contest_id):
|
70 | 71 | contest_id (int): the id of the contest
|
71 | 72 |
|
72 | 73 | """
|
73 |
| - # In order to avoid a long-living connection, first fetch the |
74 |
| - # complete list of files and then download the files; since |
75 |
| - # this is just pre-caching, possible race conditions are not |
76 |
| - # dangerous |
77 |
| - logger.info("Precaching files for contest %d.", contest_id) |
78 |
| - with SessionGen() as session: |
79 |
| - contest = Contest.get_from_id(contest_id, session) |
80 |
| - files = enumerate_files(session, contest, skip_submissions=True, |
81 |
| - skip_user_tests=True, skip_print_jobs=True) |
82 |
| - for digest in files: |
83 |
| - try: |
84 |
| - self.file_cacher.cache_file(digest) |
85 |
| - except KeyError: |
86 |
| - # No problem (at this stage) if we cannot find the |
87 |
| - # file |
88 |
| - pass |
89 |
| - |
90 |
| - logger.info("Precaching finished.") |
| 74 | + lock = self.file_cacher.precache_lock() |
| 75 | + if lock is None: |
| 76 | + # Another worker is already precaching. Hence, this worker doesn't |
| 77 | + # need to do anything. |
| 78 | + logger.info("Another worker is already precaching files for " |
| 79 | + "contest %d.", contest_id) |
| 80 | + return |
| 81 | + with lock: |
| 82 | + # In order to avoid a long-living connection, first fetch the |
| 83 | + # complete list of files and then download the files; since |
| 84 | + # this is just pre-caching, possible race conditions are not |
| 85 | + # dangerous |
| 86 | + logger.info("Precaching files for contest %d.", contest_id) |
| 87 | + with SessionGen() as session: |
| 88 | + contest = Contest.get_from_id(contest_id, session) |
| 89 | + files = enumerate_files(session, |
| 90 | + contest, |
| 91 | + skip_submissions=True, |
| 92 | + skip_user_tests=True, |
| 93 | + skip_print_jobs=True) |
| 94 | + for digest in files: |
| 95 | + try: |
| 96 | + self.file_cacher.cache_file(digest) |
| 97 | + except KeyError: |
| 98 | + # No problem (at this stage) if we cannot find the |
| 99 | + # file |
| 100 | + pass |
| 101 | + |
| 102 | + logger.info("Precaching finished.") |
91 | 103 |
|
92 | 104 | @rpc_method
|
93 | 105 | def execute_job_group(self, job_group_dict):
|
|
0 commit comments