I’m not familiar with the worker_threads npm library, but it almost certainly runs outside of the Meteor context, so that’s why Meteor is undefined. I don’t know what to suggest here.
Anyway, Meteor.users is a reference to a MongoDB collection, so the update call actually runs on the database server. If it takes awhile for the call to return that’s just Meteor waiting for the database command to finish. You can have this be an async call if need be.
FWIW I use Steve Jobs for background tasks, although in my case they’re rather lightweight. I assume they run on the same Meteor server thread but I’m not sure.
Also consider using an index if it still doesn’t run fast enough. Anything that takes more than milliseconds for a query is a badly written query. Use MongoDb’s own tool with the “explain” feature to see why the query runs slow and then try to optimize it.
This is just an example, my real script is way more complex and has way much processing, I showed this just for an example, I need to run this task in background
This is just an example, the real script has way much complexity and processing, there are many queries that runs one by one to get data, so I need this to run in background, so it doesnt effect my main method call performance
its like there is a main call that expects a response, but in between that there is a heavy recursive task running, which is causing a delay in response, so I need to run that task in background