Skip to content

Commit

Permalink
All of the reference stuff seems to be working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottArbeit committed Jul 3, 2024
1 parent 02eaf4a commit 95f8c54
Show file tree
Hide file tree
Showing 25 changed files with 535 additions and 286 deletions.
106 changes: 66 additions & 40 deletions src/Grace.Actors/Branch.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module Branch =
| Save -> { branchDto with SaveEnabled = true }
| Tag -> { branchDto with TagEnabled = true }
| External -> { branchDto with ExternalEnabled = true }
| Rebase -> branchDto // Rebase is always allowed.

branchDto
| Rebased referenceId -> { currentBranchDto with BasedOn = referenceId }
Expand Down Expand Up @@ -105,34 +106,53 @@ module Branch =
let stateManager = this.StateManager

task {
let mutable message = String.Empty
let! retrievedEvents = Storage.RetrieveState<List<BranchEvent>> stateManager eventsStateName

match retrievedEvents with
| Some retrievedEvents ->
// Load the branchEvents from the retrieved events.
branchEvents.AddRange(retrievedEvents)

// Apply all events to the branchDto.
branchDto <-
retrievedEvents
|> Seq.fold (fun branchDto branchEvent -> branchDto |> updateDto branchEvent.Event) BranchDto.Default
try
let mutable message = String.Empty
let! retrievedEvents = Storage.RetrieveState<List<BranchEvent>> stateManager eventsStateName

message <- "Retrieved from database"
| None -> message <- "Not found in database"
match retrievedEvents with
| Some retrievedEvents ->
// Load the branchEvents from the retrieved events.
branchEvents.AddRange(retrievedEvents)

let duration_ms = getPaddedDuration_ms activateStartTime
// Apply all events to the branchDto.
branchDto <-
retrievedEvents
|> Seq.fold (fun branchDto branchEvent -> branchDto |> updateDto branchEvent.Event) BranchDto.Default

// Get the latest references and update the dto.
let referenceTypes = [| Save; Checkpoint; Commit; Promotion |]
//logToConsole $"In Branch.Actor.OnActivateAsync: About to call getLatestReferenceByReferenceTypes()."
let! latestReferences = getLatestReferenceByReferenceTypes referenceTypes branchDto.BranchId
latestReferences |> Seq.iter (fun kvp ->
let referenceId = kvp.Value.ReferenceId
match kvp.Key with
| Save -> branchDto <- { branchDto with LatestSave = referenceId }
| Checkpoint -> branchDto <- { branchDto with LatestCheckpoint = referenceId }
| Commit -> branchDto <- { branchDto with LatestCommit = referenceId }
| Promotion -> branchDto <- { branchDto with LatestPromotion = referenceId }
| Rebase
| External
| Tag -> ()
)

log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; Activated {ActorType} {ActorId}. BranchName: {BranchName}; {message}.",
getCurrentInstantExtended (),
Environment.MachineName,
duration_ms,
actorName,
host.Id,
branchDto.BranchName,
message
)
message <- "Retrieved from database"
| None -> message <- "Not found in database"

let duration_ms = getPaddedDuration_ms activateStartTime

log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; Activated {ActorType} {ActorId}. BranchName: {BranchName}; {message}.",
getCurrentInstantExtended (),
getMachineName,
duration_ms,
actorName,
host.Id,
branchDto.BranchName,
message
)
with ex ->
log.LogError(ex, "Error in Branch.Actor.OnActivateAsync. BranchId: {BranchId}.", host.Id)
}
:> Task

Expand Down Expand Up @@ -177,7 +197,7 @@ module Branch =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; RepositoryId: {RepositoryId}; BranchId: {Id}; BranchName: {BranchName}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand All @@ -190,7 +210,7 @@ module Branch =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; Command: {Command}; RepositoryId: {RepositoryId}; BranchId: {Id}; BranchName: {BranchName}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand All @@ -215,15 +235,17 @@ module Branch =
branchDto <- branchDto |> updateDto branchEvent.Event

match branchEvent.Event with
| Assigned (_, _, _, _)
| Promoted (_, _, _, _)
| Committed (_, _, _, _)
| Checkpointed (_, _, _, _)
| Saved (_, _, _, _)
| Tagged (_, _, _, _)
| ExternalCreated (_, _, _, _) ->
| Assigned (referenceId, _, _, _)
| Promoted (referenceId, _, _, _)
| Committed (referenceId, _, _, _)
| Checkpointed (referenceId, _, _, _)
| Saved (referenceId, _, _, _)
| Tagged (referenceId, _, _, _)
| ExternalCreated (referenceId, _, _, _) ->
// Don't save these reference creation events, and don't send them as events; that was done by the Reference actor when the reference was created.
()
branchEvent.Metadata.Properties.Add(nameof (ReferenceId), $"{referenceId}")
| Rebased (referenceId) ->
branchEvent.Metadata.Properties.Add(nameof (ReferenceId), $"{referenceId}")
| _ ->
// For all other events, add the event to the branchEvents list, and save it to actor state.
branchEvents.Add(branchEvent)
Expand Down Expand Up @@ -251,7 +273,6 @@ module Branch =
return Ok returnValue
with ex ->
let exceptionResponse = createExceptionResponse ex

let graceError = GraceError.Create (BranchError.getErrorMessage FailedWhileApplyingEvent) branchEvent.Metadata.CorrelationId

graceError
Expand Down Expand Up @@ -322,8 +343,8 @@ module Branch =

