From e60f3b9a0dab6ecfb02814591775780c26678727 Mon Sep 17 00:00:00 2001 From: trevorjtclarke Date: Thu, 16 Dec 2021 13:01:28 -0800 Subject: [PATCH] added helper logging, more process stability --- package.json | 2 +- src/actions.js | 28 ++++++++++++++++------------ src/createSystemctl.js | 22 ++++++++++++++++++++++ src/slack.js | 4 +++- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index a1f7d4f..f28ae0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "croncat", - "version": "1.6.3", + "version": "1.6.4", "description": "cron.cat CLI and Agent Runner", "main": "src/index.js", "scripts": { diff --git a/src/actions.js b/src/actions.js index 0701c28..c64eb8b 100644 --- a/src/actions.js +++ b/src/actions.js @@ -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 () => { @@ -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) } } @@ -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) } } @@ -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) } } @@ -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. @@ -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)}*`) } } } diff --git a/src/createSystemctl.js b/src/createSystemctl.js index 75f4f56..5357b70 100644 --- a/src/createSystemctl.js +++ b/src/createSystemctl.js @@ -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 @@ -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 diff --git a/src/slack.js b/src/slack.js index 3a7ec94..3c4db68 100644 --- a/src/slack.js +++ b/src/slack.js @@ -1,3 +1,4 @@ +require('dotenv').config() import axios from 'axios' class Slack { @@ -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! ' text: options.text || 'Croncat Update!', icon_url: 'https://cron.cat/icons/icon-512x512.png',