我的应用程序使用redis与本地计算机主机连接良好,但我发现与云托管的redis连接时遇到困难。我也尝试过在渲染和redis entriprise网站上制作redis数据库。我尝试过:
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)
})();
这个函数在我的本地机器上运行良好,本地托管的redis-server端口为:6379使用redis依赖项的函数:
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,
});
};
我也尝试过使用render为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)
})();
是瑞德要用公牛还是我走错方向了...
提前感谢您的时间和解决方案!
1条答案
按热度按时间dgtucam11#