원단 가게용 작은 재고 동기화 스크립트를 관리하고 있어요. 이벤트를 읽어서 SKU별로 누적 합계를 유지해요. 가게는 자기 리전인 us-east만 신경 쓰고, 이 값은 config/shop.yml에서 와요. 그래서 restock 분기의 해시 패턴에 region을 넣었어요. 우리 쪽 이벤트에만 매치될 거라고 생각했거든요:
def handle(event)
region = @region
@log.debug("handling #{event[:type]} for #{region}")
case event
in { type: "restock", region: region, sku:, qty: }
@totals[sku] += qty
@log.info("restock #{sku} +#{qty} (#{region})")
in { type: "sale", sku:, qty: }
...
그리고 @region은 main.rb에서 config["region"].strip으로 만들어요.
그런데 로그에 restock bolt-cotton-12 +40 (eu-west)가 찍혀 있고, 그 SKU의 합계가 너무 높아요. 40이 더해졌어요. 이벤트에서 가져온 region을 찍는 로그 줄이 이상해 보이긴 했는데, 처음엔 별로 신경 쓰지 않았어요.
처음엔 YAML 문자열이 이벤트 문자열과 다른 거라고 추측했어요. frozen이든, 인코딩이든, 끝에 붙은 개행이든 그런 거요. 둘 다 inspect로 찍어 봤는데 us-east인 것들은 똑같아 보여요. .strip도 추가했는데 달라진 게 없어요. 구버전 시스템 ruby는 in에서 에러가 나서 안 쓰고, rbenv로 3.1.4에서 돌리고 있어요.
그래서 이게 YAML 문제인가요, 아니면 해시 패턴이 원래 그렇게 동작하는 건가요? eu-west가 어떻게 들어오는지 도무지 모르겠어요...
영어에서 번역·원문 보기Maintaining a little stock-sync script for a fabric shop. It reads events and keeps running totals per SKU. The shop only cares about its own region, us-east, which comes from config/shop.yml. So in the restock branch I put the region in the hash pattern, figuring it would only match our events:
def handle(event)
region = @region
@log.debug("handling #{event[:type]} for #{region}")
case event
in { type: "restock", region: region, sku:, qty: }
@totals[sku] += qty
@log.info("restock #{sku} +#{qty} (#{region})")
in { type: "sale", sku:, qty: }
...
And @region is built in main.rb with config["region"].strip.
But the log has restock bolt-cotton-12 +40 (eu-west) and the total for that SKU is too high. The 40 got counted. The log line printing the region from the event seemed odd to me, but I didn't think much of it at first.
My first guess was the YAML string being different from the event string. Frozen, encoding, a trailing newline, something like that. I printed both with inspect and the us-east ones look identical. Added .strip, no change. It's running under rbenv on 3.1.4, not the old system ruby, which chokes on in.
So is this a YAML thing or is it how hash patterns work? I don't see how eu-west gets in at all...