file_client.py 2.32 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

16
17
18
import logging
logger = logging.getLogger(__name__)

19
from agkyra.syncer import common, messaging
20
21
22
23
24
25
26


class FileClient(object):

    def list_candidate_files(self, archive):
        raise NotImplementedError

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
27
    def start_probing_file(self, objname, old_state, ref_state, callback=None):
28
29
30
31
32
33
34
35
36
        raise NotImplementedError

    def stage_file(self, source_state):
        raise NotImplementedError

    def prepare_target(self, state):
        raise NotImplementedError

    def start_pulling_file(self, source_handle, target_state, sync_state,
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
37
                           callback=None, failure_callback=None):
38
39
40
41
42
43
        try:
            synced_source_state, synced_target_state = \
                self._start(source_handle, target_state, sync_state)
            if callback is not None:
                callback(synced_source_state, synced_target_state)
        except common.SyncError as e:
44
45
46
            hard = isinstance(e, common.HardSyncError)
            if failure_callback is not None:
                failure_callback(source_handle.source_state, hard=hard)
47
48
49
50
51
            msg = messaging.SyncErrorMessage(
                objname=target_state.objname,
                serial=source_handle.source_state.serial,
                exception=e, logger=logger)
            self.settings.messager.put(msg)
52
53
54
55
56
57
58
59
60

    def _start(self, source_handle, target_state, sync_state):
        try:
            target_handle = self.prepare_target(target_state)
            synced_target_state = target_handle.pull(source_handle, sync_state)
            synced_source_state = source_handle.get_synced_state()
            return synced_source_state, synced_target_state
        finally:
            source_handle.unstage_file()