Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Cron-Near/croncat
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorJTClarke committed Jan 14, 2022
2 parents d17737f + 08386ce commit fec74a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ let croncatSettings = {}
const slackToken = process.env.SLACK_TOKEN || null
const slackProvider = new slack({ slackToken })
const notifySlack = text => {
if (slackToken) return slackProvider.send({
slackChannel: process.env.SLACK_CHANNEL,
text
})
try {
if (slackToken) return slackProvider.send({
slackChannel: process.env.SLACK_CHANNEL,
text
})
} catch (e) {
//
}
}

const pingHeartbeat = async () => {
Expand Down Expand Up @@ -75,8 +79,8 @@ export async function getAgentBalance() {
return balance
} catch (e) {
log(`${chalk.red('NEAR RPC Failed')}`)
notifySlack(`*Attention!* NEAR ${near_env} RPC Failed to retrieve balance!`)
return 0
await notifySlack(`*Attention!* NEAR ${near_env} RPC Failed to retrieve balance!`)
process.exit(1)
}
}

Expand Down Expand Up @@ -166,7 +170,7 @@ export async function checkAgentBalance(agentId) {
${chalk.bold.white('2. Use the web wallet to send funds: ')}${chalk.underline.blue(Near.config.walletUrl + '/send-money')}
${chalk.bold.white('3. Use NEAR CLI to send funds: ')} "near send OTHER_ACCOUNT ${AGENT_ACCOUNT_ID} ${(Big(BASE_GAS_FEE).mul(4))}"
`)
process.exit(1)
process.exit(0)
}
}

Expand All @@ -192,15 +196,15 @@ export async function refillAgentTaskBalance(options) {
const notEnough = Big(balance).lt(AGENT_MIN_TASK_BALANCE)
if (notEnough) {
log(`${chalk.red('Balance too low.')}`)
notifySlack(`*Attention!* Not enough balance to execute tasks, refill please.`)
await notifySlack(`*Attention!* Not enough balance to execute tasks, refill please.`)
process.exit(1)
} else {
log(`Agent Refilled, Balance: ${chalk.blue(utils.format.formatNearAmount(balance))}`)
notifySlack(`Agent Refilled, Balance: *${utils.format.formatNearAmount(balance)}*`)
await notifySlack(`Agent Refilled, Balance: *${utils.format.formatNearAmount(balance)}*`)
}
} catch (e) {
log(`${chalk.red('No balance to withdraw.')}`)
notifySlack(`*Attention!* No balance to withdraw.`)
await notifySlack(`*Attention!* No balance to withdraw.`)
process.exit(1)
}
}
Expand Down Expand Up @@ -258,8 +262,8 @@ export async function runAgentTick(options = {}) {

// Alert if agent changes status:
if (previousAgentSettings.status !== agentSettings.status) {
notifySlack(`*Agent Status Update:*\nYour agent is now a status of *${agentSettings.status}*`)
log(`Agent Status: ${chalk.white(agentSettings.status)}`)
await notifySlack(`*Agent Status Update:*\nYour agent is now a status of *${agentSettings.status}*`)

// TODO: At this point we could check if we need to re-register the agent if enough remaining balance, and status went from active to pending or none.
// NOTE: For now, stopping the process if no agent settings.
Expand Down Expand Up @@ -365,7 +369,7 @@ export async function agentFunction(method, args, isView, gas = BASE_GAS_FEE, am
log(`${chalk.bold.red('Attention!')}: ${chalk.redBright('Please add more funds to your account to continue sending transactions')}`)
log(`${chalk.bold.red('Current Account Balance:')}: ${chalk.redBright(utils.format.formatNearAmount(balance))}\n`)

notifySlack(`*Attention!* Please add more funds to your account to continue sending transactions.\nCurrent Account Balance: *${utils.format.formatNearAmount(balance)}*`)
await notifySlack(`*Attention!* Please add more funds to your account to continue sending transactions.\nCurrent Account Balance: *${utils.format.formatNearAmount(balance)}*`)
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/createSystemctl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require("path");
const { writeFileSync } = require("fs");
import chalk from 'chalk'

const generateDaemon = (env = 'testnet', user = 'near') => {
return `Description=CronCat ${env.toUpperCase()} Agent
Expand Down Expand Up @@ -28,6 +29,27 @@ export const createDaemonFile = async (env) => {
const daemon = generateDaemon(_env, user)

await writeFileSync(path.join(process.cwd(), `croncat_${_env}.service`), daemon)

// log next steps to user
const nextSteps = `
For the following steps, you can copy/paste the commands to finish setting up croncat daemon.
# 1. create the service symlink and then enable the service
${chalk.green(' sudo systemctl link ~/croncat/' + _env + '/croncat_' + _env + '.service')}
${chalk.green(' sudo systemctl enable croncat_' + _env + '.service')}
# 2. reload systemctl
${chalk.green(' sudo systemctl daemon-reload')}
# 3. start the service
${chalk.green(' sudo systemctl start croncat_' + _env + '.service')}
# 4. for accessing logs, you can use these commands, just make sure to use the right network name
${chalk.green(' journalctl -f -u croncat_' + _env + '.service')}
${chalk.green(' tail -f /var/log/croncat_' + _env + '.log')}
${chalk.green(' tail -f /var/log/croncat_' + _env + 'error.log')}
`
console.log(nextSteps)
}

// // NOTE: for testing
Expand Down
4 changes: 3 additions & 1 deletion src/slack.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
import axios from 'axios'

class Slack {
Expand All @@ -14,10 +15,11 @@ class Slack {

send(options = {}) {
const url = this.getHookUrl(options)
const env_name = process.env.NEAR_ENV || 'testnet'
if (!url) return
const data = {
channel: options.slackChannel ? `#${options.slackChannel}` : '#general',
username: 'Croncat',
username: `Croncat${env_name ? ' ' + env_name.toUpperCase() : ''}`,
// Example: 'Alert! You need to do something! <https://url.com|Click here>'
text: options.text || 'Croncat Update!',
icon_url: 'https://cron.cat/icons/icon-512x512.png',
Expand Down

0 comments on commit fec74a6

Please sign in to comment.