Skip to content

Commit

Permalink
chore: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dmerrill6 committed May 28, 2019
1 parent 37c7510 commit 7cbb73d
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 81 deletions.
10 changes: 5 additions & 5 deletions lib/src/brokkr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IClient from "./clients/iclient";
import IClient from './clients/iclient';
import Saga from './entities/saga';
import { IWorker } from "./interfaces";
import { IWorker } from './interfaces';

class Brokkr {
private client: IClient;
Expand All @@ -18,13 +18,13 @@ class Brokkr {
return saga;
}

public async loadPendingSagas(): Promise<Saga>{
throw "TODO: Implement this in case the service running this lib has to be restarted mid-process";
public async loadPendingSagas(): Promise<Saga> {
throw Error('TODO: Implement this in case the service running this lib has to be restarted mid-process');
}

public registerWorker(worker: IWorker) {
this.workers.push(worker);
}
}

export default Brokkr;
export default Brokkr;
36 changes: 22 additions & 14 deletions lib/src/clients/build-redis-client.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
import {RedisClient} from 'redis';
import IClient from './iclient'
import { RedisClient } from 'redis';
import IClient from './iclient';

const buildRedisClient = (client: RedisClient): IClient => {
const set = <T>(table: string, key: string, value: T) => {
return new Promise<boolean>((resolve, reject) => {
client.hset(table, key, JSON.stringify(value), (err) => {
if (err) { reject(err) };
client.hset(table, key, JSON.stringify(value), err => {
if (err) {
reject(err);
}
resolve(true);
});
});
}
};

const get = <T>(table: string, key: string) => {
return new Promise<T>((resolve, reject) => {
client.hget(table, key, (err, res) => {
if (err) { reject(err) };
if (err) {
reject(err);
}
resolve(JSON.parse(res));
});
});
}
};

const getKeys = (table: string) => {
return new Promise<string[]>((resolve, reject) => {
client.hkeys(table, (err, res) => {
if (err) { reject(err) };
if (err) {
reject(err);
}
resolve(res);
});
});
}
};

const getMultiple = <T>(table: string, keyArray: string[]) => {
return new Promise<T[]>((resolve, reject) => {
client.hmget(table, keyArray, (err, res) => {
if (err) { reject(err) };
if (err) {
reject(err);
}
const parsedRes = res.map(json => JSON.parse(json));
resolve(parsedRes);
});
});
}
};

const result = {
get,
getKeys,
getMultiple,
set,
}
};
return result;
}
};

export default buildRedisClient;
export default buildRedisClient;
4 changes: 2 additions & 2 deletions lib/src/clients/iclient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IHashMap} from '../interfaces';
import { IHashMap } from '../interfaces';

