|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+using System;
|
|
|
2
|
+using System.Collections.Generic;
|
|
|
3
|
+using System.Linq;
|
|
|
4
|
+using System.Threading.Tasks;
|
|
|
5
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
6
|
+using Microsoft.Extensions.Logging;
|
|
|
7
|
+using Hangfire;
|
|
|
8
|
+using System.Text;
|
|
|
9
|
+using System.IO;
|
|
|
10
|
+using System.Net;
|
|
|
11
|
+using System.Linq.Expressions;
|
|
|
12
|
+using System.Common;
|
|
|
13
|
+
|
|
|
14
|
+namespace TVShoppingCallCenter_ZLJ.Controllers.hangfire
|
|
|
15
|
+{
|
|
|
16
|
+ /// <summary>
|
|
|
17
|
+ /// 任务调度Hangfire
|
|
|
18
|
+ /// </summary>
|
|
|
19
|
+ [Produces("application/json")]
|
|
|
20
|
+ [Route("api/[controller]")]
|
|
|
21
|
+ public class HangfireController : BaseController
|
|
|
22
|
+ {
|
|
|
23
|
+ private readonly ILogger<HangfireController> _logger;
|
|
|
24
|
+
|
|
|
25
|
+ public HangfireController(
|
|
|
26
|
+ ILogger<HangfireController> logger
|
|
|
27
|
+ )
|
|
|
28
|
+ {
|
|
|
29
|
+ _logger = logger;
|
|
|
30
|
+
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+
|
|
|
34
|
+
|
|
|
35
|
+ #region 四个定时基础方法
|
|
|
36
|
+ /// <summary>
|
|
|
37
|
+ /// 基于队列的任务处理(Fire-and-forget jobs)
|
|
|
38
|
+ /// </summary>
|
|
|
39
|
+ /// <param name="str"></param>
|
|
|
40
|
+ /// <returns></returns>
|
|
|
41
|
+ public IActionResult EnqueueAsync(string str)
|
|
|
42
|
+ {
|
|
|
43
|
+ var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!"));
|
|
|
44
|
+ return Success($"基于队列的任务处理 Enqueue : jobId={jobId}");
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ /// <summary>
|
|
|
48
|
+ /// 延迟任务执行(Delayed jobs)
|
|
|
49
|
+ /// </summary>
|
|
|
50
|
+ /// <returns></returns>
|
|
|
51
|
+ public IActionResult Delayed()
|
|
|
52
|
+ {
|
|
|
53
|
+ var jobId = BackgroundJob.Schedule(() => Console.WriteLine("Delayed!"),
|
|
|
54
|
+ TimeSpan.FromSeconds(10));
|
|
|
55
|
+ return Success($"延迟任务执行 Delayed : jobId={jobId}");
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ /// <summary>
|
|
|
59
|
+ /// 定时任务执行(Recurring jobs)
|
|
|
60
|
+ /// </summary>
|
|
|
61
|
+ /// <returns></returns>
|
|
|
62
|
+ public IActionResult Recurring()
|
|
|
63
|
+ {
|
|
|
64
|
+ RecurringJob.AddOrUpdate(() => Console.Write("定时任务执行 Recurring "), "1/1 * * * *");
|
|
|
65
|
+ return Success($"定时任务执行 Recurring ");
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ /// <summary>
|
|
|
69
|
+ /// 延续性任务执行
|
|
|
70
|
+ /// </summary>
|
|
|
71
|
+ /// <param name="jobId"></param>
|
|
|
72
|
+ /// <returns></returns>
|
|
|
73
|
+ [Obsolete]
|
|
|
74
|
+ public IActionResult Continuations(string jobId)
|
|
|
75
|
+ {
|
|
|
76
|
+ BackgroundJob.ContinueWith(jobId, () => Console.WriteLine("Continuation!"));
|
|
|
77
|
+ return Success($"延续性任务执行 Continuation ");
|
|
|
78
|
+ }
|
|
|
79
|
+ #endregion
|
|
|
80
|
+
|
|
|
81
|
+
|
|
|
82
|
+
|
|
|
83
|
+
|
|
|
84
|
+ /// <summary>
|
|
|
85
|
+ /// 定时任务执行导出
|
|
|
86
|
+ /// </summary>
|
|
|
87
|
+ /// <returns></returns>
|
|
|
88
|
+ public IActionResult RecurringToExport()
|
|
|
89
|
+ {
|
|
|
90
|
+ RecurringJob.AddOrUpdate(() => HttpGetExport(), Cron.Minutely);
|
|
|
91
|
+ return Success($"定时任务执行导出 api/Values/Export/ ");
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ /// <summary>
|
|
|
95
|
+ /// 执行导出
|
|
|
96
|
+ /// </summary>
|
|
|
97
|
+ private void HttpGetExport()
|
|
|
98
|
+ {
|
|
|
99
|
+ try
|
|
|
100
|
+ {
|
|
|
101
|
+ //_logger.LogInformation("定时任务执行【导出】【开始】 api/Values/Export/");
|
|
|
102
|
+ //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8082/api/Values/Export?start=2017-12-08&end=2017-12-09");
|
|
|
103
|
+ //request.Method = "GET";
|
|
|
104
|
+ //request.ContentType = "text/html;charset=UTF-8";
|
|
|
105
|
+
|
|
|
106
|
+ //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
107
|
+ //Stream myResponseStream = response.GetResponseStream();
|
|
|
108
|
+ //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
|
|
|
109
|
+ //var result = myStreamReader.ReadToEnd();
|
|
|
110
|
+ //myStreamReader.Close();
|
|
|
111
|
+ //myResponseStream.Close();
|
|
|
112
|
+ //if (result != "定时导出Excel文件,并保存Excel文件信息到数据库中")
|
|
|
113
|
+ // throw new Exception("导出错误");
|
|
|
114
|
+ //_logger.LogInformation("定时任务执行【导出】【结束】 api/Values/Export/");
|
|
|
115
|
+ }
|
|
|
116
|
+ catch (Exception e)
|
|
|
117
|
+ {
|
|
|
118
|
+ //_logger.LogError(e, "定时任务执行【导出】【失败】 api/Values/Export/");
|
|
|
119
|
+ }
|
|
|
120
|
+ }
|
|
|
121
|
+ }
|
|
|
122
|
+}
|