Skip to content

Commit

Permalink
replace kafka topic producers with send invocations
Browse files Browse the repository at this point in the history
kafka is barely used and would just be an extra unnecessary dependency
  • Loading branch information
marcus-sa committed Aug 14, 2024
1 parent 13a7a3e commit 94edacc
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 139 deletions.
4 changes: 1 addition & 3 deletions accounting-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { Consumer } from '@ftgo/consumer-service-api';
import { Money } from '@ftgo/common';

export interface AccountingServiceHandlers {
// Only required for handlers that need to be invoked directly using a Restate client
createAccount(consumer: Consumer): Promise<void>;
createAccount(consumerId: UUID): Promise<void>;
authorize(
consumerId: UUID,
orderId: UUID,
Expand Down
21 changes: 14 additions & 7 deletions accounting-service/src/accounting.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { restate, RestateServiceContext } from 'deepkit-restate';
import { restate } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { Consumer, KafkaConsumerTopic } from '@ftgo/consumer-service-api';
import { Consumer } from '@ftgo/consumer-service-api';
import { Money } from '@ftgo/common';
import {
Account,
AccountingServiceApi,
AccountingServiceHandlers,
} from '@ftgo/accounting-service-api';
Expand All @@ -15,10 +14,9 @@ import { AccountRepository } from './account.repository';
export class AccountingService implements AccountingServiceHandlers {
constructor(private readonly account: AccountRepository) {}

// @ts-ignore
@(restate.kafka<KafkaConsumerTopic>().handler())
async createAccount(consumer: Consumer): Promise<void> {
await this.account.create(consumer.id);
@restate.handler()
async createAccount(consumerId: UUID): Promise<void> {
await this.account.create(consumerId);
}

@restate.handler()
Expand All @@ -27,4 +25,13 @@ export class AccountingService implements AccountingServiceHandlers {
orderId: UUID,
orderTotal: Money,
): Promise<unknown> {}

@restate.handler()
async reverseAuthorization(
consumerId: UUID,
orderId: UUID,
orderTotal: Money,
): Promise<unknown> {
return Promise.resolve(undefined);
}
}
2 changes: 1 addition & 1 deletion api-gateway/src/restaurant/restaurant.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
@http.controller('restaurant')
export class RestaurantController {
constructor(
private readonly service: RestaurantServiceApi,
private readonly client: RestateClient,
private readonly service: RestaurantServiceApi,
) {}

@http.POST('create')
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion consumer-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './lib/dtos';
export * from './lib/entities';
export * from './lib/services';
export * from './lib/topics';
8 changes: 0 additions & 8 deletions consumer-service-api/src/lib/topics.ts

This file was deleted.

10 changes: 5 additions & 5 deletions consumer-service/src/consumer.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { restate } from 'deepkit-restate';
import { RestateKafkaProducer } from 'deepkit-restate/kafka';
import { restate, RestateServiceContext } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { Money, PersonName } from '@ftgo/common';
import { AccountingServiceApi } from '@ftgo/accounting-service-api';
import {
Consumer,
ConsumerServiceApi,
ConsumerServiceHandlers,
KafkaConsumerTopic,
} from '@ftgo/consumer-service-api';

import { ConsumerRepository } from './consumer.repository';
Expand All @@ -16,13 +15,14 @@ import { ConsumerRepository } from './consumer.repository';
export class ConsumerService implements ConsumerServiceHandlers {
constructor(
private readonly consumer: ConsumerRepository,
private readonly kafka: RestateKafkaProducer,
private readonly accounting: AccountingServiceApi,
private readonly ctx: RestateServiceContext,
) {}

@restate.handler()
async create(name: PersonName): Promise<UUID> {
const consumer = (await this.consumer.create(name)) as Consumer;
await this.kafka.produce<KafkaConsumerTopic>([consumer]);
await this.ctx.send(this.accounting.createAccount(consumer.id));
return consumer.id;
}

Expand Down
11 changes: 8 additions & 3 deletions consumer-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { RestateKafkaProducerModule } from 'deepkit-restate/kafka';
import { RestateModule } from 'deepkit-restate';
import { provideRestateServiceProxy, RestateModule } from 'deepkit-restate';

import { provideDatabase } from '@ftgo/common';
import { AccountingServiceApi } from '@ftgo/accounting-service-api';
import { Consumer } from '@ftgo/consumer-service-api';
import { provideDatabase } from '@ftgo/common';

import { ConsumerServiceConfig } from './config';
import { ConsumerService } from './consumer.service';
Expand All @@ -22,7 +23,11 @@ void new App({
}),
],
controllers: [ConsumerService],
providers: [provideDatabase([Consumer]), ConsumerRepository],
providers: [
provideDatabase([Consumer]),
ConsumerRepository,
provideRestateServiceProxy<AccountingServiceApi>(),
],
})
.setup((module, config: ConsumerServiceConfig) => {
module
Expand Down
10 changes: 0 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ services:
volumes:
- postgres:/var/lib/postgresql/data

redpanda:
image: docker.redpanda.com/redpandadata/redpanda:v24.2.2
ports:
- '8081:8081'
- '8082:8082'
- '9092:9092'
- '19644:9644'
volumes:
- redpanda:/var/lib/redpanda/data

restate:
image: docker.io/restatedev/restate:1.0.2
ports:
Expand Down
1 change: 0 additions & 1 deletion infra/redpanda/Dockerfile

This file was deleted.

9 changes: 0 additions & 9 deletions infra/redpanda/Kraftfile

This file was deleted.

12 changes: 0 additions & 12 deletions infra/redpanda/deploy.sh

This file was deleted.

17 changes: 0 additions & 17 deletions infra/redpanda/project.json

This file was deleted.

4 changes: 0 additions & 4 deletions infra/restate/config.prod.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ bootstrap-num-partitions = 1
[worker.invoker.retry-policy]
initial-interval = "100ms"
max-attempts = 5

[[ingress.kafka-clusters]]
name = "default"
brokers = ["PLAINTEXT://ftgo-redpanda.internal:9092"]
4 changes: 0 additions & 4 deletions infra/restate/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ base-dir = "/var/lib/restate"
[worker.invoker.retry-policy]
initial-interval = "50ms"
max-attempts = 3

[[ingress.kafka-clusters]]
name = "default"
brokers = ["PLAINTEXT://redpanda:9092"]
4 changes: 4 additions & 0 deletions kitchen-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { Restaurant, RestaurantMenu } from '@ftgo/restaurant-service-api';

import { Ticket, TicketDetails } from './entities';

export interface KitchenServiceHandlers {
Expand All @@ -14,6 +16,8 @@ export interface KitchenServiceHandlers {
beginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
undoBeginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
confirmCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
reviseMenu(menu: RestaurantMenu): Promise<void>;
createMenu(restaurant: Restaurant): Promise<void>;
}

export type KitchenServiceApi = RestateService<
Expand Down
57 changes: 46 additions & 11 deletions kitchen-service/src/kitchen.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
import { restate, RestateServiceContext } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { Restaurant, RestaurantMenu } from '@ftgo/restaurant-service-api';
import {
KafkaRestaurantMenuRevisedTopic,
KafkaRestaurantCreatedTopic,
Restaurant,
RestaurantMenu,
} from '@ftgo/restaurant-service-api';
import {
Kitchen,
KitchenServiceApi,
KitchenServiceHandlers,
Ticket,
TicketDetails,
} from '@ftgo/kitchen-service-api';

@restate.service<KitchenServiceApi>()
export class KitchenService implements KitchenServiceHandlers {
// @ts-ignore
@(restate.kafka<KafkaRestaurantCreatedTopic>().handler())
@restate.handler()
async createMenu(restaurant: Restaurant): Promise<void> {}

// @ts-ignore
@(restate.kafka<KafkaRestaurantMenuRevisedTopic>().handler())
@restate.handler()
async reviseMenu(menu: RestaurantMenu): Promise<void> {}

@restate.handler()
async beginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket> {
return Promise.resolve(undefined);
}

@restate.handler()
async cancelTicket(orderId: UUID): Promise<Ticket> {
return Promise.resolve(undefined);
}

@restate.handler()
async confirmCancelTicket(
restaurantId: UUID,
orderId: UUID,
): Promise<Ticket> {
return Promise.resolve(undefined);
}

@restate.handler()
async confirmCreateTicket(ticketId: UUID): Promise<Ticket> {
return Promise.resolve(undefined);
}

@restate.handler()
async createTicket(
restaurantId: UUID,
orderId: UUID,
details: TicketDetails,
): Promise<Ticket> {
return Promise.resolve(undefined);
}

@restate.handler()
async undoBeginCancelTicket(
restaurantId: UUID,
orderId: UUID,
): Promise<Ticket> {
return Promise.resolve(undefined);
}
}
11 changes: 9 additions & 2 deletions order-service-api/src/lib/entities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ItemNotFound } from '@deepkit/orm';
import { Writable } from 'type-fest';
import {
Embedded,
entity,
Expand All @@ -8,7 +10,6 @@ import {
PrimaryKey,
UUID,
} from '@deepkit/type';
import { Writable } from 'type-fest';

import {
Address,
Expand Down Expand Up @@ -150,7 +151,13 @@ export class OrderLineItems {
changeToOrderTotal(orderRevision: OrderRevision) {}

find(menuItemId: UUID): OrderLineItem {
return this.lineItems.find(lineItem => lineItem.menuItemId === menuItemId);
const item = this.lineItems.find(
lineItem => lineItem.menuItemId === menuItemId,
);
if (!item) {
throw new ItemNotFound(menuItemId);
}
return item;
}

updateLineItems(orderRevision: OrderRevision) {}
Expand Down
6 changes: 5 additions & 1 deletion order-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { RestateService } from 'deepkit-restate';
import { restate, RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';
import { ItemNotFound } from '@deepkit/orm';

import { Restaurant, RestaurantMenu } from '@ftgo/restaurant-service-api';

import { Order } from './entities';
import { CreateOrderRequest } from './dtos';
import { OrderMinimumNotMetException } from './replies';
Expand All @@ -16,6 +18,8 @@ export interface OrderServiceHandlers {
confirmCancel(id: UUID): Promise<Order>;
reject(id: UUID): Promise<Order>;
approve(id: UUID): Promise<Order>;
createMenu(restaurant: Restaurant): Promise<void>;
reviseMenu(menu: RestaurantMenu): Promise<void>;
}

export type OrderServiceApi = RestateService<
Expand Down
Loading

0 comments on commit 94edacc

Please sign in to comment.