Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Last active July 25, 2022 22:09
Show Gist options
  • Save rossnelson/9848c3cab1c54290fc2f2006d26d1fa1 to your computer and use it in GitHub Desktop.
Save rossnelson/9848c3cab1c54290fc2f2006d26d1fa1 to your computer and use it in GitHub Desktop.
package process
import (
"encoding/json"
"fmt"
"github.com/simiancreative/simiango/logger"
)
var processors map[KindID]Processor
type Processor func(Params) (Status, error)
type Params []byte
type Status int
type Kind string
type KindID int
type Payload struct {
Kind `json:"kind"`
}
var kinds = map[Kind]KindID{
"properties": PROPERTIES,
"residents": RESIDENTS,
}
const (
PROPERTIES KindID = iota
RESIDENTS
)
const (
FAILURE Status = iota
SUCCESS
)
func Register(k KindID, p Processor) {
if _, ok := processors[k]; ok {
err := fmt.Errorf("processor already registered for %v", k)
panic(err)
}
processors[k] = p
}
func Process(jobID string, params []byte) {
processorPtr, err := findProcessor(params)
if err != nil {
// update job.log with error by jobID
logger.Errorf(err.Error())
}
processor := *processorPtr
status, err := processor(params)
// update job.log with status and err values by jobID
}
func findProcessor(params []byte) (*Processor, error) {
payload := Payload{}
json.Unmarshal(params, &payload)
kind := kinds[payload.Kind]
if processor, ok := processors[kind]; ok {
return &processor, nil
}
return nil, fmt.Errorf("No processor found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment