Desculpe por postar isso. Estou tentando executar um único arquivo, como uma tarefa executável única. Não me importo com a web, não me importo com as portas que os usuários podem acessar, não há usuários, não há nada para acessar. Quero apenas executar um único arquivo, não um servidor web, mas um arquivo.
Aqui está meu index.js
arquivo
// Retrieve Job-defined env vars
const { CLOUD_RUN_TASK_INDEX = 0, CLOUD_RUN_TASK_ATTEMPT = 0 } = process.env;
// Retrieve User-defined env vars
const { SLEEP_MS, FAIL_RATE } = process.env;
// Define main script
const main = async () => {
console.log(
`Starting Task #${CLOUD_RUN_TASK_INDEX}, Attempt #${CLOUD_RUN_TASK_ATTEMPT}...`
);
// Simulate work
if (SLEEP_MS) {
await sleep(SLEEP_MS);
}
// Simulate errors
if (FAIL_RATE) {
try {
randomFailure(FAIL_RATE);
} catch (err) {
err.message = `Task #${CLOUD_RUN_TASK_INDEX}, Attempt #${CLOUD_RUN_TASK_ATTEMPT} failed.\n\n${err.message}`;
throw err;
}
}
console.log(`Completed Task #${CLOUD_RUN_TASK_INDEX}.`);
};
// Wait for a specific amount of time
const sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
};
// Throw an error based on fail rate
const randomFailure = rate => {
rate = parseFloat(rate);
if (!rate || rate < 0 || rate > 1) {
console.warn(
`Invalid FAIL_RATE env var value: ${rate}. Must be a float between 0 and 1 inclusive.`
);
return;
}
const randomFailure = Math.random();
if (randomFailure < rate) {
throw new Error('Task failed.');
}
};
// Start script
main().catch(err => {
console.error(err);
process.exit(1); // Retry Job Task by exiting the process
});
Então eu tenho meu package.json
like então
{
"name": "jobs",
"version": "1.0.0",
"description": "Node.js sample for Cloud Run jobs",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"engines": {
"node": ">=16.0.0"
},
"author": "Google LLC",
"license": "Apache-2.0"
}
Esses arquivos foram retirados diretamente dos documentos, sem nenhuma alteração.
E eu estou conseguindo isso
Deployment failed
ERROR: (gcloud.run.deploy) Revision 'cloudruntest-00001-fdl' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.
Servidores web são legais e tudo mais, mas agora eu só quero executar um arquivo, só isso. O que eu preciso fazer para executar um arquivo no CloudRun?