bind EADDRINUSE null:8081 with cluster and express js

31 Views Asked by At

Server.js

import express from "express";
import bodyParser from "body-parser"
import {launch} from "./main.js";
import cors from 'cors';
const app = express();
const port = 8081;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors())



app.get("/", async (req, res) => {

    const data = req.body; // Accéder aux données du corps de la requête
    console.log("req = ", data);
   // Faites quelque chose avec les données
    await launch(data.gcp);
   res.send(data.gcp);


});
app.get("/info", (req, res) => {
    res.send("ceci est le conteneur qui génère le prompt!");
});
app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
});

Cluster.js

import cluster from "node:cluster";
import os from "node:os";
import { getFileFromGCSBucket, uploadFiles } from "./cloud.js";
import { createDirectory } from "./tool.js";
import { elevenlab } from "./elevenlab.js";
//import { MidloopWithPromise } from "./midjourney.js";
import {loadJsonFile} from 'load-json-file';


export async function launch(gcp_id){

  console.log("gcp_id = ", gcp_id);
  await getFileFromGCSBucket(gcp_id, 'subtitle', 'data.json', 'tiktok-jules')


  
  console.log(gcp_id)
  
  const numWorkers = 3;
  
  if (cluster.isPrimary) {
    console.log(`Primary ${process.pid} is running`);
    
    // Fork workers
    for (let i = 0; i < numWorkers; i++) {
      cluster.fork();
    }
    
    cluster.on("fork", (worker) => {
      console.log(`Worker ${worker.process.pid} has been forked.`);
    });
    console.log("cluster = ");

   
    } else {
      if (cluster.worker.id === 1) {
        // Premier worker exécutant la fonction midjourney
        await elevenlab().then(() => {
          uploadFiles(gcp_id, "tiktok-jules", "./audio", "audio");
          console.log("Elevenlabs worker dead");
        });
        stopWorkers()
      } 
      else if (cluster.worker.id === 2) { 
        await yo().then(() => {
          uploadFiles(gcp_id, "tiktok-jules", "./audio", "audio");
          console.log("Midjourney worker dead");
        });
        stopWorkers()
      }
    }
    
    
    
    // Fonction pour arrêter tous les workers
   async function stopWorkers() {
      const wk = cluster.worker;
      wk.kill();
    }

   async function yo(){
      console.log("yo")
    }
    
  }
  

  //await launch("uZjOtmRCS1")

Hi , i try to create an express to route to execute to function in same time and the better option is clustering. I cannot explain why but when i use the express route the code breaks on line cluster.on()... with error Error: bind EADDRINUSE null:8081. I have already watched my port and no program is listening on port 8081. And the details is that i use postman for test my request. Cordially

0

There are 0 best solutions below