Comments (12)
You can use better-queue
to trigger those tasks, but perhaps you can keep the tasks in your own dictionary, for example:
const Queue = require('better-queue');
const tasks = {};
tasks['task-1'] = { ... };
const q = new Queue(function (id, cb) {
const task = tasks[id];
// Trigger some server procedure
cb();
});
q.push('task-1');
// Later in your server
server.handler('/', function () {
// Edit the task to mark it as complete
})
from better-queue.
One way you can do this is keep the cb in memory, for example:
const Queue = require('better-queue');
const cbs = {};
const q = new Queue(function (task, cb) {
const { id } = task;
cbs[id] = cb;
// Trigger some server procedure, eg:
socket.emit({ id, ... });
});
q.push({ id: 'my-id', ... });
socket.on('receive-mail', (data) => {
const { id, result } = data;
const cb = cbs[id];
cb(null, result);
cbs[id] = null; // Remove the cb to prevent memory leaks!
});
For distributed systems you would probably need to keep the status of your tasks via a database.
from better-queue.
There's currently no way to trigger these outside of the processing function. What would be the use case for this?
from better-queue.
I want to transfer files between two servers , I use socket.io stream for this purpose. When user needs to submit file I add the task to the queue, then after receiving custom event about (progress, finish ...) I want to trigger task's methods in queue.
Now I use the following code to access tasks:
myQueue._workers[taskId].progressBatch(uploadedBytes, fileSize,"uploading")
and
myQueue._workers[taskId].finishBatch("File was sent successfully")
from better-queue.
It's not recommended to use _ functions, as they might change when the implementation changes.
But if you must get it to work this way, you can access the taskId
s through this._taskIds
in the processing function. I would recommend you create your own ID/progress system as it sounds like your needs are quite specific to your socket implementation.
from better-queue.
Suppose I want to create my own array that contains all my tasks, what is the best way to always check if there are tasks in my array that need processing ? what is the technology used in your library?
I like your library but I think you need to add a way to access tasks from outside processing function.
from better-queue.
Suppose
socket.on('receive-mail', (data) => {
uploader._workers[data.taskId].finishBatch(`Was Sent successfully`);
});
How can I convert this part of code to edit the task to mark it as complete.
I know it is another question, but I want to know how can I use cb() inside processing function?
Thank you for help :)
from better-queue.
Well, I understand your concept but how can I return an error using callback cb(null, result)
to make processing function trigger a failed event?
And how can I solve the problem of progress event:
socket.on('progress-mail', (data) => {
uploader._workers[data.taskId].progressBatch(data.uploadedBytes, data.fileSize, "uploading");
});
from better-queue.
const Queue = require('better-queue');
const tasks= {};
const q = new Queue(function (task, cb) {
const { id } = task;
tasks[id] = task;
// Trigger some server procedure, eg:
socket.emit({ id, ... });
});
q.push({ id: 'my-id', ... });
socket.on('receive-mail', (data) => {
const { id, result } = data;
const task= tasks[id];
// and now use task method here
// task.progressTask() or task.finishTask() or task.failedTask()
});
What do you think about that ? Can I use this concept?
Thank you again.
from better-queue.
You could keep track of the context (this
) in the processing function... In the code you provided, instead of saving task
you could save this
.
The convention in NodeJS is that the first argument of a callback, such as cb(null, result)
is for an error, so if you wanted to trigger a failed event you would call cb(error)
.
from better-queue.
Thank you, yes it was a mistake in my code I should put (this) instead of task. I think it is better to put the task's methods in the documentation because (progressTask, finishTask and failedTask) is not listed in the documentation, I saw it in your library.
from better-queue.
It is here in advanced.
from better-queue.
Related Issues (20)
- What is the suggested highest value for concurrent option? HOT 2
- task deleted and re-added on maxTimeout HOT 1
- TypeScript Support HOT 2
- maxRetries is not applied for tasks in the store
- 🐛 Large data in the store caused the app to crash
- How to update file progress?
- How to edit file progress?
- batchSize bigger than 1 => "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined" HOT 1
- maxTimeout issue?
- Most recent version no longer works with PostgreSQL store?
- It is unclear in readme.md what time uint `getStats().average` is
- Why does `maxTimeout` have no effect on `getStats().average`? HOT 4
- in `on("task_failed", handler)` , when maxTimeout error is emitted, the `error` input must be an error object
- Add CI to GitHub repo HOT 1
- Oversight on task_progress event? HOT 5
- maxRetries is not honored when tasks with same id are pushed into the queue HOT 1
- Delete a task? HOT 3
- Task queue memory leak
- When retry, queue not processed in order HOT 3
- getRunningTask is not defined in the store interface HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from better-queue.