interface IClient {
set<T>(table: string, key: string, value: T): Promise<boolean>;
Expand All @@ -7,4 +7,4 @@ interface IClient {
getMultiple<T>(table: string, keyArray: string[]): Promise<T[]>;
}

export default IClient;
export default IClient;
4 changes: 2 additions & 2 deletions lib/src/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {default as IClient} from './iclient';
export {default as buildRedisClient} from './build-redis-client';
export { default as IClient } from './iclient';
export { default as buildRedisClient } from './build-redis-client';
16 changes: 9 additions & 7 deletions lib/src/entities/entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {IClient} from "../clients";
import {create, get} from '../helpers/db';
import { IHashMap } from "../interfaces";
import { IClient } from '../clients';
import { create, get } from '../helpers/db';
import { IHashMap } from '../interfaces';

export interface IEntity {
id?: string,
id?: string;
}

abstract class Entity<T extends IEntity> {
Expand Down Expand Up @@ -36,10 +36,12 @@ abstract class Entity<T extends IEntity> {

public async getValues(): Promise<T> {
const id = this.getId();
if (!id) { throw Error('Cannot get values from an uninitialized entity'); }
const result = await get<T>(this.client, this.namespace, this.tableName, id)
if (!id) {
throw Error('Cannot get values from an uninitialized entity');
}
const result = await get<T>(this.client, this.namespace, this.tableName, id);
return result;
}
}

export default Entity;
export default Entity;
4 changes: 2 additions & 2 deletions lib/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {default as Saga, SagaStatus} from './saga';
export {default as SagaStep, SagaStepStatus} from './saga-step';
export { default as Saga, SagaStatus } from './saga';
export { default as SagaStep, SagaStepStatus } from './saga-step';
22 changes: 11 additions & 11 deletions lib/src/entities/saga-step.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IClient from "../clients/iclient";
import {create} from '../helpers/db';
import Entity from "./entity";
import IClient from '../clients/iclient';
import { create } from '../helpers/db';
import Entity from './entity';

export const TABLE_NAME = 'saga_step';

Expand All @@ -15,15 +15,15 @@ export enum SagaStepStatus {
}

export interface ISagaStep {
id?: string,
args: any[],
dependsOn: string[],
workerName: string,
status?: SagaStepStatus,
compensatorOf?: string,
id?: string;
args: any[];
dependsOn: string[];
workerName: string;
status?: SagaStepStatus;
compensatorOf?: string;
}

class SagaStep extends Entity<ISagaStep>{
class SagaStep extends Entity<ISagaStep> {
protected sagaId: string | undefined;

constructor(client: IClient, namespace: string) {
Expand Down Expand Up @@ -71,6 +71,6 @@ class SagaStep extends Entity<ISagaStep>{
// this is so we can have fast lookup of the steps for a given Saga
export const getSagaStepTableName = (sagaId: string) => {
return `${TABLE_NAME}_${sagaId}`;
}
};

export default SagaStep;
42 changes: 22 additions & 20 deletions lib/src/entities/saga.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import IClient from "../clients/iclient";
import {create, get, getMultiple, getIds, update} from '../helpers/db';
import Entity from "./entity";
import SagaStep, {SagaStepStatus, getSagaStepTableName, ISagaStep} from './saga-step';
import IClient from '../clients/iclient';
import { create, get, getIds, getMultiple, update } from '../helpers/db';
import Entity from './entity';
import SagaStep, { getSagaStepTableName, ISagaStep, SagaStepStatus } from './saga-step';

export const TABLE_NAME = 'saga';

Expand All @@ -14,27 +14,29 @@ export enum SagaStatus {
}

export interface ISaga {
id?: string,
status: SagaStatus,
};
id?: string;
status: SagaStatus;
}

class Saga extends Entity<ISaga> {
constructor(client: IClient, namespace: string) {
super(client, namespace, TABLE_NAME);
}

public async create() {
return await super.create({status: SagaStatus.Created});
return await super.create({ status: SagaStatus.Created });
}

public async addStep(
workerName: string,
args: any[],
dependsOnSteps: string[] = [],
mapPrevStepResultToArg?: Array<(data: string) => void>
mapPrevStepResultToArg?: Array<(data: string) => void>,
) {
const id = this.getId();
if (!id) { throw Error('Cannot add a step for an uninitialized Saga'); }
if (!id) {
throw Error('Cannot add a step for an uninitialized Saga');
}

const newStep = new SagaStep(this.client, this.namespace);
await newStep.createFromSaga(id, {
Expand All @@ -48,20 +50,18 @@ class Saga extends Entity<ISaga> {

public async tick() {
const id = this.getId();
const {status} = await this.getValues();
const { status } = await this.getValues();

if (status !== SagaStatus.Running || !id) { return; };
if (status !== SagaStatus.Running || !id) {
return;
}

const allStepIds = await getIds(this.client, this.namespace, getSagaStepTableName(id));
const allSteps = await getMultiple<ISagaStep>(
this.client, this.namespace, getSagaStepTableName(id), allStepIds
);
const allSteps = await getMultiple<ISagaStep>(this.client, this.namespace, getSagaStepTableName(id), allStepIds);
const unqueuedSteps = allSteps.filter(step => step.status === SagaStepStatus.Created);

if (unqueuedSteps.length === 0) {
await update<ISaga>(
this.client, this.namespace, this.tableName, id, {status: SagaStatus.Finished}
);
await update<ISaga>(this.client, this.namespace, this.tableName, id, { status: SagaStatus.Finished });
return;
}

Expand All @@ -78,7 +78,9 @@ class Saga extends Entity<ISaga> {
}
}

if (allFinished) { stepsToExecute.push(unqueuedStep); }
if (allFinished) {
stepsToExecute.push(unqueuedStep);
}
});

const stepPromises = stepsToExecute.map(stepValues => {
Expand All @@ -91,4 +93,4 @@ class Saga extends Entity<ISaga> {
}
}

export default Saga;
export default Saga;
17 changes: 8 additions & 9 deletions lib/src/helpers/db.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { IClient } from '../clients';
import { ITableMeta } from "../interfaces";
import { ITableMeta } from '../interfaces';

const META_TABLE = 'meta';


const getTable = (namespace: string, tableName: string) => `${namespace}_${tableName}`;

export const create = async <T>(client: IClient, namespace: string, tableName: string, values: T) => {
Expand All @@ -12,11 +11,11 @@ export const create = async <T>(client: IClient, namespace: string, tableName: s
// If there are no previous declarations for this table, create one
let newTableMeta: ITableMeta;
if (!oldTableMeta) {
newTableMeta = {lastId: 1};
newTableMeta = { lastId: 1 };
await client.set(getTable(namespace, META_TABLE), tableName, newTableMeta);
} else {
// else, increment the lastId value
newTableMeta = {lastId: (oldTableMeta.lastId + 1)};
newTableMeta = { lastId: oldTableMeta.lastId + 1 };
await client.set(getTable(namespace, META_TABLE), tableName, newTableMeta);
}

Expand All @@ -28,27 +27,27 @@ export const create = async <T>(client: IClient, namespace: string, tableName: s
};
await client.set<T>(getTable(namespace, tableName), id, newValues);
return newValues;
}
};

export const update = async <T>(client: IClient, namespace: string, tableName: string, id: string, values: T) => {
const oldValues = await client.get<T>(getTable(namespace, tableName), id);
await client.set<T>(getTable(namespace, tableName), id, {
...oldValues,
values,
});
}
};

export const get = async <T>(client: IClient, namespace: string, tableName: string, id: string) => {
const values = await client.get<T>(getTable(namespace, tableName), id);
return values;
}
};

export const getIds = async (client: IClient, namespace: string, tableName: string) => {
const result = await client.getKeys(getTable(namespace, tableName));
return result;
}
};

export const getMultiple = async <T>(client: IClient, namespace: string, tableName: string, ids: string[]) => {
const result = await client.getMultiple<T>(getTable(namespace, tableName), ids);
return result;
}
};
4 changes: 2 additions & 2 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {default as Brokkr} from './brokkr';
export { default as Brokkr } from './brokkr';
export * from './entities';
export * from './clients';
export * from './clients';
12 changes: 6 additions & 6 deletions lib/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export interface IHashMap {
[key: string]: any,
[key: string]: any;
}

export interface ITableMeta {
lastId: number,
};
lastId: number;
}

export interface IWorker {
name: string,
run(args: any[], dependencyArgs?:IHashMap): void
}
name: string;
run(args: any[], dependencyArgs?: IHashMap): void;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brokkr",
"version": "0.1.0-prerelease",
"version": "0.1.0-prerelease-1",
"description": "Background job processing and orchestrator for Node",
"main": "lib/dist/index.js",
"directories": {
Expand Down

0 comments on commit 7cbb73d

Please sign in to comment.