Skip to content

Commit

Permalink
run-cache: don't save always_changed stages
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Oct 14, 2020
1 parent 09e51e6 commit 9d6cc09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dvc/stage/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tempfile
from contextlib import contextmanager

from funcy import first
from funcy import cached_property, first
from voluptuous import Invalid

from dvc.cache.local import _log_exceptions
Expand Down Expand Up @@ -48,7 +48,10 @@ def _get_stage_hash(stage):
class StageCache:
def __init__(self, repo):
self.repo = repo
self.cache_dir = os.path.join(repo.cache.local.cache_dir, "runs")

@cached_property
def cache_dir(self):
return os.path.join(self.repo.cache.local.cache_dir, "runs")

@property
def tree(self):
Expand Down Expand Up @@ -129,6 +132,9 @@ def _uncached_outs(self, stage, cache):
yield out

def save(self, stage):
if stage.is_callback or stage.always_changed:
return

cache_key = _get_stage_hash(stage)
if not cache_key:
return
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/stage/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import pytest


def test_stage_cache(tmp_dir, dvc, mocker):
tmp_dir.gen("dep", "dep")
Expand Down Expand Up @@ -204,3 +206,19 @@ def _mode(path):
assert _mode(parent_cache_dir) == dir_mode
assert _mode(cache_dir) == dir_mode
assert _mode(cache_file) == file_mode


def test_always_changed(mocker):
from dvc.repo import Repo
from dvc.stage import Stage
from dvc.stage.cache import RunCacheNotFoundError, StageCache

repo = mocker.Mock(spec=Repo)
cache = StageCache(repo)
stage = Stage(repo, always_changed=True)
get_stage_hash = mocker.patch("dvc.stage.cache._get_stage_hash")
assert cache.save(stage) is None
assert get_stage_hash.not_called
with pytest.raises(RunCacheNotFoundError):
cache.restore(stage)
assert get_stage_hash.not_called

0 comments on commit 9d6cc09

Please sign in to comment.