Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: raise event on encode status change #47

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion app/src/components/interfaces/encode/chips/EncodeStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@ import type { ChipProps } from '@giantnodes/react'
import { Chip } from '@giantnodes/react'
import dayjs from 'dayjs'
import React from 'react'
import { graphql, useFragment } from 'react-relay'
import { graphql, useFragment, useSubscription } from 'react-relay'

const FRAGMENT = graphql`
fragment EncodeStatusFragment on Encode {
id
status
failed_at
cancelled_at
completed_at
}
`

const SUBSCRIPTION = graphql`
subscription EncodeStatusSubscription($where: EncodeFilterInput) {
encode_status_changed(where: $where) {
...EncodeSpeedFragment
}
}
`

type EncodeStatusChipProps = {
$key: EncodeStatusFragment$key
}

const EncodeStatus: React.FC<EncodeStatusChipProps> = ({ $key }) => {
const data = useFragment(FRAGMENT, $key)

useSubscription({
subscription: SUBSCRIPTION,
variables: {
variables: {
where: {
id: {
eq: data.id,
},
},
},
},
})

const color = React.useMemo<ChipProps['color']>(() => {
switch (data.status) {
case 'COMPLETED':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Giantnodes.Service.Dashboard.Application.Contracts.Encodes.Events;
using Giantnodes.Service.Dashboard.Domain.Aggregates.Encodes.Repositories;
using HotChocolate.Subscriptions;
using MassTransit;

namespace Giantnodes.Service.Dashboard.Application.Components.Encodes.Events;

public class RaiseEncodeStatusChangedTopic : IConsumer<EncodeStatusChangedEvent>
{
private readonly IEncodeRepository _repository;
private readonly ITopicEventSender _sender;

public RaiseEncodeStatusChangedTopic(IEncodeRepository repository, ITopicEventSender sender)
{
_repository = repository;
_sender = sender;
}

public async Task Consume(ConsumeContext<EncodeStatusChangedEvent> context)
{
var encode = await _repository.SingleAsync(x => x.Id == context.Message.EncodeId);

await _sender.SendAsync(nameof(EncodeStatusChangedEvent), encode, context.CancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Giantnodes.Infrastructure.Domain.Events;
using Giantnodes.Service.Dashboard.Domain.Shared.Enums;

namespace Giantnodes.Service.Dashboard.Application.Contracts.Encodes.Events;

public sealed record EncodeStatusChangedEvent : DomainEvent
{
public required Guid EncodeId { get; init; }

public required EncodeStatus FromStatus { get; init; }

public required EncodeStatus ToStatus { get; init; }
}
14 changes: 13 additions & 1 deletion src/Service.Dashboard/src/Domain/Aggregates/Encodes/Encode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Encode : AggregateRoot<Guid>, ITimestampableEntity
{
private readonly List<EncodeSnapshot> _snapshots = new();

private EncodeStatus _status;

/// <summary>
/// The file being encoded.
/// </summary>
Expand All @@ -34,7 +36,17 @@ public class Encode : AggregateRoot<Guid>, ITimestampableEntity
/// <summary>
/// The current status of the encoding process.
/// </summary>
public EncodeStatus Status { get; private set; }
public EncodeStatus Status
{
get => _status;
private set
{
if (Status != value)
DomainEvents.Add(new EncodeStatusChangedEvent { EncodeId = Id, FromStatus = Status, ToStatus = value });

_status = value;
}
}

/// <summary>
/// The machine performing the encoding.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Giantnodes.Service.Dashboard.Application.Contracts.Encodes.Events;
using Giantnodes.Service.Dashboard.Domain.Aggregates.Encodes;
using Giantnodes.Service.Dashboard.Persistence.DbContexts;
using Microsoft.EntityFrameworkCore;

namespace Giantnodes.Service.Dashboard.HttpApi.Resolvers.Encodes.Subscriptions;

[ExtendObjectType(OperationTypeNames.Subscription)]
public class EncodeStatusChangedSubscription
{
[Subscribe]
[Topic(nameof(EncodeStatusChangedEvent))]
[UseSingleOrDefault]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Encode> EncodeStatusChanged(
[Service] ApplicationDbContext database,
[EventMessage] Encode encode)
{
return database.Encodes.Where(x => x.Id == encode.Id).AsNoTracking();
}
}
Loading