-
Notifications
You must be signed in to change notification settings - Fork 35
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
How to work with non-fiber asynchronous flows? #31
Comments
Can you post job documents history? Also, what version are you on? In the latest version - if your job is not resolved with this.success or this.failure, the queue will stop and post a message to console. |
Version: I'm not quite sure what I'm logging, |
My apologies - typo: JobsInternal.Utilities.collection.find().fetch() Can you try 2.0.1 and see what happens? Are you getting |
If you want to use Promises you'll need to wrap the job in a fiber-aware await. For example, this will resolve correctly as a success/failure: import { Jobs } from 'meteor/msavin:sjobs';
import { Promise } from 'meteor/promise';
import fetch from 'node-fetch'
function registerJob({ name, run }) {
Jobs.register({
[name](...parameters) {
/* Forward context/params to 'run' method - execute in a fiber-aware await to
allow regular async syntax in provided function */
Promise.await(run.apply(this, parameters));
},
});
}
registerJob({
name: 'queue',
async run() {
try {
console.log('Running queue');
const result = await fetch('https://reqres.in/api/users?page=2');
const json = await result.json();
console.info('got a result', json);
this.success(result);
} catch (error) {
console.warn('got a error', error);
this.failure(error);
}
},
});
Jobs.run('queue'); |
@ramijarrar thanks for helping out Btw - every job is automatically wrapped in |
Interesting stuff @ramijarrar. I've actually shifted to a different library (for other reasons), but hopefully this thread is helpful to others :) |
@jpsear can you share the reasons? I'm curious :) |
Yeah, of course. I went with Agenda.
|
Thanks for posting @ramijarrar, your sample helped me out after I had refactored some things with async await! |
@ramijarrar would this be easy to implement into package core? Here's what executes the jobs: |
I have implemented this (or something similar) but have not really looked at my code for a few weeks. I think I have "specific" things in my code that would need to be made more generic. However, I don't see any reason why it could not be implemented. Maybe as a separate function (like |
@gary-menzel might it be possible to automatically detect if a function a async, and treat it as such? https://stackoverflow.com/questions/38508420/how-to-know-if-a-function-is-async |
I haven't really tried to detect them. Generally, if a function supports both a "callback" style or a "promise" the presence/absence of the callback parameter is used to trigger either logic. I also tend to like the "explicit" way of doing things - so the name I suggested (or something similar) appeals to me from that perspective. There is also the Meteor.bindEnvironment to think about as well. That could potentially be another method ( The other possibility is that they are configuration flags in the While From my quick look at the article all the methods mentioned to detect the function as being |
@gary-menzel there must be a way to make the package smart enough to detect how to run the function |
I've got a backlog of work to do right at the the moment but I'll add "detecting a Promise" on the list there and see if I can come up with something. Currently, I am running my own copy of It might be a few weeks before I can get to it though. |
@gary-menzel how's your availability now? Something like this hit me this morning... Jobs.register({
"sendEmail": {
type: "async",
function: function () {
// ...
},
checkEvery: 10000 // checks every 10 seconds
moreSettings: "yay"
}
})
|
Closing this thread out. @ramijarrar thanks again for your tip, I added it to the new documentation |
Hi @msavin (Max) - sorry, I have just been swamped - lost our Junior Dev on my "day job" so didn't get a chance to respond. I think your suggested solution is the simplest way. I will need to look at how it fits in with what I have done already. I do think it would be possible to detect it - but I also think the explicit approach is equally valid. |
Hey,
Just bringing up this issue again, prior reference here: #16
Behaviour with asynchronous code doesn't seem to work for me. Take this simple example:
When I start my Meteor server, I then see:
... and so on... Until I stop the server. So it's stuck in a loop.
Pretty sure the
this
context is not an issue here — but I could be wrong!The text was updated successfully, but these errors were encountered: