Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a scheduled task server, dependency injection error #3120

Closed
tutengdihuang opened this issue Dec 14, 2023 · 3 comments
Closed

Add a scheduled task server, dependency injection error #3120

tutengdihuang opened this issue Dec 14, 2023 · 3 comments
Labels
question Further information is requested

Comments

@tutengdihuang
Copy link

我想在newApp添加一个我自己写的cron server: cs *server.CronServer
func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server, cs *server.CronServer) *kratos.App {
return kratos.New(
kratos.ID(id),
kratos.Name(Name),
kratos.Version(Version),
kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
kratos.Server(
gs,
hs,
cs,
),
)
}

执行依赖注入报错inject wireApp: no provider found for *invalid type
provider已经写入: 在server 包中
var ProviderSet = wire.NewSet(
NewGRPCServer,
NewHTTPServer,
NewCronWorker,
)
请帮忙解决一下或者给一个例子

@tutengdihuang tutengdihuang added the question Further information is requested label Dec 14, 2023
@kratos-ci-bot kratos-ci-bot changed the title 添加一个定时任务的server ,依赖注入报错 Add a scheduled task server, dependency injection error Dec 14, 2023
Copy link

dosubot bot commented Dec 14, 2023

🤖

Hello @tutengdihuang! 👋

I'm Dosu, a friendly bot here to assist you with your issues, answer queries, and guide you on your journey towards becoming an amazing contributor. While we wait for a human maintainer, I'm here to make sure you're not left hanging. Looking forward to working with you! 😄

根据我在Kratos仓库中找到的类似问题,你可能需要将你的wokerStartup函数改为依赖于特定的Job实例:

func wokerStartup(job1 worker.JobA, job2 worker.Job2) {
    jobs := []worker.Job{job1, job2}
    for i := 0; i < len(jobs); i++ {
        jobs[i].Handle()
    }
}

如果wokerStartup函数比较复杂并且需要被重用,你可以使用Option模式:

首先,创建一个新的WorkerOption

type WorkerOption struct{
    Jobs []worker.Job
}

然后让wokerStartup依赖于这个WorkerOptionNewWorkerOption应该依赖于特定的Job实例。

这个解决方案来自于Kratos仓库的这个问题

如果你在实施这个解决方案时遇到任何问题,或者这个解决方案不能解决你的问题,请提供更多的信息,我会尽我所能帮助你。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@shenqidebaozi
Copy link
Member

Is the injected type the same as the dependent type

@tutengdihuang
Copy link
Author

Is the injected type the same as the dependent type

Thanks for your replay, and I have fixed the issue, because of the type problme. And thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants