aertje / cloud-tasks-emulator

Google cloud tasks emulator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to create http task calling to localhost using docker-compose?

0xori opened this issue · comments

Thanks for this great project!

I'm using the docker-compose example:

gcloud-tasks-emulator:
    image: ghcr.io/aertje/cloud-tasks-emulator:latest
    command: -host 0.0.0.0 -port 8123 -queue "projects/dev/locations/here/queues/anotherq"
    ports:
      - "${TASKS_PORT:-8123}:8123"
    environment:
      APP_ENGINE_EMULATOR_HOST: http://localhost:8080

creating a task from a local node.js:

export async function createTask({payload, taskURI, inSeconds}) {

    try {
         const project = config.project;
         const queue = config.queue;
         const location = config.location;

        // Construct the fully qualified queue name.
        const parent = client.queuePath(project, location, queue);
        let task = {
            appEngineHttpRequest: {
                httpMethod: 'POST',
                relativeUri: taskURI,
            },
        };
        if(process.env.NODE_ENV !== "production"){
            task = {
                httpRequest: {
                    httpMethod: 'POST',
                    url: "localhost:8080" + taskURI,
                },
            };
        }


        if (payload) {
            console.log("payload", payload)
            if(process.env.NODE_ENV !== "production"){
                task.httpRequest.body = Buffer.from(JSON.stringify(payload)).toString('base64');
            }else{
                task.appEngineHttpRequest.body = Buffer.from(JSON.stringify(payload)).toString('base64');
            }

        }

        if (inSeconds) {
            // The time when the task is scheduled to be attempted.
            task.scheduleTime = {
                seconds: inSeconds + Date.now() / 1000,
            };
        }

        console.log('Sending task:');
        console.log(task);
        // Send create task request.
        const request = {parent, task};
        const [response] = await client.createTask(request);
        const name = response.name;
        console.log(`Created task ${name}`);
        return true;
    }catch (e) {
        console.error("error creating task", e)
        return false;
    }

}

trying to create task from node, and i can see that the task is being created but i'm getting this error in docker log:
gcloud-tasks-emulator_1 | Post localhost:8080/api/tasks/save-session-log: unsupported protocol scheme "localhost"

seems like a docker internal network issue but i can't figure it out. any suggestions?

Thanks @0xori. Not sure this is the best place to ask for help, perhaps stackoverflow is a good spot?

Anyway, can you try to add http:// to url: "localhost:8080" + taskURI,. It's complaining because it's interpreting localhost as the protocol.