"""
Code generated by `q.pipelines`
- Timestamp: 2024-05-06 15:36:44.816930
- Input file: "backend/game-preprocess/src/moveread/pipelines/game_preprocess/spec.py"
- Input spec: Tasks({
  'input_task': 'preinput',
  'Input': moveread.pipelines.game_preprocess.spec.Input,  # class
  'Output': moveread.pipelines.game_preprocess.spec.Result,  # class
  'tasks': {
    'preinput': Task(Input -> preprocess),
    'preprocess': Task(PreInput -> join),
    'join': Task(PreResult -> output)
  }
})
"""
from typing_extensions import Literal, Any, Unpack, TypedDict, NotRequired
from dslog import Logger
from q.api import ReadQueue, WriteQueue
from q.kv import QueueKV
import q.pipelines as qp
AnyT: type = Any # type: ignore
from ..spec import Input, Result, TASKS
from .types import Queues, Pipelines

def input_queue(
  input_path: str, *, protocol: Literal['sqlite', 'fs'] = 'sqlite'
) -> QueueKV[Input]:
  return QueueKV.at(Input, input_path, protocol=protocol)

def output_queue(
  output_path: str, *, protocol: Literal['sqlite', 'fs'] = 'sqlite'
) -> QueueKV[Result]:
  return QueueKV.at(Result, output_path, protocol=protocol)

def queues(
  path: str, *,
  protocol: Literal['sqlite', 'fs'] = 'sqlite',
) -> Queues:
  return qp.local.local_queues(path, TASKS, protocol=protocol) # type: ignore

async def connect(
  Qin: ReadQueue[Input],
  Qout: WriteQueue[Result],
  queues: Queues, *,
  logger = Logger.rich().prefix('[CONNECT]')
):
  await qp.connect(Qin, Qout, queues, input_task='preinput', logger=logger) # type: ignore

def run_pipelines(queues: Queues, **pipelines: Unpack[Pipelines]):
  qp.run_pipelines(queues, **pipelines) # type: ignore

def run(
  Qin: ReadQueue[Input],
  Qout: WriteQueue[Result],
  queues: Queues, *,
  logger = Logger.rich(),
  **pipelines: Unpack[Pipelines]
):
  ps = qp.run(Qin, Qout, queues, input_task='preinput', connect_logger=logger.prefix('[CONNECT]'), **pipelines) # type: ignore
  for name, p in ps.items():
    p.start()
    logger(f'Started process "{name}" at PID = {p.pid}')
  
  for name, p in ps.items():
    p.join()
    logger(f'Process "{name}" finished')

class Params(TypedDict):
  queues_path: str
  protocol: NotRequired[Literal['sqlite', 'fs']]
  logger: NotRequired[Logger]

def run_local(
  Qin: ReadQueue[Input],
  Qout: WriteQueue[Result], *,
  queues_path: str,
  protocol: Literal['sqlite', 'fs'] = 'sqlite',
  logger = Logger.rich(),
  **pipelines: Unpack[Pipelines]
):
  Qs = queues(queues_path, protocol=protocol)
  run(Qin, Qout, Qs, logger=logger, **pipelines)

__all__ = ['input_queue', 'output_queue', 'queues', 'connect', 'run_pipelines', 'run', 'run_local', 'Params']
