在cron作业中使用*gin.Context

huangapple go评论84阅读模式
英文:

Using *gin.Context in cron Job

问题

我有一个cron job,它会调用一个需要*gin.Context作为参数的函数,这个参数将在其他步骤中的其他进程中被需要。以前我把我的代码写成这样:

_, _ = c.cr.AddFunc(constant.CronRunningAt(8), func() {
    ctx := &gin.Context{}
    c.loan.LoanRepaymentNotification(ctx)
})

但是它会抛出如下错误:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x100d154f4]

在cron作业中使用*gin.Context

有没有办法生成一个可以传递给被调用函数的*gin.Context的值类型?谢谢。

英文:

I have cron job that will call function that require *gin.Context as statement, this statement will be required int other process in the other next step. Previously I had made my code like this:

	_, _ = c.cr.AddFunc(constant.CronRunningAt(8), func() {
		ctx := &gin.Context{}
		c.loan.LoanRepaymentNotification(ctx)
	})

but it throw error like this:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x100d154f4]

在cron作业中使用*gin.Context

Is there any way to generate value type *gin.Context that can be passed to the function called? Thank you

答案1

得分: 0

如果您需要在cron任务中使用此方法,那么就意味着不能使用gin.Context
正如库文档中所指定的那样:
> Gin是一个用Go语言编写的Web框架。

因此,您可以通过另一种方式实现对gin.Context的需求,例如使用context.Context
具体取决于您最终想要做什么,但您应该通过更改LoanRepaymentNotification或其底层方法,仅在cron作业中使用context.Context来重构您的代码。

英文:

If you need to use this method on a cron task, then it means that must not use gin.Context.
As it is specified on library documentation:
> Gin is a web framework written in Go.

So what you want to do with gin.Context could be achieve by another way, thanks to the context.Context for exemple.
It depends of what you want to do at the end, but you should refactoring your code by changing LoanRepaymentNotification or its underlying methods to use only context.Context on your cron job.

huangapple
  • 本文由 发表于 2023年3月29日 14:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75873846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定