Skip to content

Commit

Permalink
Add the ability to skip queue factories
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSnowden committed Dec 1, 2022
1 parent 79a572d commit 24895ea
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 2 deletions.
17 changes: 15 additions & 2 deletions service/history/queueFactoryBase.go
Expand Up @@ -22,6 +22,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination queueFactoryBase_mock.go QueueFactory

package history

import (
Expand Down Expand Up @@ -63,6 +65,7 @@ type (
// 2. Move this interface to queues package after 1 is done so that there's no cycle dependency
// between workflow and queues package.
CreateQueue(shard shard.Context, cache workflow.Cache) queues.Queue
Enabled() bool
}

QueueFactoryBaseParams struct {
Expand Down Expand Up @@ -127,16 +130,22 @@ func QueueSchedulerRateLimiterProvider(
func QueueFactoryLifetimeHooks(
params QueueFactoriesLifetimeHookParams,
) {
var factories []QueueFactory
for _, f := range params.Factories {
if f.Enabled() {
factories = append(factories, f)
}
}
params.Lifecycle.Append(
fx.Hook{
OnStart: func(context.Context) error {
for _, factory := range params.Factories {
for _, factory := range factories {
factory.Start()
}
return nil
},
OnStop: func(context.Context) error {
for _, factory := range params.Factories {
for _, factory := range factories {
factory.Stop()
}
return nil
Expand All @@ -145,6 +154,10 @@ func QueueFactoryLifetimeHooks(
)
}

func (f *QueueFactoryBase) Enabled() bool {
return true
}

func (f *QueueFactoryBase) Start() {
if f.HostScheduler != nil {
f.HostScheduler.Start()
Expand Down
113 changes: 113 additions & 0 deletions service/history/queueFactoryBase_mock.go

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

67 changes: 67 additions & 0 deletions service/history/queueFactoryBase_test.go
@@ -0,0 +1,67 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package history

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
)

func TestQueueFactoryLifetimeHooks(t *testing.T) {
var (
ctrl = gomock.NewController(t)
disabledQueue = NewMockQueueFactory(ctrl)
enabledQueue = NewMockQueueFactory(ctrl)
)

disabledQueue.EXPECT().Enabled().Return(false)
enabledQueue.EXPECT().Enabled().Return(true)

app := fx.New(
fx.Provide(fx.Annotated{
Group: QueueFactoryFxGroup,
Target: func() QueueFactory {
return disabledQueue
},
}, fx.Annotated{
Group: QueueFactoryFxGroup,
Target: func() QueueFactory {
return enabledQueue
},
}),
fx.Invoke(QueueFactoryLifetimeHooks),
)
require.NoError(t, app.Err())

enabledQueue.EXPECT().Start()
require.NoError(t, app.Start(context.Background()))

enabledQueue.EXPECT().Stop()
require.NoError(t, app.Stop(context.Background()))
}

0 comments on commit 24895ea

Please sign in to comment.