Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / outboxcleanup.sql
Last active July 27, 2023 19:38
NServiceBus SQL Persistent outbox cleanup
DECLARE @DispatchedBefore datetime = GETUTCDATE()-1 -- Removes entries older then 24 hours
DECLARE @BatchSize INT = 4000 -- Avoid batch sizes over 4.000 to prevent lock escalation
WHILE 1 = 1
BEGIN
DELETE TOP (@BatchSize) FROM [dbo].[EndpointNameOutboxData] WITH (ROWLOCK)
WHERE Dispatched = 'true' AND DispatchedAt < @DispatchedBefore;
IF @@ROWCOUNT < @BatchSize -- Important that @@ROWCOUNT is read immediately after the DELETE
BREAK
END
@ramonsmits
ramonsmits / TaskRunBehavior.cs
Created July 17, 2023 09:32
NServiceBus 6+ behavior to run the incoming message
// If all handlers are compute bound or IO isn't happening early it can result in less concurrent processing as depending on the
// transport and it being full async and no IO is happening message processing could be sequential. This behavior will immediately
// do a Task.Run
//
// Usage:
// endpointConfiguration.Pipeline.Register(new TaskRunBehavior(), nameof(TaskRunBehavior));
//
class TaskRunBehavior : IBehavior<IIncomingPhysicalMessageContext, IIncomingPhysicalMessageContext>
{
public Task Invoke(IIncomingPhysicalMessageContext context, Func<IIncomingPhysicalMessageContext, Task> next)
@ramonsmits
ramonsmits / PathWalker.cs
Created July 10, 2023 18:29
Search for a file in the directory tree
using System;
using System.IO;
using System.Linq;
static class PathWalker
{
public static string Find(string filename, string path)
{
if (filename == null) throw new ArgumentNullException(nameof(filename));
if (path == null) throw new ArgumentNullException(nameof(path));
@ramonsmits
ramonsmits / PluginLoadContext.cs
Last active June 9, 2023 07:29
Plugin load context that probes if a framework specific version exists
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
/// <summary>
/// Based on:
///
/// - https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support
@ramonsmits
ramonsmits / drop-tables.sql
Created May 23, 2023 07:38
TSQL: Create DROP TABLE queries for all tables in database
SELECT 'DROP TABLE ' + '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_SCHEMA, TABLE_NAME
netsh advfirewall firewall add rule name="ServicePulse - TCP 9090" dir=in action=allow protocol=TCP localport=9090
netsh advfirewall firewall add rule name="ServiceControl - TCP 33333" dir=in action=allow protocol=TCP localport=33333
netsh advfirewall firewall add rule name="ServiceControl.Monitoring - TCP 33633" dir=in action=allow protocol=TCP localport=33633
netsh advfirewall firewall add rule name="ServiceControl.Audit - TCP 44444" dir=in action=allow protocol=TCP localport=44444
@ramonsmits
ramonsmits / ChargeOrchestrator.cs
Created January 23, 2023 16:53
ChargeOrchestrator
using System.Globalization;
using Microsoft.Extensions.Logging;
using MQTTnet;
class ChargeOrchestrator
{
const double PackSizeRemainingAtFull = 85000; // kWh
readonly ILogger<ChargeOrchestrator> logger;
readonly IMessagePublisher messagePublisher;
@ramonsmits
ramonsmits / emoji.md
Last active December 9, 2022 08:22
Emoji

🐛 ✨ 📦 🚨 ✔️❌ ✅❎ ⚙️ 🔧 🚨 📚

@ramonsmits
ramonsmits / water-meter.yml
Created October 4, 2022 11:46
ESPHome ESP32 water meter
# originally based on https://www.huizebruin.nl/home-assistant/esphome/watermeter-uitlezen-in-home-assistant-met-esphome
#
# Differences:
# - Uses MQTT.
# - Uses internal_filter: 250ms to skip short pulses due to "floating pin"
# voltage. Measure your minimum pulse time by opening several taps in your
# home and count the number of pulse in 30 seconds and you can calculate the
# minimum pulse duration. If you set this value just under this you should be
# safe.
# - Blinks the blue LED (GPIO2) for every received pulse.
@ramonsmits
ramonsmits / XDocumentPreserve.cs
Created September 2, 2022 11:15
XDocument read/write that preserves BOM, line ending and whitespace
//
// Preserves:
//
// - Whitespace (`LoadOptions.PreserveWhitespace`)
// - BOM (passing `UTF8Encoding(false)` ensures reader.Encoding will use this encoding, if BOM is present reader will swap to UTF8Encoding(true))
// - Line endings (using StreamReader instead of passing filename preserves line endings in document)
// - XML declaration header
//
var path = ""; // Path to XML file