Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ PS_OAPI_CFG = $(PS_OAPI_DIR)/ps-cfg.yaml
PS_OAPI_SPEC = $(PS_OAPI_DIR)/open-api.yaml
PS_OAPI_GEN = pullslip/oapi/ps-openapi_gen.go

# Scheduled task OpenAPI
SCHED_OAPI_DIR=oapi
SCHED_OAPI_CFG = $(SCHED_OAPI_DIR)/sched-cfg.yaml
SCHED_OAPI_SPEC = $(SCHED_OAPI_DIR)/open-api.yaml
SCHED_OAPI_GEN = scheduler/oapi/sched_openapi_gen.go

.PHONY: all docker generate generate-sqlc generate-api generate-commit-id check run fmt fmt-check clean view-coverage deps-update tools-update lint vulncheck check-coverage

all: $(BINARY) archive
Expand All @@ -68,7 +74,7 @@ generate-commit-id: $(COMMIT_ID)

generate-sqlc: $(SQL_GEN_OUT)

generate-api: $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN)
generate-api: $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN) $(SCHED_OAPI_GEN)

$(RETURNABLES_JSON): $(RETURNABLES_YAML)
$(GO) run github.com/mikefarah/yq/v4@v4.52.5 eval -o=json $< > $@.tmp
Expand All @@ -83,6 +89,9 @@ $(PR_OAPI_GEN): $(PR_OAPI_CFG) $(PR_OAPI_SPEC) $(PR_OAPI_OVERLAY)
$(PS_OAPI_GEN): $(PS_OAPI_CFG) $(PS_OAPI_SPEC)
$(OAPI_CODEGEN) -config ./$(PS_OAPI_CFG) ./$(PS_OAPI_SPEC)

$(SCHED_OAPI_GEN): $(SCHED_OAPI_CFG) $(SCHED_OAPI_SPEC)
$(OAPI_CODEGEN) -config ./$(SCHED_OAPI_CFG) ./$(SCHED_OAPI_SPEC)

$(SQL_GEN_OUT): $(SQL_GEN_IN) $(SQLC_CONFIG)
$(SQLC) generate -f $(SQLC_CONFIG)

Expand All @@ -92,10 +101,10 @@ $(COMMIT_ID): $(GIT_COMMIT_DEPS)
printf "%s" "$$commit" > "$@"; \
fi

$(BINARY): $(COMMIT_ID) $(SQL_GEN_OUT) $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN) $(BUILD_GOFILES) $(RETURNABLES_JSON) $(PULLSLIP_TEMPLATE)
$(BINARY): $(COMMIT_ID) $(SQL_GEN_OUT) $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN) $(SCHED_OAPI_GEN) $(BUILD_GOFILES) $(RETURNABLES_JSON) $(PULLSLIP_TEMPLATE)
$(GO) build -v -o $(BINARY) ./$(MAIN_PACKAGE)

archive: $(COMMIT_ID) $(SQL_GEN_OUT) $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN) $(BUILD_GOFILES) $(RETURNABLES_JSON) $(PULLSLIP_TEMPLATE)
archive: $(COMMIT_ID) $(SQL_GEN_OUT) $(OAPI_GEN) $(PR_OAPI_GEN) $(PS_OAPI_GEN) $(SCHED_OAPI_GEN) $(BUILD_GOFILES) $(RETURNABLES_JSON) $(PULLSLIP_TEMPLATE)
$(GO) build -v -o archive ./cmd/archive

check: generate
Expand Down Expand Up @@ -141,3 +150,4 @@ clean:
rm -f $(OAPI_GEN)
rm -f $(PR_OAPI_GEN)
rm -f $(PS_OAPI_GEN)
rm -f $(SCHED_OAPI_GEN)
47 changes: 27 additions & 20 deletions broker/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
psapi "github.com/indexdata/crosslink/broker/pullslip/api"
ps_db "github.com/indexdata/crosslink/broker/pullslip/db"
psoapi "github.com/indexdata/crosslink/broker/pullslip/oapi"
schedapi "github.com/indexdata/crosslink/broker/scheduler/api"
sched_db "github.com/indexdata/crosslink/broker/scheduler/db"
schedoapi "github.com/indexdata/crosslink/broker/scheduler/oapi"
sched_service "github.com/indexdata/crosslink/broker/scheduler/service"
"github.com/indexdata/crosslink/broker/tenant"

