3

I'm trying to refactor some code to be inside a worker and getting an error:

undefined symbol userId

Seems the worker is unable to see variables from the scope above it. How can I get the worker to see the parameter being passed in?

import ballerina.net.http;
import ballerina.lang.messages;
import ballerina.lang.jsons;

@http:BasePath ("/foo")
  service barApi {
  http:ClientConnector endpointEP = create http:ClientConnector("http://example.com");

  @http:GET
  @http:Path("/users/{userId}")
  resource users (message m,
  @http:PathParam("userId") string userId) {
    worker sampleWorker(message m) {
      string requestPath = "/user/" + userId;
      message response = http:ClientConnector.get(endpointEP, requestPath, m);
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
geekaholic
  • 31
  • 2
  • Workers cannot see variables from the enclosing scope at the moment. It is restricted by design in the current version of Ballerina. But we've had few discussions to improve this behavior where it'll allow you to pass other variables to the worker. At the moment, you can only pass a message. Please refer https://github.com/ballerinalang/ballerina/blob/master/docs/specification/workers.md for more information. – Sameera Jayasoma Mar 25 '17 at 03:44
  • @SameeraJayasoma At the moment the language parser is a bit too restrictive. For example I don't seem to be able mutate the message m just above the worker block to pass data in through it. I'm also unable to define a global endpointEP above \@http::BasePath section, which is used within the worker. – geekaholic Mar 30 '17 at 13:31
  • To add to above, I was able to define a global as long as it's a const. Any design reason for only allowing constant globals but not variables? – geekaholic Mar 31 '17 at 12:52
  • We've come to a conclusion on how to improve Ballerina worker design. You can find more details in this document. Please review this and give us your feedback. https://docs.google.com/document/d/1ovnsu_Ifjysjg3OUH0QE3aalnV_0hMtrs38QJheyYaw/edit?ts=58e1c3e6# – Sameera Jayasoma Apr 03 '17 at 17:18

1 Answers1

1

You can pass any number of variables to a ballerina worker with the new design of worker interactions. Let's say you want to pass 2 variables from your default worker (main) to another worker W1, Here is the sample code.

import ballerina.lang.system;

function main(string[] args) {
int x = 10;
string s = "Test";
x, s -> W1;

worker W1 {
int y;
string t;
y,t <- default;
system:println("Inside worker " + y + " " + t);

}
}
Chanaka udaya
  • 5,134
  • 4
  • 27
  • 34