<commit_before>﻿#nullable enable

using System.Data.Entity;
using System.Web.Hosting;
using NLog;
using Snittlistan.Queue;
using Snittlistan.Queue.Messages;
using Snittlistan.Web.Infrastructure.Database;

namespace Snittlistan.Web.Infrastructure;

public class TaskPublisher
{
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
    private readonly Tenant currentTenant;
    private readonly Databases databases;
    private readonly Guid correlationId;
    private readonly Guid? causationId;

    public TaskPublisher(
        Tenant currentTenant,
        Databases databases,
        Guid correlationId,
        Guid? causationId)
    {
        this.currentTenant = currentTenant;
        this.databases = databases;
        this.correlationId = correlationId;
        this.causationId = causationId;
    }

    public void PublishTask(TaskBase task, string createdBy)
    {
        PublishedTask publishedTask = databases.Snittlistan.PublishedTasks.Add(
            PublishedTask.CreateImmediate(
                task,
                currentTenant.TenantId,
                correlationId,
                causationId,
                createdBy));

        try
        {
            HostingEnvironment.QueueBackgroundWorkItem(ct =>
                PublishMessage(currentTenant, publishedTask.MessageId, ct));
        }
        catch (Exception ex)
        {
            Logger.Error(
                ex,
                "QueueBackgroundWorkItem failed, using fallback (publish immediately)");
            DoPublishMessage(currentTenant, publishedTask);
        }

        static async void PublishMessage(Tenant tenant, Guid messageId, CancellationToken ct)
        {
            const int MaxTries = 10;
            using IDisposable logScope = NestedDiagnosticsLogicalContext.Push("QueueBackgroundWork");
            using SnittlistanContext context = new();
            try
            {
                for (int i = 0; i < MaxTries; i++)
                {<commit_msg>no msmq when testing<commit_after>﻿#nullable enable

using System.Data.Entity;
using System.Web.Hosting;
using NLog;
using Snittlistan.Queue;
using Snittlistan.Queue.Messages;
using Snittlistan.Web.Infrastructure.Database;

namespace Snittlistan.Web.Infrastructure;

public class TaskPublisher
{
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
    private readonly Tenant currentTenant;
    private readonly Databases databases;
    private readonly Guid correlationId;
    private readonly Guid? causationId;

    public TaskPublisher(
        Tenant currentTenant,
        Databases databases,
        Guid correlationId,
        Guid? causationId)
    {
        this.currentTenant = currentTenant;
        this.databases = databases;
        this.correlationId = correlationId;
        this.causationId = causationId;
    }

    public void PublishTask(TaskBase task, string createdBy)
    {
        PublishedTask publishedTask = databases.Snittlistan.PublishedTasks.Add(
            PublishedTask.CreateImmediate(
                task,
                currentTenant.TenantId,
                correlationId,
                causationId,
                createdBy));

        try
        {
            HostingEnvironment.QueueBackgroundWorkItem(ct =>
                PublishMessage(currentTenant, publishedTask.MessageId, ct));
        }
        catch (Exception ex)
        {
            Logger.Error(
                ex,
                "QueueBackgroundWorkItem failed, using fallback (publish immediately)");
            try
            {
                DoPublishMessage(currentTenant, publishedTask);
            }
            catch (Exception ex2)
            {
                Logger.Error(ex2);
            }
        }

        static async void PublishMessage(Tenant tenant, Guid messageId, CancellationToken ct)
        {
            const int MaxTries = 10;
            using IDisposable logScope = NestedDiagnosticsLogicalContext.Push("QueueBackgroundWork");
            using SnittlistanContext context = new();
            try
            {
                for (int i = 0; i < MaxTries; i++)
                {
{'fraction_non_alphanumeric': 0.06631427198462278, 'fraction_numerical': 0.0019221528111484864, 'mean_word_length': 2.0928677563150075, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4618734', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1023, 'n_words': 250}