Skip to content

Commit

Permalink
fix: bug fixes in consumers/producers templating
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Wikström committed Jul 8, 2022
1 parent c918b0f commit 8eb1be6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 67 deletions.
8 changes: 7 additions & 1 deletion components/Consumers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { toPascalCase } from '../utils/common';

export function Consumers({ channels }) {
if (channels?.length == 0) {
return null;
}

return `_amqpService.OnSensorTemperatureChange();`;
return `protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_amqpService.${toPascalCase(channels[0].operationId)}();
return Task.CompletedTask;
}`;
}
22 changes: 11 additions & 11 deletions components/Publishers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { toPascalCase } from '../utils/common';

export function Publishers({ channels }) {
if (channels?.length == 0) {
return null;
}
return `var rnd = new Random((int) DateTime.Now.Ticks);

return `protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// TODO: Send message on custom events, below is a timing example.
var rnd = new Random((int) DateTime.Now.Ticks);
while (!stoppingToken.IsCancellationRequested)
{
var t = new Temperature
{
Celcius = rnd.Next(-25, 120),
Created = $"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss}",
Origin = "SENSOR-001"
};
_amqpService.OnSpecificSensorTemperatureReceived(t);
var message = new ${toPascalCase(channels[0].messageType)}();
_amqpService.${toPascalCase(channels[0].operationId)}(message);
await Task.Delay(rnd.Next(500, 3000), stoppingToken);
}`;
}
}`;
}
7 changes: 2 additions & 5 deletions components/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ namespace ${params.namespace}
_amqpService = new AmqpService(configuration);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
${childrenContent}
}
${childrenContent}
public override void Dispose()
{
base.Dispose();
Expand Down
19 changes: 16 additions & 3 deletions components/templates/amqpservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,23 @@ public class AmqpService : IAmqpService
consumer.operationId
)}");
// TODO: declare passive?
channel.QueueDeclare(queue);
channel.QueueBind(queue: queue,
exchange: "${consumer.exchange}",
routingKey: "${consumer.routingKey}");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (_, ea) =>
{
var body = ea.Body.ToArray();
var temperature = JsonSerializer.Deserialize<Temperature>(Encoding.UTF8.GetString(body));
_logger.Verbose("Temperature received, {@Temperature}", temperature);
var message = JsonSerializer.Deserialize<${
consumer.messageType
}>(Encoding.UTF8.GetString(body));
_logger.Verbose("${toPascalCase(
consumer.messageType
)} received, {@${toPascalCase(consumer.messageType)}}", message);
try
{
Expand All @@ -141,7 +152,9 @@ public class AmqpService : IAmqpService
}
catch (Exception e)
{
_logger.Error(e, "Something went wrong trying to process message {@Temperature},", temperature);
_logger.Error(e, "Something went wrong trying to process message {@${toPascalCase(
consumer.messageType
)}},", message);
channel.BasicReject(ea.DeliveryTag, false);
}
};
Expand Down
165 changes: 118 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8eb1be6

Please sign in to comment.