-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamingJobOperatorService.cs
105 lines (96 loc) · 4.68 KB
/
StreamingJobOperatorService.cs
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
using System.Threading;
using System.Threading.Tasks;
using Akka.Util;
using Akka.Util.Extensions;
using Arcane.Operator.Configurations;
using Arcane.Operator.Extensions;
using Arcane.Operator.Models;
using Arcane.Operator.Models.StreamClass.Base;
using Arcane.Operator.Models.StreamDefinitions.Base;
using Arcane.Operator.Services.Base;
using k8s.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Snd.Sdk.Kubernetes;
using Snd.Sdk.Kubernetes.Base;
using Snd.Sdk.Tasks;
namespace Arcane.Operator.Services.Streams;
public class StreamingJobOperatorService : IStreamingJobOperatorService
{
private readonly StreamingJobOperatorServiceConfiguration configuration;
private readonly IKubeCluster kubernetesService;
private readonly ILogger<StreamingJobOperatorService> logger;
private readonly IStreamingJobTemplateRepository streamingJobTemplateRepository;
public StreamingJobOperatorService(
ILogger<StreamingJobOperatorService> logger,
IOptions<StreamingJobOperatorServiceConfiguration> configuration,
IKubeCluster kubernetesService,
IStreamingJobTemplateRepository streamingJobTemplateRepository)
{
this.logger = logger;
this.configuration = configuration.Value;
this.kubernetesService = kubernetesService;
this.streamingJobTemplateRepository = streamingJobTemplateRepository;
}
public string StreamJobNamespace => this.configuration.Namespace;
public Task<Option<V1Job>> GetStreamingJob(string streamId)
{
return this.kubernetesService.GetJob(streamId, this.StreamJobNamespace)
.TryMap(job => job.AsOption(), exception =>
{
this.logger.LogWarning(exception, "Streaming job {streamId} not found", streamId);
return Option<V1Job>.None;
});
}
public Task<Option<StreamOperatorResponse>> StartRegisteredStream(IStreamDefinition streamDefinition, bool isBackfilling,
IStreamClass streamClass)
{
var template = streamDefinition.GetJobTemplate(isBackfilling);
return this.streamingJobTemplateRepository
.GetStreamingJobTemplate(template.Kind, streamDefinition.Namespace(), template.Name)
.Map(jobTemplate =>
{
if (!jobTemplate.HasValue)
{
return Task.FromResult(StreamOperatorResponse.OperationFailed(streamDefinition.Metadata.Namespace(),
streamDefinition.Kind,
streamDefinition.StreamId,
$"Failed to find job template with kind {template.Kind} and name {template.Name}")
.AsOption());
}
var job = jobTemplate
.Value
.GetJob()
.WithStreamingJobLabels(streamDefinition.StreamId, isBackfilling, streamDefinition.Kind)
.WithStreamingJobAnnotations(streamDefinition.GetConfigurationChecksum())
.WithMetadataAnnotations(streamClass)
.WithCustomEnvironment(streamDefinition.ToV1EnvFromSources(streamClass))
.WithCustomEnvironment(streamDefinition.ToEnvironment(isBackfilling, streamClass))
.WithOwnerReference(streamDefinition)
.WithName(streamDefinition.StreamId);
this.logger.LogInformation("Starting a new stream job with an id {streamId}",
streamDefinition.StreamId);
return this.kubernetesService
.SendJob(job, streamDefinition.Metadata.Namespace(), CancellationToken.None)
.TryMap(
_ => isBackfilling
? StreamOperatorResponse.Reloading(streamDefinition.Metadata.Namespace(),
streamDefinition.Kind,
streamDefinition.StreamId)
: StreamOperatorResponse.Running(streamDefinition.Metadata.Namespace(),
streamDefinition.Kind,
streamDefinition.StreamId),
exception =>
{
this.logger.LogError(exception, "Failed to send job");
return Option<StreamOperatorResponse>.None;
});
})
.Flatten();
}
public Task<Option<StreamOperatorResponse>> DeleteJob(string kind, string streamId)
{
return this.kubernetesService.DeleteJob(streamId, this.StreamJobNamespace)
.Map(_ => StreamOperatorResponse.Suspended(this.StreamJobNamespace, kind, streamId).AsOption());
}
}