Co-authored-by: stas <statis@microsoft.com>
This commit is contained in:
Stas
2022-04-25 15:40:28 -07:00
committed by GitHub
parent 2d15489196
commit c6992698e5
5 changed files with 95 additions and 7 deletions

View File

@ -122,16 +122,37 @@ public enum ScalesetState
CreationFailed CreationFailed
} }
public enum JobState
{
Init,
Enabled,
Stopping,
Stopped
}
public static class JobStateHelper
{
private static readonly HashSet<JobState> _shuttingDown = new HashSet<JobState>(new[] { JobState.Stopping, JobState.Stopped });
private static readonly HashSet<JobState> _avaiable = new HashSet<JobState>(new[] { JobState.Init, JobState.Enabled });
private static readonly HashSet<JobState> _needsWork = new HashSet<JobState>(new[] { JobState.Init, JobState.Stopping });
public static IReadOnlySet<JobState> Available => _avaiable;
public static IReadOnlySet<JobState> NeedsWork => _needsWork;
public static IReadOnlySet<JobState> ShuttingDown => _shuttingDown;
}
public static class ScalesetStateHelper public static class ScalesetStateHelper
{ {
static ConcurrentDictionary<string, ScalesetState[]> _states = new ConcurrentDictionary<string, ScalesetState[]>(); static ConcurrentDictionary<string, ScalesetState[]> _states = new ConcurrentDictionary<string, ScalesetState[]>();
/// set of states that indicate the scaleset can be updated /// set of states that indicate the scaleset can be updated
public static ScalesetState[] CanUpdate() public static ScalesetState[] CanUpdate()
{ {
return return
_states.GetOrAdd("CanUpdate", k => new[]{ _states.GetOrAdd(nameof(CanUpdate), k => new[]{
ScalesetState.Running, ScalesetState.Running,
ScalesetState.Resize ScalesetState.Resize
}); });
@ -141,7 +162,7 @@ public static class ScalesetStateHelper
public static ScalesetState[] NeedsWork() public static ScalesetState[] NeedsWork()
{ {
return return
_states.GetOrAdd("CanUpdate", k => new[]{ _states.GetOrAdd(nameof(NeedsWork), k => new[]{
ScalesetState.Init, ScalesetState.Init,
ScalesetState.Setup, ScalesetState.Setup,
ScalesetState.Resize, ScalesetState.Resize,
@ -154,7 +175,7 @@ public static class ScalesetStateHelper
public static ScalesetState[] Available() public static ScalesetState[] Available()
{ {
return return
_states.GetOrAdd("CanUpdate", k => _states.GetOrAdd(nameof(Available), k =>
{ {
return return
new[]{ new[]{
@ -168,7 +189,7 @@ public static class ScalesetStateHelper
public static ScalesetState[] Resizing() public static ScalesetState[] Resizing()
{ {
return return
_states.GetOrAdd("CanDelete", k => _states.GetOrAdd(nameof(Resizing), k =>
{ {
return return
new[]{ new[]{
@ -222,7 +243,7 @@ public static class TaskStateHelper
public static TaskState[] Available() public static TaskState[] Available()
{ {
return return
_states.GetOrAdd("Available", k => _states.GetOrAdd(nameof(Available), k =>
{ {
return return
new[]{ new[]{

View File

@ -524,3 +524,27 @@ public record SecretData<T>(T Secret)
return "[REDACTED]"; return "[REDACTED]";
} }
} }
public record JobConfig(
string Project,
string Name,
string Build,
int Duration,
string? Logs
);
public record JobTaskInfo(
Guid TaskId,
TaskType Type,
TaskState State
);
public record Job(
[PartitionKey] Guid JobId,
JobState State,
JobConfig Config,
string? Error,
DateTimeOffset? EndTime,
List<JobTaskInfo>? TaskInfo,
UserInfo UserInfo
) : StatefulEntityBase<JobState>(State);

View File

@ -83,6 +83,7 @@ public class Program
.AddScoped<INotificationOperations, NotificationOperations>() .AddScoped<INotificationOperations, NotificationOperations>()
.AddScoped<IUserCredentials, UserCredentials>() .AddScoped<IUserCredentials, UserCredentials>()
.AddScoped<ISecretsOperations, SecretsOperations>() .AddScoped<ISecretsOperations, SecretsOperations>()
.AddScoped<IJobOperations, JobOperations>()
//Move out expensive resources into separate class, and add those as Singleton //Move out expensive resources into separate class, and add those as Singleton
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc. // ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.

View File

@ -0,0 +1,19 @@
using ApiService.OneFuzzLib.Orm;
namespace Microsoft.OneFuzz.Service;
public interface IJobOperations : IStatefulOrm<Job, JobState>
{
}
public class JobOperations : StatefulOrm<Job, JobState>, IJobOperations
{
public JobOperations(IStorage storage, ILogTracer log, IServiceConfig config)
: base(storage, log, config)
{
}
}

View File

@ -310,6 +310,21 @@ namespace Tests
) )
); );
} }
public static Gen<Job> Job()
{
return Arb.Generate<Tuple<Guid, JobState, JobConfig, string?, DateTimeOffset?, List<JobTaskInfo>?, UserInfo>>().Select(
arg => new Job(
JobId: arg.Item1,
State: arg.Item2,
Config: arg.Item3,
Error: arg.Item4,
EndTime: arg.Item5,
TaskInfo: arg.Item6,
UserInfo: arg.Item7
)
);
}
} }
public class OrmArb public class OrmArb
@ -403,6 +418,10 @@ namespace Tests
{ {
return Arb.From(OrmGenerators.WebhookMessageEventGrid()); return Arb.From(OrmGenerators.WebhookMessageEventGrid());
} }
public static Arbitrary<Job> Job()
{
return Arb.From(OrmGenerators.Job());
}
} }
@ -598,7 +617,11 @@ namespace Tests
} }
[Property]
public bool Job(Job j)
{
return Test(j);
}
/* /*
//Sample function on how repro a failing test run, using Replay //Sample function on how repro a failing test run, using Replay