'HangFire running each job separately

I am using HangFire to run the job. I have a method that retrieve data from more than 50 sql servers. I want to use HangFire to run each location separately by LocationID, and if 1 location fails I want that job get running again for that location after x minutes. Also I want to see the log of the job on the HangFire Dashboard.

My job method :

    public void SyncCompInfo()
    {
        List<Location> locations;
        using (var db = new CompInfoDemoEntities())
        {
            locations = db.Locations.Where(x => x.IsActive).ToList();
        }
        foreach (var location in locations)
        {
            try
            {
                using (var _db = new CompInfoDemoEntities())
                {

                    _log.Info("Getting CompoInfo data from location: " + location.StoreName);
                    _syncLogService.Add("Getting CompoInfo data from location: " + location.StoreName);
                    var responses = new List<CompInfoResponse>();
                    var compInfos = _db.IMS_CompInfo.Where(x => x.LocationId == location.Id).ToList();
                    using (var cnn = new SqlConnection(location.ConnectionString))
                    {
                        var sql = "select * from IMS_CompInfo;";
                        var sqlCmd = new SqlCommand(sql, cnn);
                        cnn.Open();
                        using (SqlDataReader rdr = sqlCmd.ExecuteReader())
                        {
                            while (rdr.Read())
                            {
                                var item = new CompInfoResponse();
                                item.Id = int.Parse(rdr["Id"].ToString());
                                item.ClientID = rdr["ClientID"].ToString();
                                item.LicenceID = rdr["LicenceID"].ToString();
                                item.POSCode = rdr["POSCode"].ToString();
                                item.logiPOS_Version = rdr["logiPOS_Version"].ToString();
                                if (rdr["LastLoginDate"] != null)
                                {
                                    item.LastLoginDate = DateTime.Parse(rdr["LastLoginDate"].ToString());
                                }

                                item.ComputerName = rdr["ComputerName"].ToString();
                                if (rdr["BootTime"] != null)
                                {
                                    item.BootTime = DateTime.Parse(rdr["BootTime"].ToString());
                                }

                                item.Domain = rdr["Domain"].ToString();
                                item.Manufacturer = rdr["Manufacturer"].ToString();
                                item.Model = rdr["Model"].ToString();
                                item.Memory = rdr["Memory"].ToString();
                                item.OS = rdr["OS"].ToString();
                                item.Build = rdr["Build"].ToString();
                                item.CPU = rdr["CPU"].ToString();
                                item.ProcArchitecture = rdr["ProcArchitecture"].ToString();
                                item.IP1 = rdr["IP1"].ToString();
                                item.MAC1 = rdr["MAC1"].ToString();
                                if (rdr["LastModifiedDate"] != null)
                                {
                                    item.LastModifiedDate = DateTime.Parse(rdr["LastModifiedDate"].ToString());
                                }

                                if (rdr["Tag"] != null)
                                {
                                    item.Tag = int.Parse(rdr["Tag"].ToString());
                                }

                                item.Application = rdr["Application"].ToString();
                                responses.Add(item);
                            }
                        }
                    }


Solution 1:[1]

What you seem to need is something like this, with a method you call upon startup, which loops other the locations and enqueues a job for each location. I over simplified the thing (for example making the methods static), but I guess most of the idea is there.

Have a look at Hangfire recurring tasks I guess it may be better suited to your needs than tasks fired upon application startups.

public void CalledUponStartup()
{
    List<Location> locations;
    using (var db = new CompInfoDemoEntities())
    {
        locations = db.Locations.Where(x => x.IsActive).ToList();
    }
    foreach (var location in locations)
    {
      BackgroundJob.Enqueue(() => SyncCompInfo(location.Id));
    }       
}

public static void SyncCompInfo(int locationId)
{
        try
        {
            using (var _db = new CompInfoDemoEntities())
            {
                 var location = db.Locations.FirstOrDefault(x => x.Id == locationId);

                _log.Info("Getting CompoInfo data from location: " + location.StoreName);
                _syncLogService.Add("Getting CompoInfo data from location: " + location.StoreName);
                var responses = new List<CompInfoResponse>();
                var compInfos = _db.IMS_CompInfo.Where(x => x.LocationId == location.Id).ToList();
                using (var cnn = new SqlConnection(location.ConnectionString))
                {
                    var sql = "select * from IMS_CompInfo;";
                    var sqlCmd = new SqlCommand(sql, cnn);
                    cnn.Open();
                    using (SqlDataReader rdr = sqlCmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            var item = new CompInfoResponse();
                            item.Id = int.Parse(rdr["Id"].ToString());
                            item.ClientID = rdr["ClientID"].ToString();
                            item.LicenceID = rdr["LicenceID"].ToString();
                            item.POSCode = rdr["POSCode"].ToString();
                            item.logiPOS_Version = rdr["logiPOS_Version"].ToString();
                            if (rdr["LastLoginDate"] != null)
                            {
                                item.LastLoginDate = DateTime.Parse(rdr["LastLoginDate"].ToString());
                            }

                            item.ComputerName = rdr["ComputerName"].ToString();
                            if (rdr["BootTime"] != null)
                            {
                                item.BootTime = DateTime.Parse(rdr["BootTime"].ToString());
                            }

                            item.Domain = rdr["Domain"].ToString();
                            item.Manufacturer = rdr["Manufacturer"].ToString();
                            item.Model = rdr["Model"].ToString();
                            item.Memory = rdr["Memory"].ToString();
                            item.OS = rdr["OS"].ToString();
                            item.Build = rdr["Build"].ToString();
                            item.CPU = rdr["CPU"].ToString();
                            item.ProcArchitecture = rdr["ProcArchitecture"].ToString();
                            item.IP1 = rdr["IP1"].ToString();
                            item.MAC1 = rdr["MAC1"].ToString();
                            if (rdr["LastModifiedDate"] != null)
                            {
                                item.LastModifiedDate = DateTime.Parse(rdr["LastModifiedDate"].ToString());
                            }

                            if (rdr["Tag"] != null)
                            {
                                item.Tag = int.Parse(rdr["Tag"].ToString());
                            }

                            item.Application = rdr["Application"].ToString();
                            responses.Add(item);
                        }
                }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jbl