worker.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package informer
  15. import (
  16. "context"
  17. "yunion.io/x/log"
  18. "yunion.io/x/onecloud/pkg/appsrv"
  19. "yunion.io/x/onecloud/pkg/util/nopanic"
  20. )
  21. var (
  22. informerWorkerMan *appsrv.SWorkerManager
  23. )
  24. func init() {
  25. informerWorkerMan = appsrv.NewWorkerManager("InformerWorkerManager", 10, 10240, false)
  26. }
  27. /*type noCancel struct {
  28. ctx context.Context
  29. }
  30. func (c noCancel) Deadline() (time.Time, bool) {
  31. return time.Time{}, false
  32. }
  33. func (c noCancel) Done() <-chan struct{} {
  34. return nil
  35. }
  36. func (c noCancel) Err() error {
  37. return nil
  38. }
  39. func (c noCancel) Value(key interface{}) interface{} {
  40. return c.ctx.Value(key)
  41. }*/
  42. type informerTask struct {
  43. be IInformerBackend
  44. f func(ctx context.Context, be IInformerBackend) error
  45. }
  46. func (t *informerTask) Run() {
  47. nopanic.Run(func() {
  48. // outside context ignored cause of run in worker
  49. if err := t.f(context.Background(), t.be); err != nil {
  50. log.Errorf("run informer error: %v", err)
  51. }
  52. })
  53. }
  54. func (t *informerTask) Dump() string {
  55. return ""
  56. }
  57. func run(ctx context.Context, f func(ctx context.Context, be IInformerBackend) error) error {
  58. be := GetDefaultBackend()
  59. if be == nil {
  60. return ErrBackendNotInit
  61. }
  62. task := informerTask{
  63. f: f,
  64. be: be,
  65. }
  66. informerWorkerMan.Run(&task, nil, nil)
  67. return nil
  68. }