my case similar the example, a user presses a 'report' button to start a long-running reporting job. You add this job to the queue and send the report's result to your user via email when it's completed. But, I don't need retries job execution when the job failed. I want job stop and send notification. And can cancel on service.
public void CancelAllMyJob()
{
var jobs = backgroundJobStore.GetWaitingJobsAsync(int.MaxValue).Result.Where(o => o.JobType.Contains(nameof(MyJob))).ToList();
if (jobs.Count > 0)
{
foreach (BackgroundJobInfo job in jobs)
{
backgroundJobStore.DeleteAsync(job).Wait();
}
}
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
var id = await backgroundJobManager.EnqueueAsync<MyJob, MyJobArgs >(args);
(await backgroundJobStore.GetAsync(Convert.ToInt64(id))).TryCount = 0;
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
await Task.Run(() =>
{
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
myJob.Execute(args);
}
});
}
It's work, thanks!
Related
I am using a JobConsumer to select a batch of items to be processed in a different process. For each item to process a new message is publish. I am using an inmemoryoutbox but the messages are published directly to rabbitmq. Actually I want all these messages to be collected in a memoryoutbox and all send after the loop has succeeded.
When I debug my code the PublishEndpointProvider has a Masstransit.Middleware.InMemoryOutbix.InMemoryOutboxPublishEndpointProvider. But the messages are send directly to RabbitMq.
I am using Masstransit version 8.0.1.
Any idea what is missing in the configuration?
configuration
services.AddMassTransit(mt =>
{
mt.AddDelayedMessageScheduler();
mt.AddConsumersFromNamespaceContaining<RunConsumer>();
mt.UsingRabbitMq((context, cfg) =>
{
cfg.UseDelayedMessageScheduler();
cfg.Host(rabbitMqConfig.Host, host =>
{
host.Username(rabbitMqConfig.Username);
host.Password(rabbitMqConfig.Password);
host.Heartbeat(rabbitMqConfig.Heartbeat);
});
cfg.UseGlobalRetryPolicy();
cfg.UseInMemoryOutbox();
var options = new ServiceInstanceOptions()
.EnableJobServiceEndpoints();
cfg.ServiceInstance(options, instance =>
{
instance.ConfigureJobServiceEndpoints(x =>
{
x.JobServiceStateEndpointName = "JobType";
x.JobServiceJobAttemptStateEndpointName = "JobAttempt";
x.JobServiceJobStateEndpointName = "Job";
});
instance.ConfigureEndpoints(context);
});
});
});
services.AddHostedService<MassTransitConsoleHostedService>();
return services;
Consumer definition
public class RunConsumerDefinition :
ConsumerDefinition<RunConsumer>
{
private IBusRegistrationContext context;
public ProlongeerPeriodeConsumerDefinition(IBusRegistrationContext context)
{
this.context = context;
EndpointName = 'MyQueue';
ConcurrentMessageLimit = 1;
}
protected override void ConfigureConsumer(
IReceiveEndpointConfigurator endpointConfigurator,
IConsumerConfigurator<RunConsumer> consumerConfigurator)
{
consumerConfigurator.Options<JobOptions<RunConsumer>>(options => options
.SetJobTimeout(TimeSpan.FromMinutes(15))
.SetConcurrentJobLimit(2));
endpointConfigurator.UseMessageScope(context);
endpointConfigurator.UseInMemoryOutbox();
}
}
RunConsumer
public async Task Run(JobContext<Run> context)
{
foreach (var index in Enumerable.Range(1, 7))
{
var command = new ProcessCommand();
await context.Publish<ProcessCommand>(command, context.CancellationToken);
}
}
Job Consumers don't technically run as a consumer, they're run separately with a JobContext<T>. As such, they don't use any of the retry, outbox, or other filters from the pipeline.
I have inserted connectionstring in "[AbpTenantConnectionStrings]" table for same tenant id.
My objective is to create both the DB and migrage the DB
DB
while running below code only "Default Db is creating", can anyone let me know how to create and migrate other DB as well. what changes need to be done in the below code.
var tenants = await _tenantRepository.GetListAsync(includeDetails: true);
var migratedDatabaseSchemas = new HashSet<string>();
foreach (var tenant in tenants)
{
using (_currentTenant.Change(tenant.Id))
{
if (tenant.ConnectionStrings.Any())
{
var tenantConnectionStrings = tenant.ConnectionStrings
.Select(x => x.Value)
.ToList();
if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
{
await MigrateDatabaseSchemaAsync(tenant);
migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
}
}
await SeedDataAsync(tenant);
}
After the below changes able to migrate or create the Db based on the connection-string inserted in "AbpTenantConnectionStrings":
public async Task MigrateAsync()
{
var initialMigrationAdded = AddInitialMigrationIfNotExist();
//var str = (await _connectionResolver.ResolveAsync("Default1"));
if (initialMigrationAdded)
{
return;
}
Logger.LogInformation("Started database migrations...");
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
Logger.LogInformation($"Successfully completed host database migrations.");
var tenants = await _tenantRepository.GetListAsync(includeDetails: true);
var migratedDatabaseSchemas = new HashSet<string>();
foreach (var tenant in tenants)
{
using (_currentTenant.Change(tenant.Id))
{
if (tenant.ConnectionStrings.Any())
{
var tenantConnectionStrings = tenant.ConnectionStrings
.Select(x => x.Value)
.ToList();
tenantConnectionStrings.ForEach(async s =>
{
await MigrateDatabaseSchemaAsync(s);
// migratedDatabaseSchemas.AddIfNotContains(s);
});
//if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
//{
// await MigrateDatabaseSchemaAsync(tenant);
// migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
//}
}
await SeedDataAsync(tenant);
}
Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
}
Logger.LogInformation("Successfully completed all database migrations.");
Logger.LogInformation("You can safely end this process...");
}
private async Task MigrateDatabaseSchemaAsync(string str)
{
//Logger.LogInformation(
// $"Migrating schema for {(tenant == null ? "host" : tenant.Name + " tenant")} database...");
foreach (var migrator in _dbSchemaMigrators)
{
await migrator.MigrateAsync(str);
}
}
public interface IBookStoreDbSchemaMigrator
{
Task MigrateAsync();
Task MigrateAsync(string con);
}
public class EntityFrameworkCoreBookStoreDbSchemaMigrator
: IBookStoreDbSchemaMigrator, ITransientDependency
{
private readonly IServiceProvider _serviceProvider;
public EntityFrameworkCoreBookStoreDbSchemaMigrator(
IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task MigrateAsync()
{
/* We intentionally resolving the BookStoreMigrationsDbContext
* from IServiceProvider (instead of directly injecting it)
* to properly get the connection string of the current tenant in the
* current scope.
*/
await _serviceProvider.GetRequiredService<BookStoreMigrationsDbContext>()
.Database
.MigrateAsync();
}
public async Task MigrateAsync(string con)
{
var context = _serviceProvider
.GetRequiredService<BookStoreMigrationsDbContext>();
context.Database.SetConnectionString(con);
context.Database.Migrate();
}
}
Let me know if anyone has suggestions or a better approach.
I have a bot where I am doing something like this:
1) A new Activity (message) arrives.
2) I dispatch the message to a RootDialog.
3) Depending on some Logic, RootDialog can:
a) Call a LuisDialog (handling natural language)
b) Call a CustomDialog (handles some business logic).
But when the user state is reset, and the flow leads to an intention inside the LuisDialog, it calls the intention method twice. Just the first time the state is empty, then it works fine.
Let me show you the code:
MessagesController:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
try
{
await Conversation.SendAsync(activity, () => new RootDialog());
}
catch (HttpRequestException e)
{
...
}
}
RootDialog:
public class RootDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await MessageReceivedAsync(context, Awaitable.FromItem(context.Activity.AsMessageActivity()));
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> awaitable)
{
bool value = DoSomeCustomLogic();
if (value)
{
string message = DoSomething();
await context.PostAsync(message);
} else {
bool value2 = DoSomeCustomLogic2();
if (value2)
{
var answerValidationDialog = new ValidateAnswerWithUserDialog();
context.Call(answerValidationDialog, ValidateAnswerWithUserDialogCompletedCallback);
} else {
var luisDialog = new LuisDialog();
await context.Forward(luisDialog,LuisDialogCompletedCallback, context.Activity, CancellationToken.None);
}
}
}
Callbacks only do context.Done(true);
And LuisDialog has an Intention which goes like this:
[LuisIntent(LuisUtils.INTENT_MENU_SALUTE)]
public async Task SaluteOrMenu(IDialogContext context, LuisResult result)
{
if (LuisUtils.IntentScoreIsHighEnough(result))
{
string userName = context.Activity.From.Name;
ContextHelper helper = new ContextHelper(MessageReceived);
await helper.AskUserToDoSomeInitialAction(context, saluteWord, userName);
context.Done(true);
}
else
{
await None(context, result);
}
}
And finally class ContextHelper:
public class ContextHelper
{
private Func<IDialogContext, IAwaitable<IMessageActivity>, Task> MessageReceived;
public ContextHelper(Func<IDialogContext, IAwaitable<IMessageActivity>, Task> messageReceived)
{
MessageReceived = messageReceived;
}
public async Task AskUserToDoSomeInitialAction(IDialogContext context, string saluteWord, string userName)
{
var reply = context.MakeMessage();
List<CardAction> buttons = BuildInitialOptionActions();
List<CardImage> images = BuildInitialOptionImages();
string initialText = $"Hi stranger!"
var card = new HeroCard
{
Title = "Hello!"
Text = initialText,
Buttons = buttons,
Images = images
};
reply.Attachments = new List<Attachment> { card.ToAttachment() };
await context.PostAsync(reply);
context.Wait(AfterUserChoseOptionInSalute);
}
private async Task AfterUserChoseOptionInSalute(IDialogContext context, IAwaitable<IMessageActivity> result)
{
await ReDispatchMessageReceivedToDialog(context);
}
private async Task ReDispatchMessageReceivedToDialog(IDialogContext context)
{
await MessageReceived(context, Awaitable.FromItem(context.Activity.AsMessageActivity()));
}
}
The SaluteOrMenu Intention gets called twice (only the first time I interact with the bot or when I delete the state. After Debugging I saw that after doing context.Wait(AfterUserChoseOptionInSalute);, the bot calls that function (instead of waiting for an event to call it)
Any ideas?
Thanks in advance.
I found the line that was wrong. It was on the first dialog (the RootDialog):
public async Task StartAsync(IDialogContext context)
{
await MessageReceivedAsync(context, Awaitable.FromItem(context.Activity.AsMessageActivity()));
}
That is a line that re dispatches a message with the incoming activity. I had it somewhere in some chunk of code and (don't know why), thought it was a good idea to use it on StartAsync. So, because of that, two calls were occurring.
Dumb me.
I just changed it to this and worked:
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
How to I make the call so that the GetRecords is completed before _reportViewerService.ShowReport starts. Using SignalR the setUi updates a txt field which displays names from part of the result calculated in getRecords, and the rest should be printed in report there after.
(whats happening now is both running the same time, then the report being showed before I see the live update status)
Thanks in advance
public async Task ViewReport()
{
var reportData = await _apiCallExecutor.ExecuteAsync(new GetRecords(queryModel, setUiHooks));
try
{
if (reportData.Count > 0)
{
var settings = new ReportSettings();
settings.ReportPath = "Utilities/SetDeliveryIdByBatchReport";
settings.ReportTitle = "Set Delivery ID By Batch - Exception Listing";
settings.DataSources.Add("DeliveryIdExceptionRecords", reportData);
ReportStatus = "Printing Exception Report...";
await _reportViewerService.ShowReport(settings);
}
}
finally
{
ViewModelState = ViewModelStates.Edit;
}
ReportStatus = "Done...";
}
You want to use some kind of "signal" from GetRecords, e.g., an IObservable or Task that completes when the data has arrived.
class GetRecords
{
...
public Task Done { get; }
// or: public IObservable<Unit> Done { get; }
}
then:
var getRecords = new GetRecords(queryModel, setUiHooks);
var reportData = await _apiCallExecutor.ExecuteAsync(getRecords);
await getRecords.Done;
...
I'm using parse for sending push notification and its working perfectly but, in the background i.e., when the application is not running then the code for registering the background service is executed but, no task is set and the background task is not executed after receiving the notification. Please help!!
Here is the code for registering the background task
Debug.WriteLine("Registering task");
var taskRegistered = false;
var exampleTaskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
task.Value.Unregister(true);
break;
}
}
await BackgroundExecutionManager.RequestAccessAsync();
if (!taskRegistered)
{
Debug.WriteLine("Registering task inside");
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "Tasks.BackgroundTask";
builder.Name = "PushNotification";
builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());
BackgroundTaskRegistration task = builder.Register();
await BackgroundExecutionManager.RequestAccessAsync();
}
Code that is executed in the background:
public async void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("1");
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
string content = notification.Content;
Debug.WriteLine("2");
_deferral.Complete();
}