Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
EduSpider
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
viitto
EduSpider
Commits
27f3b25e
Commit
27f3b25e
authored
May 27, 2022
by
liudong1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
ef1fbd79
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
6 deletions
+104
-6
Startup.cs
EduSpider.WebApi/Startup.cs
+31
-6
TimerJobj.cs
EduSpider.WebApi/Timers/TimerJobj.cs
+73
-0
No files found.
EduSpider.WebApi/Startup.cs
View file @
27f3b25e
...
...
@@ -34,11 +34,7 @@ namespace EduSpider.WebApi
//ActoFac注入
services
.
Replace
(
ServiceDescriptor
.
Transient
<
IControllerActivator
,
ServiceBasedControllerActivator
>());
List
<
string
>
corsArray
=
new
()
{
"http://localhost:7100"
,
"http://localhost:7200"
,
};
List
<
string
>
corsArray
=
GetCorsArray
();
services
.
AddCors
(
options
=>
options
.
AddPolicy
(
"AllowCors"
,
policy
=>
policy
.
AllowAnyHeader
().
AllowAnyMethod
().
AllowCredentials
().
WithOrigins
(
corsArray
.
ToArray
())));
services
.
AddControllers
();
services
.
AddMvc
(
options
=>
...
...
@@ -56,8 +52,27 @@ namespace EduSpider.WebApi
}
/// <summary>
/// 获取跨域配置
/// </summary>
/// <returns></returns>
public
List
<
string
>
GetCorsArray
()
{
List
<
string
>
corsArray
=
new
()
{
"http://localhost:7100"
,
"http://localhost:7200"
,
};
var
ArrayStr
=
ConfigHelper
.
GetAppsettings
(
"corsArray"
);
if
(!
string
.
IsNullOrWhiteSpace
(
ArrayStr
))
{
corsArray
.
AddRange
(
JsonHelper
.
Deserialize
<
List
<
string
>>(
ArrayStr
));
}
return
corsArray
;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public
void
Configure
(
IApplicationBuilder
app
,
IWebHostEnvironment
env
)
public
void
Configure
(
IApplicationBuilder
app
,
IWebHostEnvironment
env
,
IHostApplicationLifetime
appLifetime
)
{
if
(
env
.
IsDevelopment
())
{
...
...
@@ -75,6 +90,16 @@ namespace EduSpider.WebApi
{
endpoints
.
MapControllers
();
});
appLifetime
.
ApplicationStarted
.
Register
(()
=>
{
Timers
.
TimerJobj
.
RunTimer
();
//网站启动完成执行
});
appLifetime
.
ApplicationStopped
.
Register
(()
=>
{
Timers
.
TimerJobj
.
RunStop
();
//网站停止完成执行
});
}
}
}
EduSpider.WebApi/Timers/TimerJobj.cs
0 → 100644
View file @
27f3b25e
using
System
;
using
System.IO
;
using
System.Threading
;
using
VTX.FW.Helper
;
namespace
EduSpider.WebApi.Timers
{
/// <summary>
/// 定时任务
/// </summary>
public
class
TimerJobj
{
static
System
.
Timers
.
Timer
timer1
;
//计时器
public
static
void
RunTimer
()
{
timer1
=
new
System
.
Timers
.
Timer
{
Interval
=
(
1000
*
60
)
*
(
60
*
3
)
//3小时执行一次
};
timer1
.
Elapsed
+=
new
System
.
Timers
.
ElapsedEventHandler
(
ClearFile
);
timer1
.
Enabled
=
true
;
}
public
static
void
RunStop
()
{
timer1
.
Enabled
=
false
;
}
/// <summary>
/// 防止重复提交
/// </summary>
private
static
int
inTimer
=
0
;
/// <summary>
/// 清理文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public
static
void
ClearFile
(
object
sender
,
System
.
Timers
.
ElapsedEventArgs
e
)
{
if
(
Interlocked
.
Exchange
(
ref
inTimer
,
1
)
==
0
)
{
string
rootBook
=
AppDomain
.
CurrentDomain
.
BaseDirectory
;
try
{
DirectoryInfo
dir
=
new
(
rootBook
+
"/upfile/temporary"
);
if
(
dir
!=
null
&&
dir
.
Exists
)
{
DirectoryInfo
[]
dirArr
=
dir
.
GetDirectories
();
foreach
(
DirectoryInfo
item
in
dirArr
)
{
if
(
item
.
CreationTime
<
DateTime
.
Now
.
AddDays
(-
1
))
item
.
Delete
(
true
);
}
foreach
(
FileInfo
fi
in
dir
.
GetFiles
())
{
if
(
fi
.
CreationTime
<
DateTime
.
Now
.
AddDays
(-
1
))
fi
.
Delete
();
}
}
}
catch
{
LogHelper
.
WriteInfo
(
"ClearFile"
,
"清理临时文件失败_ClearFile"
);
}
Interlocked
.
Exchange
(
ref
inTimer
,
0
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment