hugopl/sidekiq.cr

Middleware chain invoke not working in 0.18

Closed this issue · 1 comments

This worked in 0.17.4. The code in question is here. What I've found is that the middleware chain is no longer returning a truthy/falsy value but always Nil.

Note this code is probably suboptimal, it's ported from Ruby. I'm realizing that I don't have a sufficient test suite for middleware and need to build test cases for the things middleware should be able to do.

      def invoke(job, ctx, &block)
        chain = entries.map { |k| k }
        next_link(chain, job, ctx, &block)
      end

      def next_link(chain, job, ctx, &block)
        if chain.empty?
          block.call
        else
          chain.shift.call(job, ctx) do
            next_link(chain, job, ctx, &block)
          end
        end
      end

If a chain has two middlewares, the call sequence should look like:

invoke
  next_link
    call middleware 1
      next_link
        call middleware 2
          next_link
            call invoke's block

Each middleware should be able to stop the execution early by returning Falsy (bug 1). Otherwise the entire chain should return the result of invoke's block (bug 2). Here's how the entire sequence is invoked.

jhass commented

You probably should explicitly type the return value to Bool, so &block : -> Bool or even alias Middleware = -> Bool and &block : Middleware.