123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Newtonsoft.Json;
- using Quartz;
- using RestSharp;
- using SMP.Tool.TimingTask.Core;
- using SMP.Tool.TimingTask.Models;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SMP.Tool.TimingTask.Jobs
- {
- /// <summary>
- /// 轮询任务接口
- /// </summary>
- public class QueryWaitReadJob : IJob
- {
- public async Task Execute(IJobExecutionContext context)
- {
- int threadQty = 5;//每次最大取数50 线程5
- try
- {
- RestClient client = new RestClient(JobConfig.QueryWaitReadJobApi);
- RestRequest request = new RestRequest();
- var result = await client.ExecuteGetAsync<ApiJsonModel<IEnumerable<Finance_TimeTask>>>(request);
- if (result.Data != null && result.Data.data.Count() > 0)
- {
- ConcurrentQueue<Finance_TimeTask> queue = new ConcurrentQueue<Finance_TimeTask>(result.Data.data);
- List<Task> tasks = new List<Task>();
- TaskFactory tf = new TaskFactory();
- for (int i = 0; i < threadQty; i++)
- {
- tasks.Add(tf.StartNew(() =>
- {
- while (!queue.IsEmpty)
- {
- try
- {
- if (queue.TryDequeue(out var item))
- {
- //调用执行
- RestClient client = new RestClient(JobConfig.ExecuteTaskJobApi + $"?taskId={item.Id}&startTime={item.StartTime}&endTime={item.EndTime}");
- RestRequest request = new RestRequest();
- var result = client.ExecuteGetAsync(request).Result;
- //if (result.StatusCode == System.Net.HttpStatusCode.InternalServerError)
- //{
- //}
- //else
- //{
- //}
- }
- }
- catch (Exception ex)
- {
- }
- }
- }));
- }
- Task.WaitAll(tasks.ToArray());
- }
- Console.WriteLine($"QueryWaitReadJob: 调用成功!");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"QueryWaitReadJob:调用异常!" + ex.Message);
- }
- }
- }
- /// <summary>
- /// 定时创建任务
- /// </summary>
- public class CreatFinanceTaskJob : IJob
- {
- public async Task Execute(IJobExecutionContext context)
- {
- try
- {
- var time = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
- Finance_TimeTask model = new Finance_TimeTask()
- {
- StartTime = time,
- EndTime = time.AddDays(1),
- };
- RestClient client = new RestClient(JobConfig.CreateApi);
- var request = new RestRequest();
- request.AddBody(model);
- var result = await client.ExecutePostAsync(request);
- if (result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- Console.WriteLine($"CreatFinanceTaskJob: 调用成功!");
- }
- else
- {
- Console.WriteLine($"CreatFinanceTaskJob: 调用失败!");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"CreatFinanceTaskJob: 调用异常!" + ex.Message);
- }
- }
- }
- }
|