Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 11x 11x 11x 11x 11x 11x 11x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 11x 11x 11x 9x 9x 11x 11x 11x 11x 11x 11x 193x 193x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 193x 2x 2x 2x 2x 2x 2x 2x 193x 6x 6x 6x 193x 2x 2x 2x 193x 2x 2x 2x 193x 2x 2x 2x | import { ApplicationLogStream } from '@clevercloud/client/esm/streams/application-logs.js';
import { ResourceLogStream } from '@clevercloud/client/esm/streams/resource-logs.js';
import { config } from '../config/config.js';
import { styleText } from '../lib/style-text.js';
import { Logger } from '../logger.js';
import { waitForDeploymentEnd, waitForDeploymentStart } from './deployments.js';
import { getBest } from './domain.js';
import * as ExitStrategy from './exit-strategy-option.js';
import { JsonArray } from './json-array.js';
import { getHostAndTokens, processError } from './send-to-api.js';
import { Deferred } from './utils.js';
const RESET_COLOR = '\x1B[0m';
// 2000 logs per 100ms maximum
const THROTTLE_ELEMENTS = 2000;
const THROTTLE_PER_IN_MILLISECONDS = 100;
const retryConfiguration = {
enabled: true,
initRetryTimeout: 3000,
maxRetryCount: 10,
};
export async function displayLogs(params) {
const deferred = params.deferred || new Deferred();
const { apiHost, tokens } = await getHostAndTokens();
const { ownerId, appId, addonId, filter, since, until, format } = params;
// deploymentId only applies to apps
const deploymentId = addonId != null ? undefined : params.deploymentId;
if (format === 'json' && until == null) {
throw new Error('"json" format is only applicable with a limiting parameter such as `--until`');
}
const commonStreamParams = {
apiHost,
tokens,
ownerId,
connectionTimeout: 10_000,
retryConfiguration,
since,
until,
deploymentId,
filter,
throttleElements: THROTTLE_ELEMENTS,
throttlePerInMilliseconds: THROTTLE_PER_IN_MILLISECONDS,
};
const logStream =
addonId != null
? new ResourceLogStream({ ...commonStreamParams, addonId })
: new ApplicationLogStream({ ...commonStreamParams, appId });
// Properly close the stream
process.once('SIGINT', (signal) => {
logStream.close(signal);
process.kill(process.pid, 'SIGINT');
});
const jsonArray = new JsonArray();
logStream
.on('open', () => {
Logger.debug(styleText('blue', `Logs stream (open) ${JSON.stringify({ appId, addonId, filter, deploymentId })}`));
if (format === 'json') {
jsonArray.open();
}
})
.on('error', (event) => {
Logger.debug(styleText('red', `Logs stream (error) ${event.error.message}`));
})
.onLog((log) => {
switch (format) {
case 'json':
jsonArray.push(log);
return;
case 'json-stream':
Logger.println(JSON.stringify(log));
return;
case 'human':
default:
if (log.message === RESET_COLOR) {
return;
}
Logger.println(formatLogLine(log));
}
});
// start() is blocking until end of stream
logStream
.start()
.then(() => {
if (format === 'json') {
jsonArray.close();
}
return deferred.resolve();
})
.catch(processError)
.catch((error) => deferred.reject(error));
return logStream;
}
export async function watchDeploymentAndDisplayLogs(options) {
const { ownerId, appId, deploymentId, commitId, knownDeployments, quiet, redeployDate, exitStrategy } = options;
ExitStrategy.plotQuietWarning(exitStrategy, quiet);
// If in quiet mode, we only log start/finished deployment messages
if (!quiet) {
Logger.println(` ${styleText('blue', '→ Waiting for deployment to start…')}`);
}
const deployment = await waitForDeploymentStart({ ownerId, appId, deploymentId, commitId, knownDeployments });
Logger.println(` ${styleText('green', `✓ Deployment started ${styleText('grey', `(${deployment.uuid})`)}`)}`);
if (exitStrategy === 'deploy-start') {
return;
}
const deferred = new Deferred();
let logsStream;
if (!quiet) {
// About the deferred…
// If displayLogs() throws an error,
// the async function we're in (watchDeploymentAndDisplayLogs) will stop here and the error will be passed to the parent.
// displayLogs() defines callback listeners so if it catches error in those callbacks,
// it has no proper way to bubble up the error here.
// Using the deferred enables this.
logsStream = await displayLogs({ ownerId, appId, deploymentId: deployment.uuid, since: redeployDate, deferred });
}
if (!quiet) {
Logger.println(` ${styleText('blue', '→ Waiting for application logs…')}`);
}
// Wait for deployment end (or an error thrown by logs with the deferred)
const deploymentEnded = await Promise.race([
waitForDeploymentEnd({ ownerId, appId, deploymentId: deployment.uuid }),
deferred.promise,
]);
if (!quiet && exitStrategy !== 'never') {
logsStream.close(quiet ? 'quiet' : 'follow');
}
// deploymentEnded can be undefined if deferred resolved (e.g., stream closed via SIGINT)
if (deploymentEnded == null) {
return;
}
if (deploymentEnded.state === 'OK') {
Logger.println('');
// There can be applications without any domain, so we don't fail if we can't get one
const favouriteDomain = await getBest(appId, ownerId).catch(() => null);
if (favouriteDomain) {
Logger.println(
`${styleText(['bold', 'green'], '✓ Access your application:')} ${styleText(['underline', 'bold'], `https://${favouriteDomain.fqdn}`)}`,
);
}
Logger.println(
`${styleText(['bold', 'blue'], '→ Manage your application:')} ${styleText(['underline', 'bold'], `${config.GOTO_URL}/${appId}`)}`,
);
} else if (deploymentEnded.state === 'CANCELLED') {
throw new Error('Deployment was cancelled. Please check the activity');
} else {
throw new Error('Deployment failed. Please check the logs');
}
}
function formatLogLine(log) {
const { date, message } = log;
if (isDeploymentSuccessMessage(log)) {
return `${date.toISOString()}: ${styleText(['bold', 'green'], message)}`;
} else if (isDeploymentFailedMessage(log)) {
return `${date.toISOString()}: ${styleText(['bold', 'red'], message)}`;
} else if (isBuildSuccessMessage(log)) {
return `${date.toISOString()}: ${styleText(['bold', 'blue'], message)}`;
}
return `${date.toISOString()}: ${message}${RESET_COLOR}`;
}
function isCleverMessage(log) {
return log.service !== 'bas-deploy.service';
}
function isDeploymentSuccessMessage(log) {
return isCleverMessage(log) && log.message.toLowerCase().startsWith('successfully deployed in');
}
function isDeploymentFailedMessage(log) {
return isCleverMessage(log) && log.message.toLowerCase().startsWith('deploy failed in');
}
function isBuildSuccessMessage(log) {
return isCleverMessage(log) && log.message.toLowerCase().startsWith('build succeeded in');
}
|