Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add healthcheck command #122

Merged
merged 4 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- checkout
- restore_cache: &restore_cache
keys:
- v1-yarn-{{checksum ".circleci/config.yml"}}-{{ checksum "yarn.lock"}}
- v1-yarn-{{checksum ".circleci/config.yml"}}-{{ checksum "package-lock.json"}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

- v1-yarn-{{checksum ".circleci/config.yml"}}
- run: .circleci/greenkeeper
- run: yarn add -D nyc@11 @oclif/nyc-config@1 mocha-junit-reporter@1
Expand Down
37 changes: 37 additions & 0 deletions src/commands/health-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {flags} from '@oclif/command'
import * as colors from 'colors'
import Constants from '../services/constants'
import healthCheck from '../utils/health-checker'
import PercyCommand from './percy-command'

export default class HealthCheck extends PercyCommand {
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved
static description = 'Determins if the Percy Agent process is currently running'
static hidden = true

static flags = {
port: flags.integer({
char: 'p',
default: Constants.PORT,
description: 'port',
}),
}

static examples = [
'$ percy healthcheck',
'$ percy healthcheck --port 6884',
]

async run() {
await super.run()

// If Percy is disabled or is missing a token, gracefully exit here
if (!this.percyWillRun()) { this.exit(0) }
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved

const {flags} = this.parse(HealthCheck)
const port = flags.port as number

await healthCheck(port, {
shouldRetry: () => false,
})
}
}
3 changes: 2 additions & 1 deletion src/utils/health-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import Axios from 'axios'
import * as retryAxios from 'retry-axios'
import logger from './logger'

async function healthCheck(port: number) {
async function healthCheck(port: number, retryOptions?: object) {
const healthcheckUrl = `http://localhost:${port}/percy/healthcheck`

const retryConfig = {
retry: 5, // with exponential back off
retryDelay: 500,
shouldRetry: () => true,
...retryOptions,
}

const interceptorId = retryAxios.attach()
Expand Down
52 changes: 52 additions & 0 deletions test/commands/health-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as chai from 'chai'
import {describe} from 'mocha'
import * as nock from 'nock'
import HealthCheck from '../../src/commands/health-check'
import {captureStdErr, captureStdOut} from '../helpers/stdout'

const expect = chai.expect

describe('Health check', () => {
describe('when agent is running', () => {
beforeEach(() => {
nock(/localhost/).get('/percy/healthcheck').reply(200)
})

afterEach(() => {
nock.cleanAll()
})

it('messages that agent is ready', async () => {
const stdout = await captureStdOut(async () => HealthCheck.run([]))

expect(stdout).to.contain('[percy] percy is ready.')
})
})

describe('when agent is not running', () => {
beforeEach(() => {
nock(/localhost/).get('/percy/healthcheck').reply(500)
})

afterEach(() => {
nock.cleanAll()
})

it('messages that agent is not ready', async () => {
const errorStdout = await captureStdErr(async () => HealthCheck.run([]))

expect(errorStdout).to
.contain('[percy] Failed to establish a connection with http://localhost:5338/percy/healthcheck')
})

it('properly passes the port', async () => {
const port = '5899'
const errorStdout = await captureStdErr(async () =>
HealthCheck.run(['--port', port]),
)

expect(errorStdout).to
.contain('[percy] Failed to establish a connection with http://localhost:5899/percy/healthcheck')
})
})
})