Home > Backend Development > Golang > Using *gin.Context in cron jobs

Using *gin.Context in cron jobs

WBOY
Release: 2024-02-10 19:30:08
forward
646 people have browsed it

在 cron 作业中使用 *gin.Context

php editor Banana introduces to you the use of gin.Context in cron jobs. Gin is a popular Go language web framework, but you may encounter some problems using it in cron jobs. A cron job is a task that runs periodically in the background instead of responding to HTTP requests. Therefore, we cannot use gin.Context directly like we can in web applications. However, we can use gin.Context by simulating HTTP requests to enjoy the convenience of the Gin framework in cron jobs. In this article, we will explore how to use gin.Context in cron jobs and provide some practical tips and considerations.

Question content

I have a cron job that will call a function that requires *gin.context as a statement that will be needed in other processes in the next step. My code used to be like this:

_, _ = c.cr.addfunc(constant.cronrunningat(8), func() {
        ctx := &gin.context{}
        c.loan.loanrepaymentnotification(ctx)
    })
Copy after login

But it throws an error like this:

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

Is there any way to generate a value type that can be passed to a function called *gin.context? Thanks

Workaround

If you need to use this method on a cron task, it means must notuse gin.Context. As specified in the library documentation:

Gin is a web framework written in Go.

So what you want to do with gin.Context can be achieved in another way, such as context.Context. It depends on what you want to do in the end, but you should refactor your code by changing LoanRepaidNotification or its underlying method to only use context.Context in cron jobs.

The above is the detailed content of Using *gin.Context in cron jobs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template