Expand Down Expand Up @@ -94,16 +96,17 @@ var ServeMux *http.ServeMux
var appCtx = common.CreateExtCtxWithLogArgsAndHandler(context.Background(), nil, configLog())

type Context struct {
EventBus events.EventBus
IllRepo ill_db.IllRepo
EventRepo events.EventRepo
DirAdapter adapter.DirectoryLookupAdapter
PrRepo pr_db.PrRepo
TenantResolver *tenant.TenantResolver
ApiHandler api.ApiHandler
PrApiHandler prapi.PatronRequestApiHandler
SseBroker *api.SseBroker
PsApiHandler psapi.PullSlipApiHandler
EventBus events.EventBus
IllRepo ill_db.IllRepo
EventRepo events.EventRepo
DirAdapter adapter.DirectoryLookupAdapter
PrRepo pr_db.PrRepo
TenantResolver *tenant.TenantResolver
ApiHandler api.ApiHandler
PrApiHandler prapi.PatronRequestApiHandler
SseBroker *api.SseBroker
PsApiHandler psapi.PullSlipApiHandler
SchedApiHandler schedapi.SchedulerApiHandler
}

func configLog() slog.Handler {
Expand Down Expand Up @@ -200,21 +203,23 @@ func Init(ctx context.Context) (Context, error) {
}

schedRepoRepo := sched_db.CreateSchedRepo(pool)
schedApiHandler := schedapi.NewSchedulerApiHandler(API_PAGE_SIZE, schedRepoRepo, tenantResolver)
if err = StartScheduler(ctx, schedRepoRepo, eventBus); err != nil {
return Context{}, err
}

return Context{
EventBus: eventBus,
IllRepo: illRepo,
EventRepo: eventRepo,
DirAdapter: dirAdapter,
PrRepo: prRepo,
TenantResolver: tenantResolver,
ApiHandler: apiHandler,
PrApiHandler: prApiHandler,
SseBroker: sseBroker,
PsApiHandler: psApiHandler,
EventBus: eventBus,
IllRepo: illRepo,
EventRepo: eventRepo,
DirAdapter: dirAdapter,
PrRepo: prRepo,
TenantResolver: tenantResolver,
ApiHandler: apiHandler,
PrApiHandler: prApiHandler,
SseBroker: sseBroker,
PsApiHandler: psApiHandler,
SchedApiHandler: schedApiHandler,
}, nil
Comment thread
JanisSaldabols marked this conversation as resolved.
}

