@@ -93,10 +93,10 @@ func (p *Process) WaitFor(d time.Duration) error {
9393
9494// RegisterFunc registers the function using it's reflection name
9595func (p Process ) RegisterFunc (function interface {}) (funcName string , err error ) {
96- if reflect .TypeOf (function ).Kind () != reflect .Func {
97- return "" , errors .New ("f is not a function" )
96+ funcName , err = fn (function )
97+ if err != nil {
98+ return "" , err
9899 }
99- funcName = runtime .FuncForPC (reflect .ValueOf (function ).Pointer ()).Name ()
100100 registered := p .server .IsTaskRegistered (funcName )
101101 if ! registered {
102102 err = p .server .RegisterTask (funcName , function )
@@ -116,22 +116,38 @@ func (p Process) Register(funcName string, function interface{}) error {
116116 return nil
117117}
118118
119- // Invoke registers the func with it's reflect name, and sends the task
119+ // Invoke calls the func by its reflect name
120120func (p Process ) Invoke (f interface {}, args []tasks.Arg ) (jobID string , err error ) {
121- funcName , err := p .RegisterFunc (f )
121+ return p .InvokeWithHeaders (f , args , nil )
122+ }
123+
124+ // InvokeWithHeaders calls the function by its reflect name with headers
125+ func (p Process ) InvokeWithHeaders (f interface {}, args []tasks.Arg , headers tasks.Headers ) (jobID string , err error ) {
126+ funcName , err := fn (f )
122127 if err != nil {
123128 return "" , err
124129 }
125- return p .Call (funcName , args )
130+ return p .CallWithHeaders (funcName , args , headers )
126131}
127132
128133// Call calls a registered function, the arguments needs to be in the machinery []Arg format
129134func (p Process ) Call (funcName string , args []tasks.Arg ) (jobID string , err error ) {
135+ return p .CallWithHeaders (funcName , args , nil )
136+ }
137+
138+ // CallWithHeaders calls a register function with metadata
139+ func (p Process ) CallWithHeaders (funcName string , args []tasks.Arg , headers tasks.Headers ) (jobID string , err error ) {
140+ if ! p .server .IsTaskRegistered (funcName ) {
141+ return "" , errors .Errorf ("function %s is not registered" , funcName )
142+ }
143+
130144 sig , err := tasks .NewSignature (funcName , args )
131145 if err != nil {
132146 return "" , errors .Wrap (err , "process call" )
133147 }
134148
149+ sig .Headers = headers
150+
135151 r , err := p .server .SendTask (sig )
136152 if err != nil {
137153 return "" , errors .Wrapf (err , "call func %s" , funcName )
@@ -584,3 +600,10 @@ func interruptSubject(jobID string) string {
584600func headerSubject (jobID string ) string {
585601 return "headers_" + jobID
586602}
603+
604+ func fn (function interface {}) (string , error ) {
605+ if reflect .TypeOf (function ).Kind () != reflect .Func {
606+ return "" , errors .New ("f is not a function" )
607+ }
608+ return runtime .FuncForPC (reflect .ValueOf (function ).Pointer ()).Name (), nil
609+ }
0 commit comments