Coretex
utils.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from multiprocessing.connection import Connection
19 
20 import logging
21 
22 from ..._folder_manager import folder_manager
23 from ...utils import createFileHandler
24 
25 
26 def sendSuccess(conn: Connection, message: str) -> None:
27  conn.send({
28  "code": 0,
29  "message": message
30  })
31 
32 
33 def sendFailure(conn: Connection, message: str) -> None:
34  conn.send({
35  "code": 1,
36  "message": message
37  })
38 
39 
40 def initializeLogger(taskRunId: int) -> None:
41  formatter = logging.Formatter(
42  fmt = "%(asctime)s %(levelname)s: %(message)s",
43  datefmt = "%Y-%m-%d %H:%M:%S",
44  style = "%",
45  )
46 
47  workerLogPath = folder_manager.getRunLogsDir(taskRunId) / "worker.log"
48  fileHandler = createFileHandler(workerLogPath)
49 
50  fileHandler.setLevel(logging.DEBUG)
51  fileHandler.setFormatter(formatter)
52 
53  logging.basicConfig(
54  level = logging.NOTSET,
55  force = True,
56  handlers = [fileHandler]
57  )