Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
[main] Instructions to stop the daemon.
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangyu Bu <xybu92@live.com>
  • Loading branch information
xybu committed Feb 4, 2017
1 parent 951aa44 commit 98fbb99
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,31 @@ debug-level log verbosity and printing log to `stderr`.
onedrived start --debug
```

To stop `onedrived` process which is running in debug mode, send `SIGINT` to
the process or hitting CTRL+C if it runs in a terminal.

### Run `onedrived` as daemon

It's suggested that you set up a log file before running in daemon mode:

```
$ onedrived-pref config set logfile_path PATH_TO_SOME_WRITABLE_FILE
```

To start the program as daemon,

```bash
onedrived start
```

To stop the daemon,

```bash
onedrived stop
```

or send `SIGTERM` to the process.

### More Usages

#### Run `onedrived` with proxies
Expand Down
25 changes: 12 additions & 13 deletions onedrived/od_main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3

import asyncio
import gc
import itertools
import logging
import os
import signal
import subprocess
import sys
import weakref
Expand Down Expand Up @@ -66,25 +66,25 @@ def init_webhook():


def shutdown_webhook():
global webhook_server
if webhook_server:
webhook_server.stop()
webhook_server.join()
webhook_server = None


# noinspection PyUnusedLocal
def shutdown_callback(msg, code):
logging.info('Shutting down.')
def shutdown_callback(code, _):
logging.info('Shutting down. Code: %s.', str(code))
asyncio.gather(*asyncio.Task.all_tasks()).cancel()
context.loop.stop()
shutdown_webhook()
shutdown_workers()
if context and context.watcher:
context.watcher.close()
try:
os.remove(pidfile)
except OSError:
pass
context.watcher = None
logging.shutdown()
logging.info('Shut down complete.')


def get_repo_table(ctx):
Expand Down Expand Up @@ -121,6 +121,7 @@ def update_subscription_for_repo(repo, subscription_id=None):
if subscription:
context.loop.call_later(int(context.config['webhook_renew_interval_sec'] * 0.75),
update_subscription_for_repo, repo, subscription.id)
gc.collect()
return subscription
return None

Expand Down Expand Up @@ -173,18 +174,16 @@ def repo_updated_callback(repo):
'pidfile': pidfile,
# 'detach': False,
'shutdown_callback': shutdown_callback,
'workdir': os.getcwd()
'workdir': os.getcwd(),
'stop_timeout': 60,
})
def main():
global webhook_server, webhook_worker

# Exit program when receiving SIGTERM or SIGINT.
signal.signal(signal.SIGTERM, shutdown_callback)
gc.enable()

# When debugging, print to stdout.
if '--debug' in sys.argv:
context.set_logger(min_level=logging.DEBUG, path=None)
context.loop.set_debug(True)
context.set_logger(min_level=logging.DEBUG, path=None)
else:
context.set_logger(min_level=logging.INFO, path=context.config['logfile_path'])

Expand Down
7 changes: 5 additions & 2 deletions onedrived/od_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def __init__(self):
self._lock = threading.Lock()

def close(self, n=1):
for _ in range(n):
self.semaphore.release()
with self._lock:
self.queued_tasks.clear()
self.tasks_by_path.clear()
for _ in range(n):
self.semaphore.release()

def add_task(self, task):
"""
Expand Down
2 changes: 1 addition & 1 deletion onedrived/od_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def run(self):
if task is not None:
logging.debug('Got task %s.', task)
task.handle()
logging.debug('Stopped.')
logging.info('Stopped.')

0 comments on commit 98fbb99

Please sign in to comment.