|
| 1 | +/** |
| 2 | + * @fileoverview this program does a trivial job of writing a dummy string to an output |
| 3 | + */ |
| 4 | +const worker = require('@bazel/worker'); |
| 5 | + |
| 6 | +function runOneBuild(args, inputs) { |
| 7 | + // IMPORTANT don't log with console.out - stdout is reserved for the worker protocol. |
| 8 | + // This is true for any code running in the program, even if it comes from a third-party library. |
| 9 | + worker.log('Performing a build with args', args); |
| 10 | + if (inputs) { |
| 11 | + // The inputs help you manage a cache within the worker process |
| 12 | + // They are available only when run as a worker, not in standalone mode |
| 13 | + worker.log('We were run as a worker so we also got a manifest of all the inputs', inputs); |
| 14 | + } |
| 15 | + |
| 16 | + // Parse our arguments as usual. The worker library handles getting these out of the protocol |
| 17 | + // buffer. |
| 18 | + const [output] = args; |
| 19 | + require('fs').writeFileSync(output, 'Dummy output', {encoding: 'utf-8'}); |
| 20 | + |
| 21 | + // Return true if the tool succeeded, false otherwise. |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +if (require.main === module) { |
| 26 | + // One reason to run a program under a worker is that it takes a long time to start |
| 27 | + // Imagine that several seconds are spent here |
| 28 | + |
| 29 | + // Bazel will pass a special argument to the program when it's running us as a worker |
| 30 | + if (worker.runAsWorker(process.argv)) { |
| 31 | + worker.log('Running as a Bazel worker'); |
| 32 | + |
| 33 | + worker.runWorkerLoop(runOneBuild); |
| 34 | + } else { |
| 35 | + // Running standalone so stdout is available as usual |
| 36 | + console.log('Running as a standalone process'); |
| 37 | + |
| 38 | + // Help our users get on the fast path |
| 39 | + console.error( |
| 40 | + 'Started a new process to perform this action. Your build might be misconfigured, try --strategy=DoWork=worker'); |
| 41 | + |
| 42 | + // The first argument to the program is prefixed with '@' |
| 43 | + // because Bazel does that for param files. Strip it first. |
| 44 | + const paramFile = process.argv[2].replace(/^@/, ''); |
| 45 | + const args = require('fs').readFileSync(paramFile, 'utf-8').trim().split('\n'); |
| 46 | + |
| 47 | + // Bazel is just running the program as a single action, don't act like a worker |
| 48 | + if (!runOneBuild(args)) { |
| 49 | + process.exitCode = 1; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments