项目作者: HenrikFricke

项目描述 :
life cycle manager for Go
高级语言: Go
项目地址: git://github.com/HenrikFricke/go-lifecycle.git
创建时间: 2017-07-12T16:27:54Z
项目社区:https://github.com/HenrikFricke/go-lifecycle

开源协议:MIT License

下载


go-lifecycle

Usage

This project uses gopkg to handle versioning. Find more information about how to install and where to find the API documentation here.

About the project

This project is inspired by the Lifecycle of the Serverless Framework. The Serverless CLI handles plugins, which are able to hook before or after a process. Every plugin is also a process in the lifecycle, so other plugins can hook before or after a plugin as well. I really liked the way how Serverless build the plugin architecture and I wanted to think about how to achieve this process management in Go.

Concept

The idea is to creeate a sequence of tasks and execute them afterwards. So we have something like a queue with tasks. The tasks will be executed with the FIFO) method. But you can also have the special behaviour of hooks: You can add a pre or post hook to every task and also to every hook (because technically every hook is also a task).

Example

Let’s take a look to a simple example:

  1. package main
  2. import (
  3. "fmt"
  4. glc "github.com/HenrikFricke/go-lifecycle"
  5. )
  6. const (
  7. task1Name glc.TaskName = "task_one"
  8. task2Name = "task_two"
  9. preHook = "pre_hook"
  10. )
  11. func printOut(luggage interface{}) {
  12. fmt.Println("I'am a task")
  13. }
  14. func printOutHook(luggage interface{}) {
  15. fmt.Println("I'am a hook")
  16. }
  17. func main() {
  18. lifecycle := glc.NewLifecycle()
  19. lifecycle.AddTask(task1Name, printOut)
  20. lifecycle.AddTask(task2Name, printOut)
  21. lifecycle.AddPreHook(task2Name, preHook, printOutHook)
  22. lifecycle.Execute(nil)
  23. }

We define here two tasks and one pre hook for the second task, the output of this example is:

  1. I'am a task
  2. I'am a hook
  3. I'am a task

You can find a more complex example in the example folder. You can clone this repository on your local machine and execute the example with go run example/example.go.