From 370181b2f68f48397dbc1e788fe8879664979ff2 Mon Sep 17 00:00:00 2001 From: Mohammad Ranbar Z Date: Sat, 26 Dec 2020 00:36:18 +0330 Subject: [PATCH 1/2] Add redis pub/sub to project --- lib/redis/redis.ts | 33 +++++++++++++++++++++++++++++++++ lib/tasks/task.ts | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/redis/redis.ts diff --git a/lib/redis/redis.ts b/lib/redis/redis.ts new file mode 100644 index 0000000..6834893 --- /dev/null +++ b/lib/redis/redis.ts @@ -0,0 +1,33 @@ +import {connect, RedisConnectOptions} from "https://deno.land/x/redis/mod.ts"; +import {Task} from "../tasks/task.ts"; + +let redis: any; +let pub :any; +let sub:any + +export const initRedis = async (options:RedisConnectOptions) => { + redis = await connect(options); + pub = await connect(options); + sub = await connect(options); +} + +export const subscribe = async (channel: string) => { + const subscribe = await sub.subscribe(channel); + await (async function () { + for await (const {channel, message} of subscribe.receive()) { + console.log({channel, message:message}) + } + })(); +} + +const publish = async (channel: string, data:any) => { + console.log("publish ", data) + return pub.publish(channel, JSON.stringify(data)); +} + +export const addTaskToRedis = async (task:Task)=>{ + return publish(task.name, task) +} + + + diff --git a/lib/tasks/task.ts b/lib/tasks/task.ts index e130553..74aecac 100644 --- a/lib/tasks/task.ts +++ b/lib/tasks/task.ts @@ -1,6 +1,6 @@ import { v4 } from "https://deno.land/std/uuid/mod.ts"; -type Task = { +export type Task = { id: string; name: string; data: string; From 57c3436c02333e16942007cb31de8d372d45d8cb Mon Sep 17 00:00:00 2001 From: Mohammad Ranbar Z Date: Mon, 28 Dec 2020 23:26:59 +0330 Subject: [PATCH 2/2] finalize redis --- lib/redis/redis.ts | 24 +++++++----------------- lib/redis/redis_test.ts | 17 +++++++++++++++++ lib/tasks/task.ts | 1 - lib/tasks/task_test.ts | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 lib/redis/redis_test.ts diff --git a/lib/redis/redis.ts b/lib/redis/redis.ts index 6834893..7c151fc 100644 --- a/lib/redis/redis.ts +++ b/lib/redis/redis.ts @@ -1,11 +1,10 @@ import {connect, RedisConnectOptions} from "https://deno.land/x/redis/mod.ts"; -import {Task} from "../tasks/task.ts"; let redis: any; -let pub :any; -let sub:any +let pub: any; +let sub: any -export const initRedis = async (options:RedisConnectOptions) => { +export const initRedis = async (options: RedisConnectOptions) => { redis = await connect(options); pub = await connect(options); sub = await connect(options); @@ -13,21 +12,12 @@ export const initRedis = async (options:RedisConnectOptions) => { export const subscribe = async (channel: string) => { const subscribe = await sub.subscribe(channel); - await (async function () { - for await (const {channel, message} of subscribe.receive()) { - console.log({channel, message:message}) - } - })(); + for await (const {channel, message} of subscribe.receive()) { + console.log({channel, message: message}) + } } -const publish = async (channel: string, data:any) => { +export const publish = async (channel: string, data: any) => { console.log("publish ", data) return pub.publish(channel, JSON.stringify(data)); } - -export const addTaskToRedis = async (task:Task)=>{ - return publish(task.name, task) -} - - - diff --git a/lib/redis/redis_test.ts b/lib/redis/redis_test.ts new file mode 100644 index 0000000..3fdb1ea --- /dev/null +++ b/lib/redis/redis_test.ts @@ -0,0 +1,17 @@ +import { + beforeAll, +} from "https://x.nest.land/hooked@0.1.0/mod.ts"; +import {assertEquals} from "https://deno.land/std@0.74.0/testing/asserts.ts"; + +import {initRedis} from "./redis.ts"; + +beforeAll(async () => { + await initRedis({ + hostname: 'localhost', + port: '6379', + }) +}); + +Deno.test("should pub/sub works", async () => { + //TODO I couldn't write test case for pub-sub +}); \ No newline at end of file diff --git a/lib/tasks/task.ts b/lib/tasks/task.ts index 74aecac..835177c 100644 --- a/lib/tasks/task.ts +++ b/lib/tasks/task.ts @@ -14,7 +14,6 @@ type Handler = (payload: Data) => void; const tasks: Map = new Map(); const handlers: Map = new Map(); -const results: Map = new Map(); export function cancel(taskId: string) { tasks.delete(taskId); diff --git a/lib/tasks/task_test.ts b/lib/tasks/task_test.ts index 22b4a68..565f429 100644 --- a/lib/tasks/task_test.ts +++ b/lib/tasks/task_test.ts @@ -2,7 +2,7 @@ import { assert, assertEquals } from "https://deno.land/std@0.74.0/testing/asser import { create, handle } from "./task.ts"; function delay(ms: number): Promise { - return new Promise((res) => setTimeout(res, ms)); + return new Promise((res:()=>any) => setTimeout(res, ms)); } Deno.test("It should add tasks", async () => {