finding difficulty in setting up redis for my node.js application

351 Views Asked by At

my application connected well with the local machine host using redis but i find difficulties while connecting with cloud hosted redis. I have also tried making redis data base on render and redis entriprise website. I have tried:


import Redis from 'ioredis'

const redis = new Redis({
    host: '<host>',
    port: 13280,
    password: '<password>'
});


import Redis from 'ioredis'
(async () => {
  // Connect to your internal Redis instance using the REDIS_URL environment variable
  // The REDIS_URL is set to the internal Redis URL e.g. redis://red-343245ndffg023:6379
  const client = createClient({
      url: '<host_url>'
  });

  client.on('error', (err) => console.log('Redis Client Error', err));

  await client.connect();

  // Send and retrieve some values
  await client.set('key', 'node redis');
  const value = await client.get('key');

  console.log("found value: ", value)
})();

This function works fine with my local machine with redis-server hosted locally with port: 6379 Function that use redis dependency:


import Queue from "bull";
import Job from "../models/codesubmission.js";
import participantStatus from "../models/participantstatus.js"
import {executeCpp, executeCppWithOutputFile} from "../executecode/Cpp.js";
import {executePy} from "../executecode/Py.js"
import fs from "fs/promises"
import path from "path";
import { fileURLToPath } from 'url';

// const jobQueue = new Queue("job-runner-queue");
const jobQueue = new Queue("job-runner-queue");
const NUM_WORKERS = 5;

const __filename = fileURLToPath(import.meta.url);
const __dirname= path.dirname(__filename);
console.log("dirname",__dirname)
const outputPath = path.join(__dirname, "outputs");

jobQueue.process(NUM_WORKERS, async ({ data }) => {
  console.log("data",data)
  const jobId = data.id;
  const job = await Job.findById(jobId);
  console.log("ppp", job)
  if (job === undefined) {
    throw Error(`cannot find Job with id ${jobId}`);
  }
  try {
    let output1, output2, output3;
    job["startedAt"] = new Date();
    if (job.language === "cpp") {
      console.log("executeCpp");
      output1 = await executeCpp(job.filepath);
      (async () => {
        try {
          await fs.unlink(job.filepath);
        } catch (e) {
          console.log(e);
        }
      })();
      output2= await executeCppWithOutputFile("t2");
      output3= await executeCppWithOutputFile("t3");
      console.log(output1, getDifference(output1, data.r1))
      console.log(output2, getDifference(output2, data.r2));
      console.log(output3, getDifference(output3, data.r3))
      
      //delete here
    } else if (job.language === "py") {
      output1 = await executePy(job.filepath);
      //delete here
    }
    // job["completedAt"] = new Date();
    job["res1"] = getDifference(output1, data.r1)===0? true: false;
    job["res2"] = getDifference(output2, data.r2)===0? true: false;
    job["res3"] = getDifference(output3, data.r3)===0? true: false;

    if(job["res1"]===false || job["res2"]===false || job["res3"]===false){
      job["status"] = "error";
    }
    else{
      job["status"] = "success";
    }
  
    console.log(job);
    await job.save();


    
    return true;
  } catch (err) {
    job["completedAt"] = new Date();
    job["output"] = JSON.stringify(err);
    job["status"] = "error";
    console.log(job);
    await job.save();
    throw Error(JSON.stringify(err));
  }
});

jobQueue.on("failed", (error) => {
  console.error(error.data.id, error.failedReason);
});

function getDifference(a, b) {
    var i = 0;
    var j = 0;
    var result = 0;
    var as= a.length, bs= b.length;
    if(as!= bs) return -1;
    while (i< as && j< bs) {
      if (a[i] != b[j])
        result ++;
      i++;
      j++;
    }
    return result;
}

export const addJobToQueue = async (jobId, t1, t2, t3, r1, r2, r3, contestid, userid, problemnumber) => {
  console.log(jobId);
  await jobQueue.add({
    id: jobId,
    t1: t1,
    t2: t2,
    t3: t3,
    r1: r1,
    r2: r2,
    r3: r3,
    contestid: contestid,
    userid: userid,
    problemnumber: problemnumber,
  });
};

I have also tried to connect using render provided docs for nodejs:


import { createClient } from 'redis';

(async () => {
  // Connect to your internal Redis instance using the REDIS_URL environment variable
  // The REDIS_URL is set to the internal Redis URL e.g. redis://red-343245ndffg023:6379
  const client = createClient({
      url: process.env.REDIS_URL
  });

  client.on('error', (err) => console.log('Redis Client Error', err));

  await client.connect();

  // Send and retrieve some values
  await client.set('key', 'node redis');
  const value = await client.get('key');

  console.log("found value: ", value)
})();

Is that redis needed to use bulls or I am at wrong direction...

Thanks in advance for your time and solution!

1

There are 1 best solutions below

0
On
const jobQueue = new Queue("job-runner-queue", 
{ redis: { port: 13280, host: 'host-address', password: '<password' 
}} );

jobQueue.on('error', (error) => {
  console.log(error);
})