コード例 #1
0
 def _send_heartbeat_loop(self):
     if self._status.get_status() != self._status.RUNNING:
         log.warn('control service will stop. stop sending heartbeat')
         return
     hostinfo = None
     if platforms.is_linux():
         hostinfo = cuphb.LinuxHost(str(self._agent_ipport), True,
                                    self._confdict['control']['interface'])
     elif platforms.is_mac():
         hostinfo = cuphb.MacHost(str(self._agent_ipport), True,
                                  self._confdict['control']['interface'])
     log.info('to create msg and send msg')
     netmsg = msg.CNetMsg(is_postmsg=True)
     netmsg.set_from_addr(self._agent_ipport, (1, 1))
     netmsg.set_to_addr(self._master_ipport, (1, 1))
     netmsg.set_flag(1)
     netmsg.set_msg_type(self._type_man.getnumber_bytype('HEART_BEAT'))
     netmsg.set_uniq_id(1)
     netmsg.set_body(hostinfo.serilize())
     self.post_msg(netmsg)
     log.info('finish queue sending heartbeat to {0}'.format(
         self._master_ipport))
     self._executor.delay_exec(
         int(self._confdict['control']['heartbeat_interval']) - 3,
         self._send_heartbeat_loop,
         urgency=executor.URGENCY_HIGH)
コード例 #2
0
    def lock(self, blocking=True):
        """
        lock the file

        :param blocking:
            If blocking is True, will block there until cup gets the lock.
            True by default.

        :return:
            return False if locking fails

        :raise Exception:
            raise cup.err.LockFileError if blocking is False and
            the lock action failed
        """
        if platforms.is_linux() or platforms.is_mac():
            flags = 0x1
            if FILELOCK_SHARED == self._locktype:
                flags = FILELOCK_SHARED
            elif FILELOCK_EXCLUSIVE == self._locktype:
                flags = FILELOCK_EXCLUSIVE
            else:
                raise err.LockFileError('does not support this lock type')
            if not blocking:
                flags |= FILELOCK_NONBLOCKING
            ret = None
            try:
                ret = fcntl.flock(self._fhandle, flags)
            except IOError as error:
                raise err.LockFileError(error)
            except Exception as error:
                raise err.LockFileError(error)
            return ret
        elif platforms.is_windows():
            win_lockfile(self._fhandle, blocking)
コード例 #3
0
 def unlock(self):
     """unlock the locked file"""
     if platforms.is_linux() or platforms.is_mac():
         try:
             fcntl.flock(self._fhandle, FILELOCK_UNLOCK)
         except Exception as error:
             raise err.LockFileError(error)
     elif platforms.is_windows():
         win_unlockfile(self._fhandle)
コード例 #4
0
ファイル: exfile.py プロジェクト: jili0503/CUP
import sys
import copy
import shutil

from cup import err
from cup import decorators
from cup import platforms

__all__ = [
    'LockFile', 'FILELOCK_SHARED', 'FILELOCK_EXCLUSIVE',
    'FILELOCK_NONBLOCKING', 'FILELOCK_UNLOCK', 'mk_newnode'
]

CANNOT_DEL_PATHLIST = ['/']

if platforms.is_linux() or platforms.is_mac():
    import fcntl
    FILELOCK_EXCLUSIVE = fcntl.LOCK_EX
    FILELOCK_SHARED = fcntl.LOCK_SH
    FILELOCK_NONBLOCKING = fcntl.LOCK_NB
    FILELOCK_UNLOCK = fcntl.LOCK_UN
elif platforms.is_windows():
    import msvcrt

    def file_size(fobj):
        """win file size"""
        return os.path.getsize(os.path.realpath(fobj.name))

    def win_lockfile(fobj, blocking=True):
        """win lock file"""
        flags = msvcrt.LK_RLCK
コード例 #5
0
ファイル: heartbeat.py プロジェクト: shiqianwei0508/CUP
                return getattr(builtins, name)
            """Forbid everything else"""
            raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
                                         (module, name))
        else:
            pass


def restricted_loads(s):
    """Helper function analogous to pickle.loads()"""
    return RestrictedUnpickler(io.BytesIO(s)).load()


if plat.is_linux():
    from cup.res import linux
elif plat.is_mac():
    from cup.res import mac

try:
    # pylint: disable=W0611
    import Queue as queue
except ImportError:
    # pylint: disable=F0401
    import queue


class Device(object):
    """
    Base class for all devices in heartbeat service
    """
    def __init__(self, name):