mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Add plugin tasks (#651)
This commit is contained in:
26
pkg/plugin/examples/gorpc/gorpc.yml
Normal file
26
pkg/plugin/examples/gorpc/gorpc.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
# example plugin config
|
||||
name: Hawwwwt Tagger
|
||||
description: Ultimate Hawwwwt tagging utility.
|
||||
version: 1.0
|
||||
url: http://www.github.com/stashapp/stash
|
||||
exec:
|
||||
- plugin_gorpc
|
||||
interface: rpc
|
||||
tasks:
|
||||
- name: Add hawwwwt tag to random scene
|
||||
description: Creates a "Hawwwwt" tag if not present and adds to a random scene.
|
||||
defaultArgs:
|
||||
mode: add
|
||||
- name: Remove hawwwwt tag from system
|
||||
description: Removes the "Hawwwwt" tag from all scenes and deletes the tag.
|
||||
defaultArgs:
|
||||
mode: remove
|
||||
- name: Indefinite task
|
||||
description: Sleeps indefinitely - interruptable
|
||||
defaultArgs:
|
||||
mode: indef
|
||||
- name: Long task
|
||||
description: Sleeps for 100 seconds - interruptable
|
||||
defaultArgs:
|
||||
mode: long
|
||||
|
||||
97
pkg/plugin/examples/gorpc/main.go
Normal file
97
pkg/plugin/examples/gorpc/main.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// +build plugin_example
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
exampleCommon "github.com/stashapp/stash/pkg/plugin/examples/common"
|
||||
|
||||
"github.com/stashapp/stash/pkg/plugin/common"
|
||||
"github.com/stashapp/stash/pkg/plugin/common/log"
|
||||
"github.com/stashapp/stash/pkg/plugin/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// serves the plugin, providing an object that satisfies the
|
||||
// common.RPCRunner interface
|
||||
err := common.ServePlugin(&api{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type api struct {
|
||||
stopping bool
|
||||
}
|
||||
|
||||
func (a *api) Stop(input struct{}, output *bool) error {
|
||||
log.Info("Stopping...")
|
||||
a.stopping = true
|
||||
*output = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run is the main work function of the plugin. It interprets the input and
|
||||
// acts accordingly.
|
||||
func (a *api) Run(input common.PluginInput, output *common.PluginOutput) error {
|
||||
modeArg := input.Args.String("mode")
|
||||
|
||||
var err error
|
||||
if modeArg == "" || modeArg == "add" {
|
||||
client := util.NewClient(input.ServerConnection)
|
||||
err = exampleCommon.AddTag(client)
|
||||
} else if modeArg == "remove" {
|
||||
client := util.NewClient(input.ServerConnection)
|
||||
err = exampleCommon.RemoveTag(client)
|
||||
} else if modeArg == "long" {
|
||||
err = a.doLongTask()
|
||||
} else if modeArg == "indef" {
|
||||
err = a.doIndefiniteTask()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
*output = common.PluginOutput{
|
||||
Error: &errStr,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
outputStr := "ok"
|
||||
*output = common.PluginOutput{
|
||||
Output: &outputStr,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *api) doLongTask() error {
|
||||
const total = 100
|
||||
upTo := 0
|
||||
|
||||
log.Info("Doing long task")
|
||||
for upTo < total {
|
||||
time.Sleep(time.Second)
|
||||
if a.stopping {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Progress(float64(upTo) / float64(total))
|
||||
upTo++
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *api) doIndefiniteTask() error {
|
||||
log.Warn("Sleeping indefinitely")
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
if a.stopping {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user