public void TransferData()
 {
     try
     {
         List <User> data = _context.Users.ToList();
         for (int i = 0; i < data.Count; i++)
         {
             User user     = data[i];
             var  tempUser = _context.TempUsers.Where(x => x.Name == data[i].Name).FirstOrDefault();
             if (tempUser == null)
             {
                 TempUser temp = new TempUser()
                 {
                     Name    = data[i].Name,
                     Contect = data[i].Contect,
                     Date    = DateTime.Now
                 };
                 _context.TempUsers.Add(temp);
                 _context.SaveChanges();
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
        public Task Execute(IJobExecutionContext context)
        {
            User user = new User()
            {
                Name    = "Nitin",
                Contect = "5464964865",
                Date    = DateTime.Now,
            };

            _quartz.Users.Add(user);
            _quartz.SaveChanges();
            return(Task.CompletedTask);
        }
Beispiel #3
0
        public async Task Execute(IJobExecutionContext context)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            var url = context.JobDetail.JobDataMap.GetString(Constant.RequestUrl);

            url = url?.IndexOf("http") == 0 ? url : "http://" + url;
            var requestParameters = context.GetString(Constant.RequestParameters);
            var logs          = context.JobDetail.JobDataMap[Constant.LogList] as List <string> ?? new List <string>();
            var headersString = context.GetString(Constant.Headers);
            var headers       = headersString != null?JsonConvert.DeserializeObject <Dictionary <string, string> >(headersString?.Trim()) : null;

            var requestType = (HttpMethod)Enum.Parse(typeof(HttpMethod), context.GetString(Constant.RequestType));
            var loginfo     = new LogInfoModel
            {
                Url         = url,
                BeginTime   = DateTime.Now,
                RequestType = requestType,
                Parameters  = requestParameters,
                JobName     = $"{context.JobDetail.Key.Group}.{context.JobDetail.Key.Name}",
            };

            try
            {
                var http     = HttpHelper.Instance;
                var response = new HttpResponseMessage();
                switch (requestType)
                {
                case HttpMethod.Get:
                    response = await http.GetAsync(url, headers);

                    break;

                case HttpMethod.Post:
                    response = await http.PostAsync(url, requestParameters, headers);

                    break;

                case HttpMethod.Put:
                    response = await http.PutAsync(url, requestParameters, headers);

                    break;

                case HttpMethod.Delete:
                    response = await http.DeleteAsync(url, headers);

                    break;
                }

                var result = HttpUtility.HtmlEncode(await response.Content.ReadAsStringAsync());

                stopwatch.Stop();
                long seconds = stopwatch.Elapsed.Ticks;
                loginfo.EndTime = DateTime.Now;
                loginfo.Seconds = seconds;
                loginfo.Result  = result.PadLeft(200);
                if (!response.IsSuccessStatusCode)
                {
                    loginfo.ErrorMsg = result.PadLeft(200);
                    context.JobDetail.JobDataMap[Constant.Exception] = this.SerializeObject(loginfo);
                }
                else
                {
                    using (var dbContext = new QuartzContext())
                    {
                        dbContext.QuartzLog.Add(new QuartzLog()
                        {
                            CreateTime = DateTime.Now,
                            LogLevel   = LogLevel.Info,
                            ExcuteTime = new DateTime(stopwatch.Elapsed.Ticks),
                            Message    = result,
                            Name       = context.JobDetail.Key.Name,
                            Group      = context.JobDetail.Key.Group,
                        });
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                stopwatch.Stop();
                double seconds = stopwatch.Elapsed.TotalSeconds;
                loginfo.ErrorMsg = ex.Message + ex.StackTrace;
                context.JobDetail.JobDataMap[Constant.Exception] = this.SerializeObject(loginfo);
                loginfo.Seconds = seconds;
            }
            finally
            {
                logs.Add(this.SerializeObject(loginfo));
                context.JobDetail.JobDataMap[Constant.LogList] = logs;
            }
        }