Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1337 from efiop/master
updater: spawn a background daemon
- Loading branch information
Showing
6 changed files
with
159 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dvc.command.base import CmdBase | ||
|
||
|
||
class CmdDaemonUpdater(CmdBase): | ||
def __init__(self, args): | ||
pass | ||
|
||
def run_cmd(self): | ||
return self.run() | ||
|
||
def run(self): | ||
import os | ||
from dvc.project import Project | ||
from dvc.updater import Updater | ||
|
||
root_dir = self._find_root() | ||
dvc_dir = os.path.join(root_dir, Project.DVC_DIR) | ||
updater = Updater(dvc_dir) | ||
updater.fetch(detach=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
import sys | ||
from subprocess import Popen | ||
from dvc.logger import Logger | ||
|
||
|
||
class Daemon(object): # pragma: no cover | ||
def _spawn_windows(self, cmd): | ||
CREATE_NEW_PROCESS_GROUP = 0x00000200 | ||
DETACHED_PROCESS = 0x00000008 | ||
creationflags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS | ||
Popen(cmd, | ||
close_fds=True, | ||
shell=False, | ||
creationflags=creationflags) | ||
|
||
def _spawn_posix(self, cmd): | ||
try: | ||
pid = os.fork() | ||
if pid > 0: | ||
return | ||
except OSError as exc: | ||
Logger.error("Failed at first fork", exc) | ||
sys.exit(1) | ||
|
||
os.setsid() | ||
os.umask(0) | ||
|
||
try: | ||
pid = os.fork() | ||
if pid > 0: | ||
sys.exit(0) | ||
except OSError as exc: | ||
Logger.error("Failed at second fork", exc) | ||
sys.exit(1) | ||
|
||
sys.stdin.close() | ||
sys.stdout.close() | ||
sys.stderr.close() | ||
|
||
Popen(cmd, | ||
close_fds=True, | ||
shell=False) | ||
|
||
def __call__(self, name): | ||
from dvc.utils import is_binary | ||
|
||
cmd = [sys.executable] | ||
if not is_binary(): | ||
cmd += ['-m', 'dvc'] | ||
cmd += ['daemon', name, '-v'] | ||
|
||
Logger.debug("Trying to spawn '{}'".format(cmd)) | ||
|
||
if os.name == 'nt': | ||
self._spawn_windows(cmd) | ||
elif os.name == 'posix': | ||
self._spawn_posix(cmd) | ||
else: | ||
raise NotImplementedError | ||
|
||
Logger.debug("Spawned '{}'".format(cmd)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters