Skip to content

Commit

Permalink
Merge pull request #723 from golemfactory/task/JST-652/implement-codi…
Browse files Browse the repository at this point in the history
…ng-conventions

JST-652: Implement coding conventions
  • Loading branch information
grisha87 authored Dec 15, 2023
2 parents 9c8fc52 + 50cb9d8 commit 6731207
Show file tree
Hide file tree
Showing 32 changed files with 176 additions and 140 deletions.
34 changes: 34 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@
{
"checkLoops": false
}
],
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "import",
"format": ["camelCase", "PascalCase"]
},
{
"selector": "variable",
"format": ["camelCase", "UPPER_CASE"],
"leadingUnderscore": "forbid",
"trailingUnderscore": "forbid"
},
{
"selector": ["memberLike", "function"],
"format": ["camelCase"],
"leadingUnderscore": "forbid",
"filter": {
"regex": "golem.*", // ProposalProperties like 'golem.com.payment.debit-notes.accept-timeout?'
"match": false
}
},
{
"selector": "typeLike",
"format": ["PascalCase"]
},
{
"selector": "enumMember",
"format": ["PascalCase"]
},
{
"selector": "objectLiteralProperty",
"format": null // We have too many varrying cases like YAGNA_APPKEY, Authorization, golem.com.scheme.payu.payment-timeout-sec? which break this constantly
}
]
},
"overrides": [
Expand Down
13 changes: 5 additions & 8 deletions examples/blender/blender.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TaskExecutor } from "@golem-sdk/golem-js";
import { program } from "commander";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const DIR_NAME = fileURLToPath(new URL(".", import.meta.url));

