Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] deno #224

Closed
melkir opened this issue Oct 3, 2021 · 4 comments
Closed

[feature request] deno #224

melkir opened this issue Oct 3, 2021 · 4 comments

Comments

@melkir
Copy link

melkir commented Oct 3, 2021

I would love to see this project ported to deno.
There's actually an existing project but having both project maintained at the same place seems to me a better choice for the long term.

Here's a ressource that might help https://medium.com/samsung-internet-dev/using-node-modules-in-deno-2885600ed7a9

@jbielick
Copy link
Owner

jbielick commented Nov 9, 2021

What would you need from this library to allow it to work in deno?

@melkir
Copy link
Author

melkir commented Jan 3, 2022

Hello @jbielick, regarding the latest compatibility from https://deno.land/std@0.119.0/node/README.md, it might work as it with the compat flag 😄

@jcs224
Copy link

jcs224 commented Feb 11, 2022

Great news, it seems to work in Deno now using the latest stuff!

std/node version: 0.125.0
Deno version: 1.18.2

Run this command for the job pusher: deno run --allow-read --allow-env --allow-net --unstable faktory.js

// faktory.js
import { createRequire } from "https://deno.land/std@0.125.0/node/module.ts";
const require = createRequire(import.meta.url);

const faktory = require('faktory-worker');

try {
  const client = await faktory.connect();
  await client.job("ResizeImage", { id: 333, size: "thumb" }).push();
  await client.close(); // reuse client if possible! remember to disconnect!
} catch (e) {
  console.error(e);
}

console.log('finished');

Run this for the worker: deno run --allow-read --allow-env --allow-net --unstable faktory-worker.js
(delayed by one second to show that promises work)

//faktory-worker.js
import { createRequire } from "https://deno.land/std@0.125.0/node/module.ts";
const require = createRequire(import.meta.url);

const faktory = require('faktory-worker');

faktory.register("ResizeImage", async ({ id, size }) => {
  await new Promise((resolve) => {
    setTimeout(() => {
      console.log('waiting for id of ',id,' and size ',size);
      resolve();
    }, 1000);
  });
});

faktory.work().catch(error => {
  console.error(`worker failed to start: ${error}`);
  process.exit(1);
});

jbielick it looks like there is enough Node compatibility to finally make this thing work! It would be cool to consider it a supported platform, and I will deprecate my own Deno-specific library. It is not nearly as feature-rich or stable as this project 😅

@melkir
Copy link
Author

melkir commented Nov 1, 2022

Here's what I would recommend for Deno on latest versions.

// producer.ts
import { Client as FaktoryClient } from "npm:faktory-worker";

const client = new FaktoryClient();
await client.connect();

const job = client.job("ResizeImage", { id: 333, size: "thumb" });
await job.push();

await client.close()
// receiver.ts
import { Worker as FaktoryWorker } from "npm:faktory-worker";

const worker = new FaktoryWorker();

worker.register("ResizeImage", async ({ id, size }) => {
  const image = await Image.find(id);
  await image.resize(size);
});

try {
  await worker.work();
} catch (error: unknown) {
  console.error(`worker failed to start: ${error}`);
  Deno.exit(1);
}

If you need an example of usage, I have a project that I recently migrated to this dependency.
https://github.com/melkir/dsn-validator
⚠️ Please note that this code is still being tested, so please do not use it in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants