Esempio n. 1
0
        public Task <string> IsScheduledJob(string ccid, int connectorId, ScheduleType scheduleType, int?customScheduleInMinutes = 1200)
        {
            string recurringJobId = string.Empty;

            if (!string.IsNullOrEmpty(ccid) && connectorId > 0)
            {
                try
                {
                    string cronExpr = string.Empty;
                    switch (scheduleType)
                    {
                    case ScheduleType.EVERY_15_MINS:
                        cronExpr = DedupCron.MinuteInterval(15);
                        break;

                    case ScheduleType.EVERY_60_MINS:
                        cronExpr = DedupCron.Hourly();
                        break;

                    case ScheduleType.CUSTOM:
                        cronExpr = DedupCron.MinuteInterval((int)customScheduleInMinutes);
                        break;

                    case ScheduleType.ONCE_DAILY:
                        cronExpr = DedupCron.Daily();
                        break;

                    case ScheduleType.TWICE_DAILY:
                        cronExpr = DedupCron.HourInterval(11);
                        break;

                    case ScheduleType.TWICE_WEEKLY:
                        cronExpr = DedupCron.DayInterval(2);
                        break;

                    case ScheduleType.ONCE_WEEKLY:
                        cronExpr = DedupCron.Weekly();
                        break;
                    }

                    //Create new connection to schedule database
                    using (ConnectionFactory connectionFactory = new ConnectionFactory(ConfigVars.Instance.hangfireConnectionString))
                    {
                        StringBuilder sb             = new StringBuilder();
                        string        hangFireSchema = ConfigVars.Instance.herokuAddonAppName;
                        sb.Append($"SELECT h1.* FROM \"{hangFireSchema}\".hash h1");
                        sb.Append($" JOIN (SELECT key FROM \"{hangFireSchema}\".hash WHERE value LIKE '%{ccid}%' AND (value::json->>'Arguments')::json->>1='{connectorId}' AND REPLACE((value::json->>'Arguments')::json->>2,'\"','')='{ccid}') h2 ON h1.key=h2.key");
                        sb.Append(" WHERE h1.field='CreatedAt' OR h1.field='Cron' OR (h1.field='LastJobId' AND h1.value<>'')");
                        var recrecurringJobs = connectionFactory.DbConnection.Query <dynamic>(sb.ToString()).ToArray();
                        if (recrecurringJobs != null)
                        {
                            //get scheduled jobId
                            if (recrecurringJobs.Where(p => p.field.Trim() == "Cron" && p.value.Trim() == cronExpr.Trim()).Count() > 0)
                            {
                                var jobIds = recrecurringJobs.Where(p => p.field.Trim() == "Cron" && p.value.Trim() == cronExpr.Trim()).Select(p => ((string)p.key).Trim()).ToArray();
                                recurringJobId = recrecurringJobs.Where(p => jobIds.Contains(((string)p.key).Trim()) && p.field.Trim() == "CreatedAt").OrderByDescending(p => Convert.ToDateTime(p.value.Trim())).FirstOrDefault().key.Trim();
                            }

                            List <string> deletedIds = new List <string>();
                            for (int i = 0; i < recrecurringJobs.Length; ++i)
                            {
                                if (!recurringJobId.Equals(recrecurringJobs[i].key.Trim(), StringComparison.OrdinalIgnoreCase))
                                {
                                    if (recrecurringJobs[i].field.Trim() == "LastJobId")
                                    {
                                        try
                                        {
                                            RecurringJob.RemoveIfExists(recrecurringJobs[i].value.Trim());
                                            Console.WriteLine("RecurringJob=>Expired {0}:{1} deleted", recrecurringJobs[i].key.Trim(), recrecurringJobs[i].value.Trim());
                                        }
                                        catch (Exception exception)
                                        {
                                            Console.WriteLine("Error: {0}", exception.Message);
                                        }

                                        try
                                        {
                                            BackgroundJob.Delete(recrecurringJobs[i].value.Trim());
                                            Console.WriteLine("BackgroundJob=>Expired {0}:{1} deleted", recrecurringJobs[i].key.Trim(), recrecurringJobs[i].value.Trim());
                                        }
                                        catch (Exception exception)
                                        {
                                            Console.WriteLine("Error: {0}", exception.Message);
                                        }
                                    }

                                    try
                                    {
                                        if (!deletedIds.Contains(recrecurringJobs[i].key.Trim()))
                                        {
                                            RecurringJob.RemoveIfExists(recrecurringJobs[i].key.Trim().Replace("recurring-job:", ""));
                                            Console.WriteLine("RecurringJob=>Expired {0} deleted", recrecurringJobs[i].key.Trim());
                                            deletedIds.Add(recrecurringJobs[i].key.Trim());
                                        }
                                    }
                                    catch (Exception exception)
                                    {
                                        Console.WriteLine("Error: {0}", exception.Message);
                                    }
                                }
                            }
                            deletedIds.Clear();
                            deletedIds = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                }
            }

            return(Task.FromResult(recurringJobId));
        }
Esempio n. 2
0
        /// <summary>
        /// Method: ScheduleJob
        /// Description: It is used to schedule background sync process for connector
        /// </summary>
        /// <param name="connector"></param>
        /// <param name="id"></param>
        /// <returns>status as int</returns>
        public async Task <int> ScheduleJob(string ccid, int id, ConnectorType connectorType, ScheduleType scheduleType, int?customScheduleInMinutes = 1200)
        {
            var syncStatus = 1;

            try
            {
                if (string.IsNullOrEmpty(ccid) || id == 0)
                {
                    return(0);
                }

                IJobCancellationToken token = JobCancellationToken.Null;
                //token = new JobCancellationToken(true);
                var jobKey = string.Empty;
                if (scheduleType != ScheduleType.MANUAL_SYNC)// && scheduleType != ScheduleType.STREAMING_SYNC)
                {
                    jobKey = Math.Abs(Guid.NewGuid().ToInt()).ToString();
                }
                if (ConnectorType.Azure_SQL == connectorType)
                {
                    //switch (scheduleType)
                    //{
                    //    case ScheduleType.EVERY_15_MINS:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.MinuteInterval(15), null, "critical");
                    //        break;
                    //    case ScheduleType.CUSTOM:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.MinuteInterval(customScheduleInMinutes), null, "critical");
                    //        break;
                    //    case ScheduleType.EVERY_60_MINS:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.Hourly, null, "critical");
                    //        break;
                    //    case ScheduleType.ONCE_DAILY:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.Daily, null, "critical");
                    //        break;
                    //    case ScheduleType.TWICE_DAILY:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.HourInterval(11), null, "critical");
                    //        break;
                    //    case ScheduleType.TWICE_WEEKLY:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, service => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.DayInterval(2), null, "critical");
                    //        break;
                    //    case ScheduleType.ONCE_WEEKLY:
                    //        RecurringJob.AddOrUpdate<DERepository>(jobKey, (service) => service.AddDataRowsToSqlConnector(token, id, ccid), Cron.Weekly, null, "critical");
                    //        break;
                    //    case ScheduleType.MANUAL_SYNC:
                    //        jobKey = BackgroundJob.Enqueue<DERepository>(service => service.AddDataRowsToSqlConnector(token, id, ccid));
                    //        break;
                    //    case ScheduleType.STREAMING_SYNC:
                    //        jobKey = string.Empty;
                    //        break;
                    //}
                }
                else
                {
                    switch (scheduleType)
                    {
                    case ScheduleType.EVERY_15_MINS:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.MinuteInterval(15), TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.CUSTOM:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.MinuteInterval((int)customScheduleInMinutes), TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.EVERY_60_MINS:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.Hourly, TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.ONCE_DAILY:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.Daily, TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.TWICE_DAILY:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.HourInterval(11), TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.TWICE_WEEKLY:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, service => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.DayInterval(2), TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.ONCE_WEEKLY:
                        RecurringJob.AddOrUpdate <ISyncRepository>(jobKey, (service) => service.DeDupRowsFromDatabaseTable(token, id, ccid), DedupCron.Weekly, TimeZoneInfo.Local, Constants.JOB_QUEUE_NAME);
                        break;

                    case ScheduleType.MANUAL_SYNC:
                        jobKey = BackgroundJob.Enqueue <ISyncRepository>(service => service.DeDupRowsFromDatabaseTable(token, id, ccid));
                        break;
                        //case ScheduleType.STREAMING_SYNC:
                        //    jobKey = string.Empty;
                        //    break;
                    }
                }

                if (!string.IsNullOrEmpty(jobKey))
                {
                    SyncRepository.UpdateSyncInfo(id: id, ccid: ccid, jobid: jobKey);
                }
            }
            catch (Exception ex)
            {
                syncStatus = 0;
                Console.WriteLine("Error: {0}", ex.Message);
            }
            return(await Task.FromResult(syncStatus));
        }