let referenceActor = actorProxyFactory.CreateActorProxy<IReferenceActor>(actorId, Constants.ActorName.Reference)

let referenceCommand = Reference.ReferenceCommand.Create(referenceId, branchDto.RepositoryId, branchDto.BranchId, directoryId, sha256Hash, referenceType, referenceText)

let referenceDto = {ReferenceDto.Default with ReferenceId = referenceId; RepositoryId = branchDto.RepositoryId; BranchId = branchDto.BranchId; DirectoryId = directoryId; Sha256Hash = sha256Hash; ReferenceText = referenceText; ReferenceType = referenceType}
let referenceCommand = Reference.ReferenceCommand.Create referenceDto
match! referenceActor.Handle referenceCommand metadata with
| Ok _ -> return Ok referenceId
| Error error -> return Error error
Expand All @@ -344,11 +365,16 @@ module Branch =
)

return Ok (Created(branchId, branchName, parentBranchId, basedOn, repositoryId, branchPermissions))
| Rebase referenceId ->
| BranchCommand.Rebase referenceId ->
metadata.Properties.Add("BasedOn", $"{referenceId}")
metadata.Properties.Add(nameof (ReferenceId), $"{referenceId}")
metadata.Properties.Add(nameof (BranchId), $"{this.Id}")
metadata.Properties.Add(nameof (BranchName), $"{branchDto.BranchName}")
return Ok (Rebased referenceId)
let referenceActorProxy = actorProxyFactory.CreateActorProxy<IReferenceActor>(Reference.GetActorId referenceId, ActorName.Reference)
let! promotionDto = referenceActorProxy.Get metadata.CorrelationId
match! addReference promotionDto.DirectoryId promotionDto.Sha256Hash promotionDto.ReferenceText ReferenceType.Rebase with
| Ok referenceId -> return Ok (Rebased referenceId)
| Error error -> return Error error
| SetName branchName -> return Ok (NameSet branchName)
| EnableAssign enabled -> return Ok (EnabledAssign enabled)
| EnablePromotion enabled -> return Ok (EnabledPromotion enabled)
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Actors/BranchName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module BranchName =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; RepositoryId: {RepositoryId}; BranchName: {BranchName}; BranchId: {BranchId}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
10 changes: 2 additions & 8 deletions src/Grace.Actors/Commands.Actor.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Grace.Actors

open Grace.Shared.Dto.Reference
open Grace.Shared.Types
open Grace.Shared.Utilities
open System.Runtime.Serialization
Expand Down Expand Up @@ -82,14 +83,7 @@ module Commands =
module Reference =
[<KnownType("GetKnownTypes")>]
type ReferenceCommand =
| Create of
referenceId: ReferenceId *
repositoryId: RepositoryId *
branchId: BranchId *
directoryId: DirectoryVersionId *
sha256Hash: Sha256Hash *
referenceType: ReferenceType *
referenceText: ReferenceText
| Create of referenceDto: ReferenceDto
| DeleteLogical of force: bool * DeleteReason: DeleteReason
| DeletePhysical
| Undelete
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/DirectoryVersion.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module DirectoryVersion =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; Activated {ActorType} {ActorId}. {message}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
actorName,
host.Id,
Expand Down Expand Up @@ -142,7 +142,7 @@ module DirectoryVersion =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; Id: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand All @@ -153,7 +153,7 @@ module DirectoryVersion =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; Command: {Command}; Id: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
10 changes: 2 additions & 8 deletions src/Grace.Actors/Events.Actor.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Grace.Actors

open Grace.Shared.Dto.Reference
open Grace.Shared.Types
open Grace.Shared.Utilities
open NodaTime
Expand Down Expand Up @@ -127,14 +128,7 @@ module Events =
/// Defines the events for the Reference actor.
[<KnownType("GetKnownTypes")>]
type ReferenceEventType =
| Created of
referenceId: ReferenceId *
repositoryId: RepositoryId *
branchId: BranchId *
directoryId: DirectoryVersionId *
sha256Hash: Sha256Hash *
referenceType: ReferenceType *
referenceText: ReferenceText
| Created of referenceDto: ReferenceDto
| LogicalDeleted of force: bool * DeleteReason: DeleteReason
| PhysicalDeleted
| Undeleted
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/Organization.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Organization =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; Activated {ActorType} {ActorId}. {message}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
actorName,
host.Id,
Expand Down Expand Up @@ -134,7 +134,7 @@ module Organization =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; OrganizationId: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand All @@ -145,7 +145,7 @@ module Organization =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; Command: {Command}; OrganizationId: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Actors/OrganizationName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module OrganizationName =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; OwnerId: {OwnerId}; OrganizationName: {OrganizationName}; OrganizationId: {OrganizationId}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Actors/Owner.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module Owner =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; Activated {ActorType} {ActorId}. {message}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
actorName,
host.Id,
Expand Down Expand Up @@ -142,7 +142,7 @@ module Owner =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; OwnerId: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand All @@ -153,7 +153,7 @@ module Owner =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; Command: {Command}; OwnerId: {Id}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Actors/OwnerName.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module OwnerName =
log.LogInformation(
"{currentInstant}: Node: {hostName}; Duration: {duration_ms}ms; CorrelationId: {correlationId}; Finished {ActorName}.{MethodName}; OwnerName: {OwnerName}; OwnerId: {ownerId}.",
getCurrentInstantExtended (),
Environment.MachineName,
getMachineName,
duration_ms,
this.correlationId,
actorName,
Expand Down
Loading

0 comments on commit 95f8c54

Please sign in to comment.