Expand All @@ -241,12 +246,14 @@ func StartServer(ctx Context) error {
oapi.HandlerFromMux(&ctx.ApiHandler, ServeMux)
proapi.HandlerFromMux(&ctx.PrApiHandler, ServeMux)
psoapi.HandlerFromMux(&ctx.PsApiHandler, ServeMux)
schedoapi.HandlerFromMux(&ctx.SchedApiHandler, ServeMux)
ServeMux.HandleFunc("GET /sse/events", ctx.SseBroker.ServeHTTP)
if ctx.TenantResolver.HasTenantMapping() {
basePath := tenant.OKAPI_PATH_PREFIX
oapi.HandlerFromMuxWithBaseURL(&ctx.ApiHandler, ServeMux, basePath)
proapi.HandlerFromMuxWithBaseURL(&ctx.PrApiHandler, ServeMux, basePath)
psoapi.HandlerFromMuxWithBaseURL(&ctx.PsApiHandler, ServeMux, basePath)
schedoapi.HandlerFromMuxWithBaseURL(&ctx.SchedApiHandler, ServeMux, basePath)
ServeMux.HandleFunc("GET "+basePath+"/sse/events", ctx.SseBroker.ServeHTTP)
}
signatureHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
92 changes: 91 additions & 1 deletion broker/descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,42 @@
"permissionsRequired": [
"broker.pullslips.post"
]
},
{
"methods": [
"GET"
],
"pathPattern": "/broker/batch_actions",
"permissionsRequired": [
"broker.batch_actions.get"
]
},
{
"methods": [
"POST"
],
"pathPattern": "/broker/batch_actions",
"permissionsRequired": [
"broker.batch_actions.post"
]
},
{
"methods": [
"GET"
],
"pathPattern": "/broker/batch_actions/{id}",
"permissionsRequired": [
"broker.batch_actions.item.get"
]
},
{
"methods": [
"DELETE"
],
"pathPattern": "/broker/batch_actions/{id}",
"permissionsRequired": [
"broker.batch_actions.item.delete"
]
}
]
}
Expand Down Expand Up @@ -328,6 +364,56 @@
"displayName": "Broker - create pull slip for a patron request",
"permissionName": "broker.pullslips.post"
},
{
"description": "List batch actions",
"displayName": "Broker - list batch actions",
"permissionName": "broker.batch_actions.get"
},
{
"description": "Create a batch action",
"displayName": "Broker - create batch action",
"permissionName": "broker.batch_actions.post"
},
{
"description": "Read a batch action",
"displayName": "Broker - read batch action",
"permissionName": "broker.batch_actions.item.get"
},
{
"description": "Delete a batch action",
"displayName": "Broker - delete batch action",
"permissionName": "broker.batch_actions.item.delete"
},
{
"description": "Read-only access to batch actions",
"displayName": "Broker - batch actions: read",
"permissionName": "broker.batch_actions.read",
"visible": true,
"subPermissions": [
"broker.batch_actions.get",
"broker.batch_actions.item.get"
]
},
{
"description": "Write access to batch actions",
"displayName": "Broker - batch actions: write",
"permissionName": "broker.batch_actions.write",
"visible": true,
"subPermissions": [
"broker.batch_actions.post",
"broker.batch_actions.item.delete"
]
},
{
"description": "Read and write access to batch actions",
"displayName": "Broker - batch actions: all",
"permissionName": "broker.batch_actions.all",
"visible": true,
"subPermissions": [
"broker.batch_actions.read",
"broker.batch_actions.write"
]
},
{
"description": "Read-only access to patron requests",
"displayName": "Broker - patron requests: read",
Expand Down Expand Up @@ -401,7 +487,11 @@
"broker.patron_requests.item.notifications.item.receipt.put",
"broker.pullslips.item.get",
"broker.pullslips.item.pdf.get",
"broker.pullslips.post"
"broker.pullslips.post",
"broker.batch_actions.get",
"broker.batch_actions.post",
"broker.batch_actions.item.get",
"broker.batch_actions.item.delete"
]
}
]
Expand Down
8 changes: 8 additions & 0 deletions broker/events/eventmodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
EventNameLmsSupplierMessage EventName = "lms-supplier-message"
EventNameSendNotification EventName = "send-notification"
EventNameCheckAvailability EventName = "check-availability"
EventNameSendEmail EventName = "send-email"
)

type Signal string
Expand Down Expand Up @@ -94,6 +95,7 @@ type CommonEventData struct {
Action *pr_db.PatronRequestAction `json:"action,omitempty"`
ActionResult *ActionResult `json:"actionResult,omitempty"`
Notification *pr_db.Notification `json:"notification,omitempty"`
BatchActionData *BatchActionData `json:"batchActionData,omitempty"`
}

type ActionResult struct {
Expand Down Expand Up @@ -123,6 +125,12 @@ type NotifyData struct {
Target SignalTarget `json:"target"`
}

type BatchActionData struct {
ActionName string `json:"actionName"`
Selector string `json:"selector"`
TaskId string `json:"taskId"`
Comment thread
JanisSaldabols marked this conversation as resolved.
}

func NewErrorResult(message string, cause string) (EventStatus, *EventResult) {
return EventStatusError, &EventResult{
CommonEventData: CommonEventData{
Expand Down
4 changes: 4 additions & 0 deletions broker/migrations/039_add_batch_action.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP TABLE IF EXISTS batch_action;

Comment thread
JanisSaldabols marked this conversation as resolved.
DELETE FROM scheduled_task WHERE event_name = 'send-email';
DELETE FROM event_config WHERE event_name = 'send-email';
18 changes: 18 additions & 0 deletions broker/migrations/039_add_batch_action.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE batch_action
(
id TEXT PRIMARY KEY,
action_name TEXT NOT NULL,
schedule TEXT NOT NULL,
batch_query TEXT NOT NULL,
owner TEXT NOT NULL,
scheduled_task_id TEXT NOT NULL,
Comment thread
JanisSaldabols marked this conversation as resolved.
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ,
FOREIGN KEY (scheduled_task_id) REFERENCES scheduled_task (id)
);

CREATE INDEX idx_batch_action_owner ON batch_action (owner);

INSERT INTO event_config (event_name, event_type, retry_count)
VALUES ('send-email', 'TASK', 1);

1 change: 1 addition & 0 deletions broker/oapi/cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ output-options:
- patron-requests-api
- sse-api
- pull-slips-api
- scheduler-api
overlay:
path: oapi/overlay.yaml
generate:
Expand Down
Loading