Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/redis/redis.ts
Original file line number Diff line number Diff line change
@@ -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})
}
})();

@jamiri jamiri Dec 25, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this do the job? Since the parent function is already defined as async.

Suggested change
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) => {
console.log("publish ", data)
return pub.publish(channel, JSON.stringify(data));
}

export const addTaskToRedis = async (task:Task)=>{
return publish(task.name, task)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this module should not know anything about tasks. It should be concerned with saving and receiving data from Redis. We need another module/package to handle connecting to tasks. WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I should separate the concerns




2 changes: 1 addition & 1 deletion lib/tasks/task.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down