summaryrefslogtreecommitdiff
path: root/plugins/temporal/client/plugin.go
blob: 047a1815cdd04606b086f48d0a63a271b021aba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package client

import (
	"fmt"
	"os"
	"sync/atomic"

	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pool"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	rrt "github.com/spiral/roadrunner/v2/plugins/temporal/protocol"
	"go.temporal.io/sdk/client"
	"go.temporal.io/sdk/converter"
	"go.temporal.io/sdk/worker"
)

// PluginName defines public service name.
const PluginName = "temporal"

// indicates that the case size was set
var stickyCacheSet = false

// Plugin implement Temporal contract.
type Plugin struct {
	workerID int32
	cfg      *Config
	dc       converter.DataConverter
	log      logger.Logger
	client   client.Client
}

// Temporal define common interface for RoadRunner plugins.
type Temporal interface {
	GetClient() client.Client
	GetDataConverter() converter.DataConverter
	GetConfig() Config
	GetCodec() rrt.Codec
	CreateWorker(taskQueue string, options worker.Options) (worker.Worker, error)
}

// Config of the temporal client and depended services.
type Config struct {
	Address    string
	Namespace  string
	Activities *pool.Config
	Codec      string
	DebugLevel int `mapstructure:"debug_level"`
	CacheSize  int `mapstructure:"cache_size"`
}

// Init initiates temporal client plugin.
func (p *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
	const op = errors.Op("temporal_client_plugin_init")
	p.log = log
	p.dc = rrt.NewDataConverter(converter.GetDefaultDataConverter())
	err := cfg.UnmarshalKey(PluginName, &p.cfg)
	if err != nil {
		return errors.E(op, err)
	}
	if p.cfg == nil {
		return errors.E(op, errors.Disabled)
	}

	return nil
}

// GetConfig returns temporal configuration.
func (p *Plugin) GetConfig() Config {
	if p.cfg != nil {
		return *p.cfg
	}
	// empty
	return Config{}
}

// GetCodec returns communication codec.
func (p *Plugin) GetCodec() rrt.Codec {
	if p.cfg.Codec == "json" {
		return rrt.NewJSONCodec(rrt.DebugLevel(p.cfg.DebugLevel), p.log)
	}

	// production ready protocol, no debug abilities
	return rrt.NewProtoCodec()
}

// GetDataConverter returns data active data converter.
func (p *Plugin) GetDataConverter() converter.DataConverter {
	return p.dc
}

// Serve starts temporal srv.
func (p *Plugin) Serve() chan error {
	const op = errors.Op("temporal_client_plugin_serve")
	errCh := make(chan error, 1)
	var err error

	if stickyCacheSet == false && p.cfg.CacheSize != 0 {
		worker.SetStickyWorkflowCacheSize(p.cfg.CacheSize)
		stickyCacheSet = true
	}

	p.client, err = client.NewClient(client.Options{
		Logger:        p.log,
		HostPort:      p.cfg.Address,
		Namespace:     p.cfg.Namespace,
		DataConverter: p.dc,
	})

	if err != nil {
		errCh <- errors.E(op, err)
	}

	p.log.Debug("connected to temporal server", "address", p.cfg.Address)

	return errCh
}

// Stop stops temporal srv connection.
func (p *Plugin) Stop() error {
	if p.client != nil {
		p.client.Close()
	}

	return nil
}

// GetClient returns active srv connection.
func (p *Plugin) GetClient() client.Client {
	return p.client
}

// CreateWorker allocates new temporal worker on an active connection.
func (p *Plugin) CreateWorker(tq string, options worker.Options) (worker.Worker, error) {
	const op = errors.Op("temporal_client_plugin_create_worker")
	if p.client == nil {
		return nil, errors.E(op, errors.Str("unable to create worker, invalid temporal client"))
	}

	if options.Identity == "" {
		if tq == "" {
			tq = client.DefaultNamespace
		}

		// ensures unique worker IDs
		options.Identity = fmt.Sprintf(
			"%d@%s@%s@%v",
			os.Getpid(),
			getHostName(),
			tq,
			atomic.AddInt32(&p.workerID, 1),
		)
	}

	return worker.New(p.client, tq, options), nil
}

// Name of the service.
func (p *Plugin) Name() string {
	return PluginName
}

func getHostName() string {
	hostName, err := os.Hostname()
	if err != nil {
		hostName = "Unknown"
	}
	return hostName
}