summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-08-12 15:38:19 +0300
committerGitHub <[email protected]>2021-08-12 15:38:19 +0300
commitdf27287c78d7b17d7c8f0e7fff59fa7cbf2a4f9f (patch)
treedf0749155487eae6bcdbb2456885131a21916f4d /common
parent67db4b5f7b66e9a32713133baed83c3ab7146bb8 (diff)
parentecbfc5c5265a9895f4e371ce4388f64df8714e63 (diff)
#726: feat(plugin): new `jobs` plugin
#726: feat(plugin): new `jobs` plugin
Diffstat (limited to 'common')
-rw-r--r--common/doc.go9
-rw-r--r--common/jobs/interface.go26
-rw-r--r--common/kv/interface.go39
-rw-r--r--common/pubsub/interface.go54
-rw-r--r--common/pubsub/psmessage.go15
5 files changed, 143 insertions, 0 deletions
diff --git a/common/doc.go b/common/doc.go
new file mode 100644
index 00000000..adc03351
--- /dev/null
+++ b/common/doc.go
@@ -0,0 +1,9 @@
+/*
+Package common used to collect common interfaces/structures which might be implemented (or imported) by a different plugins.
+For example, 'pubsub' interface might be implemented by memory, redis, websockets and many other plugins.
+
+Folders:
+- kv - contains KV interfaces and structures
+- pubsub - contains pub-sub interfaces and structures
+*/
+package common
diff --git a/common/jobs/interface.go b/common/jobs/interface.go
new file mode 100644
index 00000000..c957df2b
--- /dev/null
+++ b/common/jobs/interface.go
@@ -0,0 +1,26 @@
+package jobs
+
+import (
+ "context"
+
+ "github.com/spiral/roadrunner/v2/pkg/events"
+ priorityqueue "github.com/spiral/roadrunner/v2/pkg/priority_queue"
+ "github.com/spiral/roadrunner/v2/plugins/jobs/job"
+ "github.com/spiral/roadrunner/v2/plugins/jobs/pipeline"
+)
+
+// Consumer todo naming
+type Consumer interface {
+ Push(ctx context.Context, job *job.Job) error
+ Register(ctx context.Context, pipeline *pipeline.Pipeline) error
+ Run(ctx context.Context, pipeline *pipeline.Pipeline) error
+ Stop(ctx context.Context) error
+
+ Pause(ctx context.Context, pipeline string)
+ Resume(ctx context.Context, pipeline string)
+}
+
+type Constructor interface {
+ JobsConstruct(configKey string, e events.Handler, queue priorityqueue.Queue) (Consumer, error)
+ FromPipeline(pipe *pipeline.Pipeline, e events.Handler, queue priorityqueue.Queue) (Consumer, error)
+}
diff --git a/common/kv/interface.go b/common/kv/interface.go
new file mode 100644
index 00000000..5736a6a7
--- /dev/null
+++ b/common/kv/interface.go
@@ -0,0 +1,39 @@
+package kv
+
+import kvv1 "github.com/spiral/roadrunner/v2/proto/kv/v1beta"
+
+// Storage represents single abstract storage.
+type Storage interface {
+ // Has checks if value exists.
+ Has(keys ...string) (map[string]bool, error)
+
+ // Get loads value content into a byte slice.
+ Get(key string) ([]byte, error)
+
+ // MGet loads content of multiple values
+ // Returns the map with existing keys and associated values
+ MGet(keys ...string) (map[string][]byte, error)
+
+ // Set used to upload item to KV with TTL
+ // 0 value in TTL means no TTL
+ Set(items ...*kvv1.Item) error
+
+ // MExpire sets the TTL for multiply keys
+ MExpire(items ...*kvv1.Item) error
+
+ // TTL return the rest time to live for provided keys
+ // Not supported for the memcached
+ TTL(keys ...string) (map[string]string, error)
+
+ // Clear clean the entire storage
+ Clear() error
+
+ // Delete one or multiple keys.
+ Delete(keys ...string) error
+}
+
+// Constructor provides storage based on the config
+type Constructor interface {
+ // KVConstruct provides Storage based on the config key
+ KVConstruct(key string) (Storage, error)
+}
diff --git a/common/pubsub/interface.go b/common/pubsub/interface.go
new file mode 100644
index 00000000..06252d70
--- /dev/null
+++ b/common/pubsub/interface.go
@@ -0,0 +1,54 @@
+package pubsub
+
+/*
+This interface is in BETA. It might be changed.
+*/
+
+// PubSub interface designed to implement on any storage type to provide pub-sub abilities
+// Publisher used to receive messages from the PHP app via RPC
+// Subscriber should be implemented to subscribe to a topics and provide a connections list per topic
+// Reader return next message from the channel
+type PubSub interface {
+ Publisher
+ Subscriber
+ Reader
+}
+
+type SubReader interface {
+ Subscriber
+ Reader
+}
+
+// Subscriber defines the ability to operate as message passing broker.
+// BETA interface
+type Subscriber interface {
+ // Subscribe broker to one or multiple topics.
+ Subscribe(connectionID string, topics ...string) error
+
+ // Unsubscribe from one or multiply topics
+ Unsubscribe(connectionID string, topics ...string) error
+
+ // Connections returns all connections associated with the particular topic
+ Connections(topic string, ret map[string]struct{})
+}
+
+// Publisher publish one or more messages
+// BETA interface
+type Publisher interface {
+ // Publish one or multiple Channel.
+ Publish(message *Message) error
+
+ // PublishAsync publish message and return immediately
+ // If error occurred it will be printed into the logger
+ PublishAsync(message *Message)
+}
+
+// Reader interface should return next message
+type Reader interface {
+ Next() (*Message, error)
+}
+
+// Constructor is a special pub-sub interface made to return a constructed PubSub type
+type Constructor interface {
+ PSConstruct(key string) (PubSub, error)
+}
diff --git a/common/pubsub/psmessage.go b/common/pubsub/psmessage.go
new file mode 100644
index 00000000..e33d9284
--- /dev/null
+++ b/common/pubsub/psmessage.go
@@ -0,0 +1,15 @@
+package pubsub
+
+import json "github.com/json-iterator/go"
+
+// Message represents a single message with payload bound to a particular topic
+type Message struct {
+ // Topic (channel in terms of redis)
+ Topic string `json:"topic"`
+ // Payload (on some decode stages might be represented as base64 string)
+ Payload []byte `json:"payload"`
+}
+
+func (m *Message) MarshalBinary() (data []byte, err error) {
+ return json.Marshal(m)
+}