forked from LBRYCommunity/lbry-sdk
Service.run -> Daemon.run
This commit is contained in:
parent
d11f4f9bed
commit
7ffb169376
4 changed files with 37 additions and 32 deletions
|
@ -1,11 +1,10 @@
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import signal
|
|
||||||
from typing import List, Optional, Tuple, NamedTuple
|
from typing import List, Optional, Tuple, NamedTuple
|
||||||
|
|
||||||
from aiohttp.web import GracefulExit
|
|
||||||
|
|
||||||
|
from lbry.conf import Config
|
||||||
from lbry.db import Database
|
from lbry.db import Database
|
||||||
from lbry.db.constants import TXO_TYPES
|
from lbry.db.constants import TXO_TYPES
|
||||||
from lbry.schema.result import Censor
|
from lbry.schema.result import Censor
|
||||||
|
@ -23,8 +22,10 @@ class BlockEvent(NamedTuple):
|
||||||
|
|
||||||
class Sync:
|
class Sync:
|
||||||
|
|
||||||
def __init__(self, service: 'Service'):
|
def __init__(self, ledger: Ledger, db: Database):
|
||||||
self.service = service
|
self.ledger = ledger
|
||||||
|
self.conf = ledger.conf
|
||||||
|
self.db = db
|
||||||
|
|
||||||
self._on_block_controller = EventController()
|
self._on_block_controller = EventController()
|
||||||
self.on_block = self._on_block_controller.stream
|
self.on_block = self._on_block_controller.stream
|
||||||
|
@ -74,30 +75,6 @@ class Service:
|
||||||
self._on_connected_controller = EventController()
|
self._on_connected_controller = EventController()
|
||||||
self.on_connected = self._on_connected_controller.stream
|
self.on_connected = self._on_connected_controller.stream
|
||||||
|
|
||||||
def run(self):
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
def exit():
|
|
||||||
raise GracefulExit()
|
|
||||||
|
|
||||||
try:
|
|
||||||
loop.add_signal_handler(signal.SIGINT, exit)
|
|
||||||
loop.add_signal_handler(signal.SIGTERM, exit)
|
|
||||||
except NotImplementedError:
|
|
||||||
pass # Not implemented on Windows
|
|
||||||
|
|
||||||
try:
|
|
||||||
loop.run_until_complete(self.start())
|
|
||||||
loop.run_forever()
|
|
||||||
except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError):
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
loop.run_until_complete(self.stop())
|
|
||||||
logging.shutdown()
|
|
||||||
|
|
||||||
if hasattr(loop, 'shutdown_asyncgens'):
|
|
||||||
loop.run_until_complete(loop.shutdown_asyncgens())
|
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
await self.db.open()
|
await self.db.open()
|
||||||
await self.wallets.ensure_path_exists()
|
await self.wallets.ensure_path_exists()
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import signal
|
||||||
from weakref import WeakSet
|
from weakref import WeakSet
|
||||||
|
from aiohttp.web import GracefulExit
|
||||||
from aiohttp.web import Application, AppRunner, WebSocketResponse, TCPSite, Response
|
from aiohttp.web import Application, AppRunner, WebSocketResponse, TCPSite, Response
|
||||||
from aiohttp.http_websocket import WSMsgType, WSCloseCode
|
from aiohttp.http_websocket import WSMsgType, WSCloseCode
|
||||||
|
|
||||||
from lbry.extras.daemon.json_response_encoder import JSONResponseEncoder
|
from lbry.extras.daemon.json_response_encoder import JSONResponseEncoder
|
||||||
from lbry.service.base import Service
|
from lbry.service.base import Service
|
||||||
from lbry.service.api import API
|
from lbry.service.api import API
|
||||||
|
from lbry.console import Console
|
||||||
|
|
||||||
|
|
||||||
def jsonrpc_dumps_pretty(obj, **kwargs):
|
def jsonrpc_dumps_pretty(obj, **kwargs):
|
||||||
|
@ -62,9 +64,10 @@ class WebSocketManager(WebSocketResponse):
|
||||||
|
|
||||||
class Daemon:
|
class Daemon:
|
||||||
|
|
||||||
def __init__(self, service: Service):
|
def __init__(self, service: Service, console: Console):
|
||||||
self.service = service
|
self.service = service
|
||||||
self.conf = service.conf
|
self.conf = service.conf
|
||||||
|
self.console = console
|
||||||
self.api = API(service)
|
self.api = API(service)
|
||||||
self.app = Application()
|
self.app = Application()
|
||||||
self.app['websockets'] = WeakSet()
|
self.app['websockets'] = WeakSet()
|
||||||
|
@ -80,7 +83,32 @@ class Daemon:
|
||||||
self.app.on_shutdown.append(self.on_shutdown)
|
self.app.on_shutdown.append(self.on_shutdown)
|
||||||
self.runner = AppRunner(self.app)
|
self.runner = AppRunner(self.app)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
def exit():
|
||||||
|
raise GracefulExit()
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop.add_signal_handler(signal.SIGINT, exit)
|
||||||
|
loop.add_signal_handler(signal.SIGTERM, exit)
|
||||||
|
except NotImplementedError:
|
||||||
|
pass # Not implemented on Windows
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop.run_until_complete(self.start())
|
||||||
|
loop.run_forever()
|
||||||
|
except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError):
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
loop.run_until_complete(self.stop())
|
||||||
|
logging.shutdown()
|
||||||
|
|
||||||
|
if hasattr(loop, 'shutdown_asyncgens'):
|
||||||
|
loop.run_until_complete(loop.shutdown_asyncgens())
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
|
self.console.starting()
|
||||||
await self.runner.setup()
|
await self.runner.setup()
|
||||||
site = TCPSite(self.runner, 'localhost', self.conf.api_port)
|
site = TCPSite(self.runner, 'localhost', self.conf.api_port)
|
||||||
await site.start()
|
await site.start()
|
||||||
|
|
|
@ -19,7 +19,7 @@ class FullNode(Service):
|
||||||
def __init__(self, ledger: Ledger, db_url: str, chain: Lbrycrd = None):
|
def __init__(self, ledger: Ledger, db_url: str, chain: Lbrycrd = None):
|
||||||
super().__init__(ledger, db_url)
|
super().__init__(ledger, db_url)
|
||||||
self.chain = chain or Lbrycrd(ledger)
|
self.chain = chain or Lbrycrd(ledger)
|
||||||
self.sync = BlockchainSync(self, self.chain)
|
self.sync = BlockchainSync(self.chain, self.db, self.conf.processes)
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
await self.chain.open()
|
await self.chain.open()
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from lbry.conf import Config
|
from lbry.conf import Config
|
||||||
from lbry.service.api import Client
|
|
||||||
from lbry.blockchain.ledger import Ledger
|
from lbry.blockchain.ledger import Ledger
|
||||||
from lbry.db import Database
|
from lbry.db import Database
|
||||||
from lbry.wallet.sync import SPVSync
|
from lbry.wallet.sync import SPVSync
|
||||||
|
|
||||||
from .base import Service
|
from .base import Service
|
||||||
|
from .api import Client
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue