Commit 07107922 authored by Liuhaoyang's avatar Liuhaoyang

Refactor BootService

parent 88f766a6
......@@ -17,11 +17,13 @@
*/
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SkyWalking.Boot
{
public interface IBootService: IDisposable
public interface IBootService : IDisposable
{
void Init();
Task Initialize(CancellationToken token);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Config
{
public class CollectorConfig
{
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Config
{
......
......@@ -20,6 +20,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace SkyWalking.Boot
{
......@@ -30,6 +32,7 @@ namespace SkyWalking.Boot
public static ServiceManager Instance => _instance;
private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
private ServiceManager()
{
......@@ -52,16 +55,14 @@ namespace SkyWalking.Boot
return (T) GetService(typeof(T));
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Init()
public async Task Initialize()
{
var types = FindServiceTypes();
foreach (var type in types)
{
if (Activator.CreateInstance(type) is IBootService service)
{
service.Init();
await service.Initialize(_tokenSource.Token);
_services.Add(type, service);
}
}
......@@ -70,11 +71,13 @@ namespace SkyWalking.Boot
[MethodImpl(MethodImplOptions.Synchronized)]
public void Dispose()
{
_tokenSource.Cancel();
foreach (var item in _services.Values)
{
var service = item as IBootService;
service?.Dispose();
}
_tokenSource.Dispose();
}
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SkyWalking.Boot
{
public abstract class TimerService : IBootService
{
protected abstract TimeSpan Interval { get; }
private Task _task;
public virtual void Dispose()
{
}
public async Task Initialize(CancellationToken token)
{
await Initializing(token);
_task = Task.Factory.StartNew(async () =>
{
while (true)
{
await Execute(token);
await Task.Delay(Interval, token);
}
},
token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
protected virtual Task Initializing(CancellationToken token)
{
return Task.CompletedTask;
}
protected abstract Task Execute(CancellationToken token);
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using SkyWalking.Boot;
using SkyWalking.Config;
using SkyWalking.Context.Trace;
......@@ -178,10 +179,11 @@ namespace SkyWalking.Context
{
}
public void Init()
public Task Initialize(CancellationToken token)
{
TracingContext.ListenerManager.Add(this);
IgnoredTracerContext.ListenerManager.Add(this);
return Task.CompletedTask;
}
public void AfterFinish(ITracerContext tracerContext)
......
......@@ -16,6 +16,9 @@
*
*/
using System.Threading;
using System.Threading.Tasks;
namespace SkyWalking.Sampling
{
public class SamplingService : ISampler
......@@ -32,12 +35,11 @@ namespace SkyWalking.Sampling
public void Dispose()
{
throw new System.NotImplementedException();
}
public void Init()
public Task Initialize(CancellationToken token)
{
throw new System.NotImplementedException();
return Task.CompletedTask;
}
}
}
\ No newline at end of file
......@@ -14,4 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\SkyWalking.Abstractions\SkyWalking.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Remote" />
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment