Skip to content

Commit

Permalink
chore: improve debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed Apr 25, 2024
1 parent c2a65b9 commit 91800dd
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion backend/src/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Yagna } from "./services/yagna/routes.js";

export const startupFastifyServer = async (): Promise<FastifyInstance> => {
const fastify = Fastify({
logger: true,
logger: false,
});

fastify.register(fastifySensible);
Expand Down
1 change: 0 additions & 1 deletion backend/src/services/file/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const fileService = fastifyPlugin(
if (!deposit) {
throw new Error("Cant process file without deposit");
}
console.log("deposit", deposit);
container.cradle.fileService.processFile(
data?.filename,
request.user._id,
Expand Down
13 changes: 7 additions & 6 deletions backend/src/services/file/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Worker } from "../yagna/worker.js";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const DIR_NAME = fileURLToPath(new URL("../../../../temp/", import.meta.url));

import { debugLog } from "../../utils.js";

export const fileService = (
GolemSDK: typeof sdk,
contractAddress: any
Expand All @@ -18,8 +20,6 @@ export const fileService = (
processFile: (fileName: string, userId: string, depositId: bigint) => void;
init: () => void;
} => {
console.log("creating file service", contractAddress);

return {
resultStream: new Subject<IScanResult>(),
workers: {},
Expand All @@ -43,12 +43,10 @@ export const fileService = (
}
}

console.log("Scanning file on Golem", fileName);

//TODO handle errors and timeouts
//but it seems that there was no try to find another one
//this is task executor abstraction so it should handle it for me

debugLog("file", "Scanning file on Golem", fileName);
const results = await worker.context
?.beginBatch()
.uploadFile(`${DIR_NAME}${fileName}`, `/golem/workdir/${fileName}`)
Expand All @@ -62,14 +60,17 @@ export const fileService = (
return JSON.parse((results[3].stdout || "null") as string);
},
async processFile(fileName: string, userId: string) {
debugLog("file", "Processing file", fileName);
const worker = await container.cradle.Yagna.getUserWorker(userId);
worker.addFileToQueue(fileName);
await worker.isFree(fileName);
debugLog("file", "Worker is free", fileName);
worker.setState("busy");
const scanResult = await this.scanFileOnGolem(fileName, worker);
debugLog("file", "Scan result", fileName, scanResult);
worker.setState("free");

if (scanResult) {
console.log("scanResult", scanResult);
this.resultStream.next({
result: scanResult.Viruses ? fileStatus.INFECTED : fileStatus.CLEAN,
id: fileName,
Expand Down
4 changes: 0 additions & 4 deletions backend/src/services/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export const paymentService = (
functionName: "getDepositByNonce",
args: [BigInt(nonce), user.walletAddress],
});

console.log("data", data);
console.log("nonce", nonce);
if (!data) {
throw new Error(
`Deposit not found with nonce ${nonce} and funder ${user.walletAddress}`
Expand All @@ -61,7 +58,6 @@ export const paymentService = (
if (feeRatio < Number(serviceFee)) {
documentDeposit.isValid = false;
}
console.log("going to add deposit");
userService.addDeposit(userId, documentDeposit);
},
};
Expand Down
1 change: 0 additions & 1 deletion backend/src/services/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const userService: IUserService = {
},
//@ts-ignore
findById: async (userId: string) => {
console.log("find by id", userId);
return userModel.findOne({ _id: userId });
},
//@ts-ignore
Expand Down
1 change: 0 additions & 1 deletion backend/src/services/yagna/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const Yagna = fastifyPlugin((fastify: FastifyInstance, opts, done) => {
});
}
const allocation = await Yagna.getUserAllocation(requestUser._id);
console.log("allocation", allocation);
if (!allocation) {
reply.code(500).send({
message: "Unable to create allocation",
Expand Down
17 changes: 12 additions & 5 deletions backend/src/services/yagna/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export class Yagna {
}

releaseAgreement(activityId: string) {
console.log("releasing agreement", activityId);
return this.activityService.terminateAgreement(activityId);
}

Expand All @@ -137,6 +136,7 @@ export class Yagna {

//task executor creates allocation as well
async createExecutor(userId: string) {
debugLog("payments", "creating executor", userId);
const userService = container.cradle.userService;
const userDeposit = await userService.getCurrentDeposit(userId);

Expand All @@ -154,7 +154,11 @@ export class Yagna {
expirationSec: Number(userDeposit.validTo) - dayjs().unix() - 1000,
};

console.log("crteaing executor", allocation);
//@ts-ignore
if (allocation?.id) {
debugLog("payments", "allocation already exists, reusing", allocation);
}

const executor = await TaskExecutor.create({
package: "pociejewski/clamav:latest",
//here I would like to be able to pass SUBNET but i have to do that usiong env
Expand All @@ -177,6 +181,7 @@ export class Yagna {

//task executor creates agreement and activity
async getUserWorker(userId: string): Promise<Worker> {
debugLog("payments", "getting user worker", userId);
return new Promise(async (resolve, reject) => {
const worker = this.userContext.getWorker(userId);
if (worker) {
Expand Down Expand Up @@ -214,6 +219,11 @@ export class Yagna {
newWorker.context = ctx;

newWorker.setState("free");
debugLog(
"payments",
"worker connected, agreement done",
ctx.activity.agreement.id
);
container.cradle.userService.setCurrentActivityId(
userId,
ctx.activity.agreement.id
Expand Down Expand Up @@ -252,10 +262,8 @@ export class Yagna {
);

debitNoteEvents.forEach((event: any) => {
console.log("debit note event", event);
const debitNoteId = event.debitNoteId;
const debitNote = this.paymentService.getDebitNote(debitNoteId);
console.log("debit note", debitNote);
this.debitNoteEvents.next(debitNote);
this.lastDebitNoteEventTimestamp = event.eventDate;
});
Expand All @@ -271,7 +279,6 @@ export class Yagna {
);

invoiceEvents.forEach((event: any) => {
console.log("invoice event", event);
this.debitNoteEvents.next(event);
this.lastInvoiceEventTimestamp = event.eventDate;
});
Expand Down
5 changes: 0 additions & 5 deletions backend/src/services/yagna/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export class Worker {
const subscription = this.state$.subscribe((state) => {
const isFirstInQueue = this.queue[this.queue.length - 1] === fileId;
if (state === "free" && isFirstInQueue) {
console.log("Worker is free now");
// subscription.unsubscribe();
resolve();
}
});
Expand All @@ -56,10 +54,7 @@ export class Worker {
return new Promise((resolve) => {
const subscription = this.state$.subscribe(async (state) => {
if (state !== "connecting") {
console.log("Worker is connected now");
// subscription.unsubscribe();
const state = await this.context?.getState();
console.log("state", state);
resolve(ActivityStateEnum.Ready === state);
}
});
Expand Down
2 changes: 1 addition & 1 deletion golem-sdk-task-executor

0 comments on commit 91800dd

Please sign in to comment.