Skip to content

Instantly share code, notes, and snippets.

View danielsiwiec's full-sized avatar

Dan Siwiec danielsiwiec

View GitHub Profile
@danielsiwiec
danielsiwiec / heapkiller.kt
Last active November 15, 2022 02:57
heap killer
val list = mutableListOf<ByteArray>()
generateSequence(0) { it + 1 }.forEach {
if (it % (HEAP_TO_FILL / INCREMENTS_IN_MB) == 0) list.clear()
list.add(ByteArray(INCREMENTS_IN_MB * BYTES_TO_MB))
}
@danielsiwiec
danielsiwiec / order_service.kt
Last active May 25, 2022 11:03
Order Processing - Orchestration
class OrderService(
inventoryService: InventoryService,
paymentService: PaymentService,
shipmentService: ShipmentService,
emailService: EmailService) {
fun processOrder(order: Order): Status {
val customerEmail = customerService.getEmailAddress(order.customerId)
val checkItemAvailability = {
if (order.items.all { inventoryService.isAvailable(it) }) SUBMITTED else ITEMS_UNAVAILABLE
@danielsiwiec
danielsiwiec / email_service.kt
Created May 25, 2022 09:08
Order Processing - Choreography - Email Service
class EmailService(private val customerEmails: MutableMap<Int, String> = mutableMapOf()) {
@KafkaListener(topics = [ORDERS, SHIPMENTS, PAYMENTS])
fun onOrder(order: Order) {
customerEmails[order.customerId]?.let { emailGateway.notify(it, "order now at status: ${order.status}") }
?: throw RuntimeException("Customer email not found")
}
@KafkaListener(topics = [CUSTOMERS])
fun onCustomer(customer: Customer) {
customerEmails[customer.id] = customer.email
@danielsiwiec
danielsiwiec / shipment_service.kt
Last active May 25, 2022 09:05
Order Processing - Choreography - Shipment Service
class ShipmentService(private val customerAddresses: MutableMap<Int, String> = mutableMapOf()) {
@KafkaListener(topics = [PAYMENTS])
fun onOrder(order: Order) {
if (order.status !== PAID) return
sendShipment(order, customerAddresses[order.customerId] ?: throw RuntimeException("No address for customer ${order.customerId}"))
sendMessage(SHIPMENTS, order.withStatus(SHIPPED))
}
@KafkaListener(topics = [CUSTOMERS])
@danielsiwiec
danielsiwiec / inventory_service.kt
Created May 25, 2022 08:59
Order Processing - Choreography - Inventory Service
class InventoryService(private val inventory: MutableMap<Int, Sku> = mutableMapOf()) {
@KafkaListener(topics = [SHIPMENTS])
fun onOrder(order: Order) {
if (order.status !== SHIPPED) return
order.items.forEach {
inventory.computeIfPresent(it) { _, v -> v.decrementStock().also { sku -> sendMessage(SKUS, sku) } }
}
}
@danielsiwiec
danielsiwiec / order_service.kt
Last active May 25, 2022 08:54
Order Processing - Choreography - Order Service
class OrderService(private val availableSkus:MutableList<Int> = mutableListOf()) {
fun submitOrder(order: Order) {
sendMessage(
ORDERS,
order.withStatus(if (availableSkus.containsAll(order.items)) SUBMITTED else ITEMS_UNAVAILABLE)
)
}
@KafkaListener(topics = [SKUS])
fun onSku(sku: Sku) {
@danielsiwiec
danielsiwiec / payment_service.kt
Last active May 25, 2022 08:53
Order Processing - Choreography - Payment Service
class PaymentService(
private val customerAccounts: MutableMap<Int, Int> = mutableMapOf(),
private val skuPrices: MutableMap<Int, Double> = mutableMapOf()
) {
@KafkaListener(topics = [ORDERS])
fun onOrder(order: Order) {
if (order.status != SUBMITTED) return
val total = order.items.sumOf { skuPrices[it] ?: throw RuntimeException("Customer unknown") }
val status = paymentGateway.processPayment(
@danielsiwiec
danielsiwiec / conditionalChangeset.groovy
Created March 24, 2022 21:50
Conditional Changeset Pipeline
stage('Build foo-service') {
when { changeset pattern: "services/foo-service/*" }
steps {
Build the foo-service here
}
}
@danielsiwiec
danielsiwiec / conditional.groovy
Created March 24, 2022 21:49
Declarative Conditional Pipeline
stage('Example Deploy') {
when {
branch 'production'
}
steps {
echo 'Deploying'
}
}
@danielsiwiec
danielsiwiec / scripted.groovy
Created March 24, 2022 21:47
Scripted Pipeline
node {
stage('Build and test') {
echo 'Building and testing...'
}
if (env.BRANCH_NAME == 'master') {
['qa', 'prod'].each { environment ->
stage("Deploy to $environment") {
echo 'I only execute on the master branch'
}
}