Skip to content

Commit

Permalink
auto log send on crash (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzduniak committed May 10, 2019
1 parent 2c9ae4c commit c5f5d0d
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 9 deletions.
73 changes: 72 additions & 1 deletion README.md
Expand Up @@ -198,6 +198,16 @@ This code is also in [`demos/hello-world.js`](demos/hello-world.js), if you want
- [Properties](#properties-18)
- [PaymentBatchItem](#paymentbatchitem)
- [Properties](#properties-19)
- [Team](#team)
- [addMembers](#addmembers)
- [Parameters](#parameters-21)
- [Examples](#examples-24)
- [removeMember](#removemember)
- [Parameters](#parameters-22)
- [Examples](#examples-25)
- [listTeamMemberships](#listteammemberships)
- [Parameters](#parameters-23)
- [Examples](#examples-26)

### Bot

Expand Down Expand Up @@ -270,13 +280,14 @@ A collection of types used by the bot.

Options for initializing the bot.

Type: {verbose: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, botLite: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, disableTyping: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?}
Type: {verbose: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, botLite: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, disableTyping: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, autoLogSendOnCrash: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?}

##### Properties

- `verbose` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
- `botLite` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
- `disableTyping` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
- `autoLogSendOnCrash` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**

#### BotInfo

Expand Down Expand Up @@ -965,6 +976,66 @@ Type: {recipient: [string](https://developer.mozilla.org/docs/Web/JavaScript/Ref
- `amount` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `message` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?**

### Team

**Extends ClientBase**

The wallet module of your Keybase bot. For more info about the API this module uses, you may want to check out `keybase wallet api`.

#### addMembers

Add a bunch of people with different privileges to a team

##### Parameters

- `additions` **AddMembersParam** an array of the users to add, with privs

##### Examples

```javascript
bot.team
.addMembers({
team: 'phoenix',
emails: [{email: 'alice@keybase.io', role: 'writer'}, {email: 'cleo@keybase.io', role: 'admin'}],
usernames: [{username: 'frank', role: 'reader'}, {username: 'keybaseio@twitter', role: 'writer'}],
})
.then(res => console.log(res))
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<AddMembersResult>** \-

#### removeMember

Remove someone from a team

##### Parameters

- `removal` **RemoveMemberParam** object with the `team` name and `username`

##### Examples

```javascript
bot.team.removeMember({team: 'phoenix', username: 'frank'}).then(res => console.log(res))
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<RemoveMemberResult>** \-

#### listTeamMemberships

List a team's members

##### Parameters

- `team` **ListTeamMembershipsParam** an object with the `team` name in it

##### Examples

```javascript
bot.team.listTeamMemberships({team: 'phoenix'}).then(res => console.log(res))
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<ListTeamMembershipsResult>** \-

## Contributions

Make sure that you have Node, Yarn, and the Keybase application installed. We also use developer tools such as [EditorConfig](https://editorconfig.org), [ESLint](https://eslint.org), [Flow](https://flow.org), and [Prettier](https://prettier.io) so you'll probably want to make sure that your development is configured to use those tools somewhere in your code writing process.
Expand Down
46 changes: 42 additions & 4 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Large diffs are not rendered by default.

46 changes: 43 additions & 3 deletions lib/service/index.js
Expand Up @@ -16,12 +16,17 @@ class Service {
disableTyping: boolean
serviceLogFile: void | string
workingDir: string
autoLogSendOnCrash: boolean

_paperkey: void | string

constructor(workingDir: string) {
this.workingDir = workingDir
this.initialized = false
this.verbose = false
this.botLite = true
this.disableTyping = true
this.autoLogSendOnCrash = false
}

async init(username: string, paperkey: string, options?: InitOptions): Promise<void> {
Expand All @@ -37,11 +42,14 @@ class Service {
}

this.homeDir = this.workingDir
this.serviceLogFile = path.join(this.homeDir, 'Library', 'Logs', 'keybase.service.log')
this.serviceLogFile = path.join(this.homeDir, 'logs', 'keybase.service.log')
this.botLite = options ? Boolean(typeof options.botLite !== 'boolean' || options.botLite) : true
this.disableTyping = options
? Boolean(typeof options.disableTyping !== 'boolean' || options.disableTyping)
: true
this.autoLogSendOnCrash = options
? Boolean(typeof options.autoLogSendOnCrash === 'boolean' && options.autoLogSendOnCrash)
: false
// Unlike with clients we don't need to store the service, since it shuts down with ctrl stop
try {
await this.startupService()
Expand All @@ -62,6 +70,7 @@ class Service {
if (currentInfo && currentInfo.username && currentInfo.devicename) {
this.initialized = 'paperkey'
this.username = currentInfo.username
this._paperkey = paperkey
this.devicename = currentInfo.devicename
this.verbose = options ? Boolean(options.verbose) : false
}
Expand Down Expand Up @@ -152,7 +161,7 @@ class Service {
args.unshift('--home', this.homeDir)
}
if (this.serviceLogFile) {
args.unshift('-d', '--log-file', this.serviceLogFile)
args.unshift('--log-file', this.serviceLogFile)
}
if (this.botLite) {
args.unshift('--enable-bot-lite-mode')
Expand All @@ -162,8 +171,11 @@ class Service {

// keep track of the subprocess' state
this.running = true
child.on('exit', code => {
child.on('exit', async code => {
this.running = false
if (code !== 0 && this.autoLogSendOnCrash) {
await this.logSend()
}
})

return new Promise(async (resolve, reject) => {
Expand All @@ -184,6 +196,34 @@ class Service {
resolve()
})
}

async logSend(): Promise<void> {
const initiallyRunning = this.running
if (!initiallyRunning) {
try {
await this.startupService()
if (this.initialized === 'paperkey' && this.username) {
await keybaseExec(this.workingDir, this.homeDir, ['oneshot', '--username', this.username], {
stdinBuffer: this._paperkey,
})
}
} catch (e) {}
}

const feedback = `keybase-bot auto log send
username: ${this.username || 'none'}
initialized: ${this.initialized || 'false'}`

const args = ['log', 'send', '--no-confirm', '--feedback', feedback]
if (this.serviceLogFile) {
args.unshift('--log-file', this.serviceLogFile)
}
await keybaseExec(this.workingDir, this.homeDir, args)

if (!initiallyRunning) {
await this._killCustomService()
}
}
}

export default Service
2 changes: 2 additions & 0 deletions lib/utils/options.js
Expand Up @@ -9,4 +9,6 @@ export type InitOptions = {|
botLite?: boolean,
// Disable sending/receiving typing notifications to reduce bot bandwidth
disableTyping?: boolean,
// Automatically send logs on crash
autoLogSendOnCrash?: boolean,
|}

0 comments on commit c5f5d0d

Please sign in to comment.