const blender_params = (frame) => ({
const blenderParams = (frame) => ({
scene_file: "/golem/resource/scene.blend",
resolution: [400, 300],
use_compositing: false,
Expand Down Expand Up @@ -32,19 +32,16 @@ async function main(subnetTag: string, driver?: string, network?: string, debug?

try {
executor.onActivityReady(async (ctx) => {
await ctx.uploadFile(`${__dirname}/cubes.blend`, "/golem/resource/scene.blend");
await ctx.uploadFile(`${DIR_NAME}/cubes.blend`, "/golem/resource/scene.blend");
});

const futureResults = [0, 10, 20, 30, 40, 50].map((frame) =>
executor.run(async (ctx) => {
const result = await ctx
.beginBatch()
.uploadJson(blender_params(frame), "/golem/work/params.json")
.uploadJson(blenderParams(frame), "/golem/work/params.json")
.run("/golem/entrypoints/run-blender.sh")
.downloadFile(
`/golem/output/out${frame?.toString().padStart(4, "0")}.png`,
`${__dirname}/output_${frame}.png`,
)
.downloadFile(`/golem/output/out${frame?.toString().padStart(4, "0")}.png`, `${DIR_NAME}/output_${frame}.png`)
.end();
return result?.length ? `output_${frame}.png` : "";
}),
Expand Down
8 changes: 4 additions & 4 deletions examples/external-request/request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { TaskExecutor } from "@golem-sdk/golem-js";
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const DIR_NAME = fileURLToPath(new URL(".", import.meta.url));

(async function main() {
const executor = await TaskExecutor.create({
manifest: Buffer.from(readFileSync(`${__dirname}/manifest.json`, "utf-8")).toString("base64"),
manifest: Buffer.from(readFileSync(`${DIR_NAME}/manifest.json`, "utf-8")).toString("base64"),
/**
* Uncomment this if you have a certificate and a signed manifest
*/
// manifestSig: readFileSync(`${__dirname}/manifest.json.base64.sign.sha256.base64`, "utf-8"),
// manifestCert: readFileSync(`${__dirname}/golem-manifest.crt.pem.base64`, "utf-8"),
// manifestSig: readFileSync(`${DIR_NAME}/manifest.json.base64.sign.sha256.base64`, "utf-8"),
// manifestCert: readFileSync(`${DIR_NAME}/golem-manifest.crt.pem.base64`, "utf-8"),
// manifestSigAlgorithm: "sha256",
capabilities: ["inet", "manifest-support"],
});
Expand Down
22 changes: 11 additions & 11 deletions examples/fibonacci/fibo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node */

function fibo_loop(n) {
function fiboLoop(n) {
let a = 1,
b = 0;
while (n >= 0) {
Expand All @@ -10,30 +10,30 @@ function fibo_loop(n) {
return b;
}

function fibo_recur(n) {
return n > 1 ? fibo_recur(n - 1) + fibo_recur(n - 2) : 1;
function fiboRecur(n) {
return n > 1 ? fiboRecur(n - 1) + fiboRecur(n - 2) : 1;
}

function fibo_memo(n, memo) {
function fiboMemo(n, memo) {
memo = memo || {};
if (memo[n]) return memo[n];
if (n <= 1) return 1;
return (memo[n] = fibo_memo(n - 1, memo) + fibo_memo(n - 2, memo));
return (memo[n] = fiboMemo(n - 1, memo) + fiboMemo(n - 2, memo));
}

function benchmark(fn, n, type) {
const time_start = process.hrtime();
const timeStart = process.hrtime();
const result = fn(n);
const time_stop = process.hrtime(time_start);
return { type, result, time: parseFloat(time_stop.join(".")) };
const timeSTop = process.hrtime(timeStart);
return { type, result, time: parseFloat(timeSTop.join(".")) };
}

const n = process.argv[2] || 1;

const results = [
benchmark(fibo_loop, n, "loop"),
benchmark(fibo_recur, n, "recursion"),
benchmark(fibo_memo, n, "memoization"),
benchmark(fiboLoop, n, "loop"),
benchmark(fiboRecur, n, "recursion"),
benchmark(fiboMemo, n, "memoization"),
].sort((a, b) => a.time - b.time);

console.table(results);
4 changes: 4 additions & 0 deletions src/activity/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class Result<TData = any> implements ResultData<TData> {
}

export interface StreamingBatchEvent {
// Reason for disable: That's something what yagna returns from its api
// eslint-disable-next-line @typescript-eslint/naming-convention
batch_id: string;
index: number;
timestamp: string;
Expand All @@ -83,6 +85,8 @@ interface RuntimeEventStarted {
}

interface RuntimeEventFinished {
// Reason for disable: That's something what yagna returns from its api
// eslint-disable-next-line @typescript-eslint/naming-convention
return_code: number;
message: string;
}
4 changes: 2 additions & 2 deletions src/activity/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ describe("Activity Pool Service", () => {
describe("run()", () => {
it("should start service", async () => {
await activityService.run();
expect(activityService.isRunning).toEqual(true);
expect(activityService.isRunning()).toEqual(true);
await activityService.end();
});
});
describe("end()", () => {
it("should stop service", async () => {
await activityService.run();
await activityService.end();
expect(activityService.isRunning).toEqual(false);
expect(activityService.isRunning()).toEqual(false);
});
});
describe("getActivity()", () => {
Expand Down
13 changes: 7 additions & 6 deletions src/activity/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface ActivityServiceOptions extends ActivityOptions {}
export class ActivityPoolService {
private logger: Logger;
private pool: Activity[] = [];
private _isRunning = false;
private runningState = false;

constructor(
private yagnaApi: YagnaApi,
private agreementService: AgreementPoolService,
Expand All @@ -29,19 +30,19 @@ export class ActivityPoolService {
* Start ActivityPoolService
*/
async run() {
this._isRunning = true;
this.runningState = true;
this.logger.debug("Activity Pool Service has started");
}

get isRunning() {
return this._isRunning;
isRunning() {
return this.runningState;
}

/**
* Get an activity from the pool of available ones or create a new one
*/
async getActivity(): Promise<Activity> {
if (!this._isRunning) {
if (!this.runningState) {
throw new GolemError("Unable to get activity. Activity service is not running");
}
return this.pool.shift() || (await this.createActivity());
Expand Down Expand Up @@ -69,7 +70,7 @@ export class ActivityPoolService {
*/
async end() {
await Promise.all(this.pool.map((activity) => activity.stop().catch((e) => this.logger.warn(e))));
this._isRunning = false;
this.runningState = false;
this.logger.debug("Activity Pool Service has been stopped");
}

Expand Down
4 changes: 2 additions & 2 deletions src/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RequireAtLeastOne } from "../utils/types";
/**
* Global Event Type with which all API events will be emitted. It should be used on all listeners that would like to handle events.
*/
export const EventType = "GolemEvent";
export const EVENT_TYPE = "GolemEvent";

// Temporary polyfill
// It is now implemented natively only for nodejs 19 and newest browsers
Expand All @@ -26,7 +26,7 @@ class CustomEvent<DataType> extends Event {

export abstract class BaseEvent<DataType> extends CustomEvent<DataType> {
constructor(data?: DataType) {
super(EventType, { detail: data });
super(EVENT_TYPE, { detail: data });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as Events from "./events";
export { EventType, BaseEvent } from "./events";
export { EVENT_TYPE, BaseEvent } from "./events";
12 changes: 6 additions & 6 deletions src/executor/executor.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Package, PackageOptions } from "../package";
import { MarketService } from "../market";
import { AgreementPoolService } from "../agreement";
import { Task, TaskQueue, TaskService, Worker, TaskOptions } from "../task";
import { PaymentService, PaymentOptions } from "../payment";
import { Task, TaskOptions, TaskQueue, TaskService, Worker } from "../task";
import { PaymentOptions, PaymentService } from "../payment";
import { NetworkService } from "../network";
import { sleep, Logger, LogLevel, runtimeContextChecker, Yagna } from "../utils";
import { StorageProvider, GftpStorageProvider, NullStorageProvider, WebSocketBrowserStorageProvider } from "../storage";
import { Logger, LogLevel, runtimeContextChecker, sleep, Yagna } from "../utils";
import { GftpStorageProvider, NullStorageProvider, StorageProvider, WebSocketBrowserStorageProvider } from "../storage";
import { ExecutorConfig } from "./config";
import { Events } from "../events";
import { StatsService } from "../stats/service";
import { TaskServiceOptions } from "../task/service";
import { NetworkServiceOptions } from "../network/service";
import { AgreementServiceOptions } from "../agreement/service";
import { WorkOptions } from "../task/work";
import { MarketOptions } from "../market/service";
import { RequireAtLeastOne } from "../utils/types";
import { TaskExecutorEventsDict } from "./events";
import { EventEmitter } from "eventemitter3";
import { GolemError } from "../error/golem-error";
import { WorkOptions } from "../task/work";

const terminatingSignals = ["SIGINT", "SIGTERM", "SIGBREAK", "SIGHUP"];

Expand Down Expand Up @@ -80,7 +80,7 @@ export type ExecutorOptions = {
PaymentOptions &
NetworkServiceOptions &
AgreementServiceOptions &
Omit<WorkOptions, "isRunning">;
WorkOptions;

/**
* Contains information needed to start executor, if string the imageHash is required, otherwise it should be a type of {@link ExecutorOptions}
Expand Down
43 changes: 24 additions & 19 deletions src/golem_network/golem_network.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import { v4 } from "uuid";
import { YagnaOptions } from "../executor";
import { Job } from "../job";
import { Yagna } from "../utils";
import { Yagna, YagnaApi } from "../utils";
import { RunJobOptions } from "../job/job";
import { GolemError } from "../error/golem-error";

export type GolemNetworkConfig = Partial<RunJobOptions> & { yagna?: YagnaOptions };

/**
* The Golem Network class provides a high-level API for running jobs on the Golem Network.
*/
export class GolemNetwork {
private _yagna: Yagna | null = null;
private yagna: Yagna;
private api: YagnaApi | null = null;

private jobs = new Map<string, Job>();

/**
* @param config - Configuration options that will be passed to all jobs created by this instance.
*/
constructor(private readonly config: Partial<RunJobOptions> & { yagna?: YagnaOptions }) {}

private get yagna() {
if (this._yagna === null) {
throw new GolemError("GolemNetwork not initialized, please run init() first");
}
return this._yagna;
constructor(private readonly config: GolemNetworkConfig) {
this.yagna = new Yagna(this.config.yagna);
}

public isInitialized() {
return this._yagna !== null;
return this.api !== null;
}

public async init() {
if (this._yagna !== null) {
return;
}
const yagna = new Yagna(this.config.yagna);
// this will throw an error if yagna is not running
await yagna.connect();
this._yagna = yagna;
await this.yagna.connect();
this.api = this.yagna.getApi();
}

/**
Expand All @@ -46,22 +39,34 @@ export class GolemNetwork {
* @param options - Configuration options for the job. These options will be merged with the options passed to the constructor.
*/
public createJob<Output = unknown>(options: RunJobOptions = {}) {
this.checkInitialization();

const jobId = v4();
const job = new Job<Output>(jobId, this.yagna.getApi(), { ...this.config, ...options });
const job = new Job<Output>(jobId, this.api!, { ...this.config, ...options });
this.jobs.set(jobId, job);

return job;
}

public getJobById(id: string) {
this.checkInitialization();

return this.jobs.get(id);
}

/**
* Close the connection to the Yagna service and cancel all running jobs.
*/
public async close() {
const pendingJobs = Array.from(this.jobs.values()).filter((job) => job.isRunning);
const pendingJobs = Array.from(this.jobs.values()).filter((job) => job.isRunning());
await Promise.allSettled(pendingJobs.map((job) => job.cancel()));
await this.yagna.end();
this.api = null;
}

private checkInitialization() {
if (!this.isInitialized()) {
throw new GolemError("GolemNetwork not initialized, please run init() first");
}
}
}
2 changes: 1 addition & 1 deletion src/golem_network/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { GolemNetwork } from "./golem_network";
export { GolemNetwork, GolemNetworkConfig } from "./golem_network";
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { ProposalFilters, ProposalFilter, MarketHelpers, MarketService, MarketOp
export { Package, PackageOptions, AllPackageOptions } from "./package";
export { PaymentFilters, PaymentService, PaymentOptions } from "./payment";
export { NetworkService, NetworkServiceOptions } from "./network";
export { Events, BaseEvent, EventType } from "./events";
export { Events, BaseEvent, EVENT_TYPE } from "./events";
export {
Logger,
LogLevel,
Expand All @@ -25,5 +25,5 @@ export {
} from "./utils";
export { Yagna, YagnaOptions } from "./utils/yagna/yagna";
export { Job, JobState } from "./job";
export { GolemNetwork } from "./golem_network";
export * from "./golem_network";
export { Worker, WorkContext } from "./task";
Loading

0 comments on commit 6731207

Please sign in to comment.