All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references组件列表系统相关定时任务-gcron定时任务-基本使用.md

基本使用

package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        err error
        ctx = gctx.New()
    )
    _, err = gcron.Add(ctx, "* * * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "Every second")
    }, "MySecondCronJob")
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "0 30 * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour on the half hour")
    })
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "@hourly", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour")
    })
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "@every 1h30m", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour thirty")
    })
    if err != nil {
        panic(err)
    }

    g.Dump(gcron.Entries())

    time.Sleep(3 * time.Second)

    g.Log().Print(ctx, `stop cronjob "MySecondCronJob"`)
    gcron.Stop("MySecondCronJob")

    time.Sleep(3 * time.Second)

    g.Log().Print(ctx, `start cronjob "MySecondCronJob"`)
    gcron.Start("MySecondCronJob")

    time.Sleep(3 * time.Second)
}

执行后,输出结果为:

[
    {
        Name: "MySecondCronJob",
        Job:  0x14077e0,
        Time: "2021-11-14 12:13:53.445132 +0800 CST m=+0.006167069",
    },
    {
        Name: "cron-1",
        Job:  0x14078a0,
        Time: "2021-11-14 12:13:53.44515 +0800 CST m=+0.006185688",
    },
    {
        Name: "cron-2",
        Job:  0x1407960,
        Time: "2021-11-14 12:13:53.445161 +0800 CST m=+0.006196483",
    },
    {
        Name: "cron-3",
        Job:  0x1407a20,
        Time: "2021-11-14 12:13:53.445218 +0800 CST m=+0.006252937",
    },
]
2021-11-14 12:13:54.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:55.441 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.440 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} stop cronjob "MySecondCronJob"
2021-11-14 12:13:59.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} start cronjob "MySecondCronJob"
2021-11-14 12:14:00.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:01.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:02.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second

AddSingleton添加单例定时任务

单例定时任务,即同时只能有一个该任务正在运行。当第二个相同的定时任务触发执行时,如果发现已有该任务正在执行,第二个任务将会退出不执行,定时器将会继续等待下一次定时任务的触发检测,以此类推。可以使用 AddSingleton 添加单例定时任务。

package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        err error
        ctx = gctx.New()
    )
    _, err = gcron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "doing")
        time.Sleep(2 * time.Second)
    })
    if err != nil {
        panic(err)
    }
    select {}
}

执行后,输出结果为:

2021-11-14 12:16:54.073 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:16:57.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:00.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:03.071 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:06.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:09.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
...

AddOnce添加单次定时任务

单次定时任务, AddOnce 方法用于添加只运行一次的定时任务,当运行一次数后该定时任务自动销毁,Size 方法可以查看运行状态,相关方法:

package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main()  {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
        array.Append(1)
    })
    fmt.Println(cron.Size(),array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(),array.Len())

}

执行后,输出结果为:

1 0
0 1

AddTimes添加指定次数的定时任务

指定次数的定时任务, AddTimes 方法用于添加运行指定次数的定时任务,当运行 times 次数后该定时任务自动销毁, Size 方法可以查看运行状态,相关方法:

package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 2,func(ctx context.Context) {
        array.Append(1)
    })
     fmt.Println(cron.Size(), array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(), array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(), array.Len())
}

执行后,输出结果为:

1 0
1 1
0 2

Entries获取注册的定时任务列表

获取所有注册的定时任务信息, Entries 方法用于获取当前所有已注册的定时任务信息,以切片的形式返回(按注册时间 asc 排序),相关方法:

package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    entries := cron.Entries()
    for  k, v := range entries{
        fmt.Println(k,v.Name,v.Time)

    }
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len())

}

执行后,输出结果为:

0 cron2 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
1 cron1 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
3

Search搜索定时任务

Search 搜索返回具有指定“名称”的计划任务。(返回定时任务 *Entry 对象指针),如果找不到,则返回nil。相关方法:

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    search := cron.Search("cron2")

    g.Log().Print(ctx, search)

    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len())

    // Output:
    // 3
}

执行后,输出结果为:

2022-02-09 10:52:30.011 {18a909957cfed11680c1b145da1ef096} {"Name":"cron2","Time":"2022-02-09T10:52:29.9972842+08:00"}

Stop暂停定时任务

Stop 方法用于停止定时任务( Stop会停止但不会删除), 可通过 name 参数指定需要停止的任务名称,如果没有指定 name,它将停止整个 cron。相关方法:

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    fmt.Println(array.Len(),cron.Size())
    cron.Stop("cron2")
    fmt.Println(array.Len(),cron.Size())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())

    // Output:
    // 1
    // 1
}

执行后,输出结果为:

0 2
0 2
1 1

:::info Stop方法会对定时任务做暂停标记,并立即返回,并不会等待正在执行的定时任务结束。但在某些场景下,我们可能需要等待当前正在执行的定时任务完成后再退出。从v2.8版本开始,提供了StopGracefully方法,用于优雅地暂停定时任务。该方法会阻塞等待正在执行的定时任务完成后再返回。 :::

Remove停止并删除任务

Remove 方法用于根据名称 name 删除定时任务(停止并删除);相关方法:

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    fmt.Println(array.Len(),cron.Size())
    cron.Remove("cron2")
    fmt.Println(array.Len(),cron.Size())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())
    // Output:
    // 0 2
    // 0 1
    // 1 0
}

执行后,输出结果为:

0 2
0 1
1 0

Start启动定时任务

Start 方法用于启动定时任务( Add 后自动启动定时任务), 可通过 name 参数指定需要启动的任务名称。如果没有指定 name,它将启动整个 cron。相关方法:

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    cron.Stop("cron2")
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())
    cron.Start("cron2")
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())

    // Output:
    // 0 1
    // 1 0
}

执行后,输出结果为:

0 1
1 0

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.