Open Closed

App Service running multiple times inconstantly #6795


User avatar
0
garymedina@hotmail.com created

Hi,

I am having an issue where my app service is being run multiple times. It is inconsistently running 1 - 4 times on a single start. I need it to run one time

Here is the service -

using BridgeMlsPinApi;
using OrganizeRe.Crg.Listing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.Internal.Mappers;
using CsvHelper;
using OrganizeRe.Crg.TeamMembers;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Newtonsoft.Json;
using Volo.Abp.Application.Services;
using Microsoft.Extensions.Hosting;
using OrganizeRe.Crg.MlsPostLogs;

namespace OrganizeRe.Crg
{
    public class MlsActivityAppService : ApplicationService, IMlsActivityAppService
    {
        protected ITeamMemberRepository _teamMembersRepository;
        protected IMlsPostLogRepository _mlsPostLogRepository;
        protected MlsPostLogManager _mlsPostLogManager;

        public MlsActivityAppService(ITeamMemberRepository teamMembersRepository, 
            IMlsPostLogRepository mlsPostLogRepository, MlsPostLogManager mlsPostLogManager)
        {
            _teamMembersRepository = teamMembersRepository;
            _mlsPostLogRepository = mlsPostLogRepository;
            _mlsPostLogManager = mlsPostLogManager;
        }

        public async Task<string> ProcessActivityAsync()
        {
            MlsFetch mlsFetch = new MlsFetch("mls_key");

            List<TeamMember> teamMembers = await _teamMembersRepository.GetListAsync();
            teamMembers = teamMembers.Where(x => x.AutopostMlsSold == true).ToList();

            HashSet<ProcessDto> listingToProcess = new HashSet<ProcessDto>();


            //Get all the listings for the agents in list and add to process list
            foreach (var agent in teamMembers)
            {
                if (agent.MlsId != null)
                {
                    Debug.WriteLine("Processing listings for agent ID " + agent);

                    //Get listings for each agent from MLSPin
                    var listings = await mlsFetch.GetLIstingsByAgentAsync(agent.MlsId);

                    foreach (var listing in listings)
                    {
                        var listingExist = await _mlsPostLogRepository.FindAsync(e => e.MlsId ==  listing.ListingId
                            && e.MlsStatus == listing.MlsStatus);

                       
                        if (listingExist == null)
                        {
                            //filter out all but New and check if agent has New checked
                            if (listing.MlsStatus == "New" && agent.AutopostMlsNew)
                            {
                                Console.WriteLine(listing.ListingId + " | " + listing.MlsStatus);
                                listingToProcess.Add(new ProcessDto() { Agent = agent, Listing = listing });
                            }

                            //filter out all but UAG and check if agent has New checked
                            if (listing.MlsStatus == "Under Agreement" && agent.AutopostMlsSold)
                            {
                                Console.WriteLine(listing.ListingId + " | " + listing.MlsStatus);
                                listingToProcess.Add(new ProcessDto() { Agent = agent, Listing = listing });
                            }

                            //filter out all but Sold and check if agent has New checked
                            if (listing.MlsStatus == "Sold" && agent.AutopostMlsSold)
                            {
                                Console.WriteLine(listing.ListingId + " | " + listing.MlsStatus);
                                listingToProcess.Add(new ProcessDto() { Agent = agent, Listing = listing });
                            }
                        }
                    }
                }
            }

            // Process the list and create graphics
            var fileName = string.Empty;
            var gm = new GraphicsMillUtil();

            foreach (var listing in listingToProcess)
            {
                fileName = await gm.CreateSoldGraphics(listing);

                // invisible line break - 
                var postMessage = $"{listing.Listing.MlsStatus} by {listing.Agent.Name} \\u2063\\n" +
                                  $"{listing.Listing.StreetNumber} {listing.Listing.StreetName} \\u2063\\n" +
                                  $"{listing.Listing.City}, {listing.Listing.StateOrProvince} {listing.Listing.PostalCode} \\u2063\\n" +
                                  $"\\u2063\\n" +
                                  $"{listing.Listing.PublicRemarks} \\u2063\\n" +
                                  $"\\u2063\\n" +
                                  $"https://idx.mlspin.com/MLS.SocialMedia/Listing/{listing.Listing.ListingId}";

                // Post to social via AyrShare
                AyrshareService ayrshare = new AyrshareService("as_key");
                var results = ayrshare.CreatePost(postMessage, "https://crg.organizere.com/images/" + fileName, includeFacebook: true,
                    includeInstagram: true);


                // Insert into log
                var mlsPostLog = await _mlsPostLogManager.CreateAsync(
                    listing.Listing.ListingId, listing.Listing.MlsStatus
                );

            }

            Debug.WriteLine("------- End of Post Job ---------");
            return "Done";
        }


    }
}

-------- And starting with a simple api call but would like to execute with hangfire when this does not repeat ------

    [Route("/test")]
    public async Task Test()
    {
        await _testAppService.ProcessActivityAsync();
    }

---- And the hangfire job -----

using System.Threading.Tasks;
using Hangfire;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;

namespace OrganizeRe.Crg
{
    [Queue("alpha")]
    public class MlsActivityJob : AsyncBackgroundJob<string>, ITransientDependency
    {
        private readonly IMlsActivityAppService _mlsActivityAppService;

        public MlsActivityJob(IMlsActivityAppService mlsActivityAppService)
        {
            _mlsActivityAppService = mlsActivityAppService;
        }

        public override async Task ExecuteAsync(string args)
        {
            await _mlsActivityAppService.ProcessActivityAsync();
        }
    }
}

1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think this is because there are unfinished jobs. hangfire will try to retry the job. this is expected behavior

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38