forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.cs
More file actions
205 lines (180 loc) · 5.45 KB
/
Copy pathJob.cs
File metadata and controls
205 lines (180 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Threading;
using Autofac;
using SmartStore.Core.Domain.Tasks;
using SmartStore.Core.Infrastructure;
using SmartStore.Core.Logging;
namespace SmartStore.Services.Tasks
{
/// <summary>
/// Task
/// </summary>
/// <remarks>
/// Formerly <c>Task</c>. Had to rename to <c>Job</c> in order to prevent naming conflicts with <c>System.Threading.Tasks.Task</c>
/// </remarks>
public partial class Job
{
/// <summary>
/// Ctor for Task
/// </summary>
private Job()
{
this.Enabled = true;
}
/// <summary>
/// Ctor for Task
/// </summary>
/// <param name="task">Task </param>
public Job(ScheduleTask task)
{
this.Type = task.Type;
this.Enabled = task.Enabled;
this.StopOnError = task.StopOnError;
this.Name = task.Name;
this.LastError = task.LastError;
this.IsRunning = task.IsRunning; //task.LastStartUtc.GetValueOrDefault() > task.LastEndUtc.GetValueOrDefault();
}
private ITask CreateTask(ILifetimeScope scope)
{
ITask task = null;
if (this.Enabled)
{
var type2 = System.Type.GetType(this.Type);
if (type2 != null)
{
object instance;
if (!EngineContext.Current.ContainerManager.TryResolve(type2, scope, out instance))
{
// not resolved
instance = EngineContext.Current.ContainerManager.ResolveUnregistered(type2, scope);
}
task = instance as ITask;
}
}
return task;
}
/// <summary>
/// Executes the task
/// </summary>
/// <remarks>
/// The caller is responsible for disposing the lifetime scope
/// </remarks>
public void Execute(ILifetimeScope scope = null, bool throwOnError = false)
{
Execute(CancellationToken.None, scope, throwOnError);
}
/// <summary>
/// Executes the task
/// </summary>
/// <remarks>
/// The caller is responsible for disposing the lifetime scope
/// </remarks>
public void Execute(
CancellationToken cancellationToken,
ILifetimeScope scope = null,
bool throwOnError = false)
{
this.IsRunning = true;
var faulted = false;
scope = scope ?? EngineContext.Current.ContainerManager.Scope();
try
{
var task = this.CreateTask(scope);
if (task != null)
{
this.LastStartUtc = DateTime.UtcNow;
var scheduleTaskService = scope.Resolve<IScheduleTaskService>();
var scheduleTask = scheduleTaskService.GetTaskByType(this.Type);
if (scheduleTask != null)
{
//update appropriate datetime properties
scheduleTask.LastStartUtc = this.LastStartUtc;
scheduleTaskService.UpdateTask(scheduleTask);
}
//execute task
var ctx = new TaskExecutionContext
{
LifetimeScope = scope,
CancellationToken = cancellationToken
};
task.Execute(ctx);
ctx.CancellationToken.ThrowIfCancellationRequested();
this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow;
this.LastError = null;
}
else
{
faulted = true;
this.LastError = "Could not create task instance";
}
}
catch (Exception ex)
{
faulted = true;
this.Enabled = !this.StopOnError;
this.LastEndUtc = DateTime.UtcNow;
this.LastError = ex.Message.Truncate(995, "...");
//log error
var logger = scope.Resolve<ILogger>();
logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, ex.Message), ex);
if (throwOnError)
{
throw;
}
}
finally
{
var scheduleTaskService = scope.Resolve<IScheduleTaskService>();
var scheduleTask = scheduleTaskService.GetTaskByType(this.Type);
if (scheduleTask != null)
{
// update appropriate properties
scheduleTask.LastError = this.LastError;
scheduleTask.LastEndUtc = this.LastEndUtc;
if (!faulted)
{
scheduleTask.LastSuccessUtc = this.LastSuccessUtc;
}
scheduleTaskService.UpdateTask(scheduleTask);
}
this.IsRunning = false;
}
}
/// <summary>
/// A value indicating whether a task is running
/// </summary>
public bool IsRunning { get; private set; }
/// <summary>
/// Datetime of the last start
/// </summary>
public DateTime? LastStartUtc { get; private set; }
/// <summary>
/// Datetime of the last end
/// </summary>
public DateTime? LastEndUtc { get; private set; }
/// <summary>
/// Datetime of the last success
/// </summary>
public DateTime? LastSuccessUtc { get; private set; }
/// <summary>
/// Message of the last error
/// </summary>
public string LastError { get; private set; }
/// <summary>
/// A value indicating type of the task
/// </summary>
public string Type { get; private set; }
/// <summary>
/// A value indicating whether to stop task on error
/// </summary>
public bool StopOnError { get; private set; }
/// <summary>
/// Get the task name
/// </summary>
public string Name { get; private set; }
/// <summary>
/// A value indicating whether the task is enabled
/// </summary>
public bool Enabled { get; set; }
}
}