Comments (7)
I created this solution for now which appears to work, by adding the scoped using as part of the task it builds up a valid scope that is in the context of the call:
_queue.QueueAsyncTask(async () =>
{
using (var scope = _scopeFactory.CreateScope())
{
var task = scope.ServiceProvider.GetService<MigrationCreateTask>() as MigrationCreateTask;
task.Initialise(request.Name, request.EnvironmentUrl, request.WhitelistPrefix);
await task.Invoke();
}
});
Thoughts on this solution? Is this what you would recommend?
It just means there is no way to use the other methods on queue like
_queue.QueueInvocable<MigrationCreateTask>();
As this would fail both because the scope would not be valid and I can't call Initialise .
from coravel.
Hmmm, are your invocables configured as scoped or transient in the DI config?
from coravel.
Using _queue.QueueInvocable<MigrationCreateTask>();
, under the covers, does something akin to your temporary solution. At face value, I'm not sure what's going on. I'll see if I can try to replicate something like this and do a little test.
from coravel.
The invocable is transient. Without the scope of the controller request, I see the need for the new scope surrounding the task. Since DbContexts normally use the scoped lifetime, since the controller request exits with an OK response, the scope will be disposed and since the task is queue for 30 seconds before being started, then the DbContext will be disposed too.
That is my assumption anyway, I could be wrong hence the question.
Full code for the controller method. Code was confirmed with your QueueWithCancellationTokens sample.
[HttpPost]
public IActionResult CreateMigration([FromBody]MigrationCreateRequest request)
{
if (request == null)
return BadRequest($"Missing the create {DisplayName} request body.");
var newGuid = Guid.NewGuid();
var newToken = new CancellationTokenSource();
_tokens.TryAdd(newGuid, newToken);
_queue.QueueAsyncTask(async () =>
{
using (var scope = _scopeFactory.CreateScope())
{
var invocable = scope.ServiceProvider.GetService<MigrationCreateTask>() as MigrationCreateTask;
invocable.Initialise(request.Name, request.EnvironmentUrl, request.WhitelistPrefix);
await invocable.Invoke();
}
});
return Json(newGuid);
}
I'm actually happy for this to be closed as the code is working really well now.
from coravel.
Calling _queue.QueueInvocable<MigrationCreateTask>();
should work. Under the covers, that method will create a new scope so it's not tied to the HTTP request/scope at all....🤔
Also, if you do use QueueInvocable
then you won't be able to call the custom method you made invocable.Initialise()
. If that method is required (which surely seems it is) then manually creating the scope as you did will work.
However, in cases where you want to queue something in the background yet be able to pass certain parameters to the invocable, it might be worth looking at the event broadcasting feature of coravel.
You could create an event CreateMigration
or whatever with an event listener (instead of an invocable).
https://github.com/jamesmh/coravel/blob/master/Docs/Queuing.md#queue-event-broadcasting
from coravel.
I added an EF Core sample (really simple) to the repo: https://github.com/jamesmh/coravel/tree/master/Samples/EFCoreSample
It works as expected, so there must be something unique about your project that's causing the EF Core DB context to behave like it is?
from coravel.
Thank you. Like you said, "that method will create a new scope" is the key, because I can't use 'QueueInvocable' due to initialization then yes I had to add the scope myself.
I am looking into the event broadcasting as an alternate and it's looking very promising. Thanks for this suggestion. Learning someone else component is always better than writing your own.
from coravel.
Related Issues (20)
- Sample to show how to execute Task on demand in .NET core 7
- Using Dependency Injection with ScheduleAsync HOT 1
- Run Task One Another / Control Concurrency in Queues HOT 1
- Is Coravel good fit for Blazor Server? HOT 4
- Stop scheduled job HOT 2
- Cron Expressions that support Seconds
- Design/Run Time
- Add support for setting Sender in mailer
- Add support for adding attachments as LinkedResources in MailMessage HOT 1
- Dispatcher.Broadcast doesn't work correctly when my listener class implements more than one IListener<> interfaces HOT 1
- Worker Service with Scheduler doesn't execute IInvocable using EveryMinute().WeekDay() on a Friday. HOT 1
- Advanced cron expressions HOT 2
- Is it possible for a job (or something else) to know when the next execution is scheduled for? HOT 2
- Unwanted Clock Drift HOT 2
- Add throttling when queuing items
- Extend Coravel Schedule With CoravelJobAttrbutes
- Scheduler does not run until WEB API server receives its first HTTP Request HOT 7
- How to use Event Broadcasting in worker service
- Task, scheduled to run on the same second, are not run in order they were added. HOT 1
- Turn off logging "Coravel's scheduler is behind" HOT 2
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 coravel.