Better logging on api server
This commit is contained in:
		
							parent
							
								
									134eb654d2
								
							
						
					
					
						commit
						19cdfd0403
					
				
					 1 changed files with 8 additions and 5 deletions
				
			
		
							
								
								
									
										13
									
								
								api/index.js
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								api/index.js
									
										
									
									
									
								
							| 
						 | 
					@ -35,6 +35,9 @@ const start = process.hrtime();
 | 
				
			||||||
const log = (msg, jobNum) => {
 | 
					const log = (msg, jobNum) => {
 | 
				
			||||||
  console.log(`[${process.hrtime(start)[1] / 1000000}${jobNum !== undefined ? `:${jobNum}` : ""}]\t ${msg}`);
 | 
					  console.log(`[${process.hrtime(start)[1] / 1000000}${jobNum !== undefined ? `:${jobNum}` : ""}]\t ${msg}`);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					const error = (msg, jobNum) => {
 | 
				
			||||||
 | 
					  console.error(`[${process.hrtime(start)[1] / 1000000}${jobNum !== undefined ? `:${jobNum}` : ""}]\t ${msg}`);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class JobCache extends Map {
 | 
					class JobCache extends Map {
 | 
				
			||||||
  set(key, value) {
 | 
					  set(key, value) {
 | 
				
			||||||
| 
						 | 
					@ -65,7 +68,7 @@ const acceptJob = (id, sock) => {
 | 
				
			||||||
  }, sock).then(() => {
 | 
					  }, sock).then(() => {
 | 
				
			||||||
    log(`Job ${id} has finished`);
 | 
					    log(`Job ${id} has finished`);
 | 
				
			||||||
  }).catch((err) => {
 | 
					  }).catch((err) => {
 | 
				
			||||||
    console.error(`Error on job ${id}:`, err);
 | 
					    error(`Error on job ${id}:`, err, job.num);
 | 
				
			||||||
    const newJob = jobs.get(id);
 | 
					    const newJob = jobs.get(id);
 | 
				
			||||||
    if (!newJob.tag) {
 | 
					    if (!newJob.tag) {
 | 
				
			||||||
      newJob.error = err.message;
 | 
					      newJob.error = err.message;
 | 
				
			||||||
| 
						 | 
					@ -96,7 +99,7 @@ wss.on("connection", (ws, request) => {
 | 
				
			||||||
  ws.send(init);
 | 
					  ws.send(init);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ws.on("error", (err) => {
 | 
					  ws.on("error", (err) => {
 | 
				
			||||||
    console.error(err);
 | 
					    error(err);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ws.on("message", (msg) => {
 | 
					  ws.on("message", (msg) => {
 | 
				
			||||||
| 
						 | 
					@ -153,7 +156,7 @@ wss.on("connection", (ws, request) => {
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
wss.on("error", (err) => {
 | 
					wss.on("error", (err) => {
 | 
				
			||||||
  console.error("A WS error occurred: ", err);
 | 
					  error("A WS error occurred: ", err);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const httpServer = createServer();
 | 
					const httpServer = createServer();
 | 
				
			||||||
| 
						 | 
					@ -201,7 +204,7 @@ httpServer.on("request", async (req, res) => {
 | 
				
			||||||
    const data = jobs.get(id).data;
 | 
					    const data = jobs.get(id).data;
 | 
				
			||||||
    jobs.delete(id);
 | 
					    jobs.delete(id);
 | 
				
			||||||
    return res.end(data, (err) => {
 | 
					    return res.end(data, (err) => {
 | 
				
			||||||
      if (err) console.error(err);
 | 
					      if (err) error(err);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    res.statusCode = 404;
 | 
					    res.statusCode = 404;
 | 
				
			||||||
| 
						 | 
					@ -228,7 +231,7 @@ httpServer.on("upgrade", (req, sock, head) => {
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
httpServer.on("error", (e) => {
 | 
					httpServer.on("error", (e) => {
 | 
				
			||||||
  console.error("An HTTP error occurred: ", e);
 | 
					  error("An HTTP error occurred: ", e);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
const port = parseInt(process.env.PORT) || 3762;
 | 
					const port = parseInt(process.env.PORT) || 3762;
 | 
				
			||||||
httpServer.listen(port, () => {
 | 
					httpServer.listen(port, () => {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue