|
19 | 19 |
|
20 | 20 | import base64 |
21 | 21 | import collections |
| 22 | +import logging |
22 | 23 | import os |
23 | 24 | import tempfile |
24 | 25 | from urllib.parse import quote |
|
29 | 30 | from apache_beam.io import filesystems |
30 | 31 | from apache_beam.io import textio |
31 | 32 | from apache_beam.io import tfrecordio |
| 33 | +from apache_beam.options.pipeline_options import PipelineOptions |
32 | 34 | from apache_beam.testing import test_stream |
33 | 35 | from apache_beam.transforms import combiners |
34 | 36 |
|
| 37 | +_LOGGER = logging.getLogger(__name__) |
| 38 | + |
35 | 39 |
|
36 | 40 | class CacheManager(object): |
37 | 41 | """Abstract class for caching PCollections. |
@@ -286,13 +290,17 @@ def raw_source(self, *labels): |
286 | 290 | return self._reader_class(self._glob_path(*labels)) |
287 | 291 |
|
288 | 292 | def cleanup(self): |
289 | | - if self._cache_dir.startswith('gs://'): |
290 | | - from apache_beam.io.gcp import gcsfilesystem |
291 | | - from apache_beam.options.pipeline_options import PipelineOptions |
292 | | - fs = gcsfilesystem.GCSFileSystem(PipelineOptions()) |
293 | | - fs.delete([self._cache_dir + '/full/']) |
294 | | - elif filesystems.FileSystems.exists(self._cache_dir): |
295 | | - filesystems.FileSystems.delete([self._cache_dir]) |
| 293 | + try: |
| 294 | + if self._cache_dir.startswith('gs://'): |
| 295 | + # Import GCP dependencies only when needed. |
| 296 | + from apache_beam.io.gcp import gcsfilesystem |
| 297 | + fs = gcsfilesystem.GCSFileSystem(PipelineOptions()) |
| 298 | + fs.delete([self._cache_dir + '/full/']) |
| 299 | + elif filesystems.FileSystems.exists(self._cache_dir): |
| 300 | + filesystems.FileSystems.delete([self._cache_dir]) |
| 301 | + except Exception as e: |
| 302 | + _LOGGER.warning( |
| 303 | + 'Failed to clean up cache directory %s: %s', self._cache_dir, e) |
296 | 304 | self._saved_pcoders = {} |
297 | 305 |
|
298 | 306 | def _glob_path(self, *labels): |
|
0 commit comments