Skip to content

Commit

Permalink
Merge pull request #30 from CronCats/ft/agent-poc
Browse files Browse the repository at this point in the history
Ft/agent poc
  • Loading branch information
TrevorJTClarke authored Feb 24, 2022
2 parents fec74a6 + b07b5e2 commit e93cca7
Show file tree
Hide file tree
Showing 15 changed files with 792 additions and 466 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ AGENT_AUTO_RE_REGISTER=false
# Period between executing standard tasks, needs to be less than 60 seconds to be effective
WAIT_INTERVAL_MS=25000

# Period between checking triggers, needs to be less than 10 seconds to be effective
# Period will need to be consistent with RPC rate limits to ensure query goes through
TRIGGER_INTERVAL_MS=1000

# Period to check if a trigger needs executing, you should use a semi-fast value that doesnt break your RPC service.
VIEW_INTERVAL_MS=2000

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Commands:
croncat unregister <account_id> Account to remove from list of active agents.
croncat withdraw [account_id] Withdraw all rewards earned for this account
croncat status [account_id] Check agent status and balance for this account
croncat tasks Check how many tasks are currently available
croncat tasks Check tasks list
croncat triggers Check how many triggers are currently available
croncat go [account_id] Run tasks that are available, if agent is registered and has balance
croncat daemon [near_env] Generate a network specific croncat daemon service
```
Expand Down Expand Up @@ -80,6 +81,7 @@ You can then configure the following:
NODE_ENV=production
NEAR_ENV=mainnet
LOG_LEVEL=info
BETA_FEATURES=false
# The account registered as an agent
AGENT_ACCOUNT_ID=YOUR_ACCOUNT.near
Expand All @@ -93,6 +95,10 @@ AGENT_AUTO_RE_REGISTER=false
# The interval to wait between checking for tasks. Good intervals are below 60 seconds and above 10 seconds.
WAIT_INTERVAL_MS=450000
# Period between checking triggers, needs to be less than 10 seconds to be effective
# Period will need to be consistent with RPC rate limits to ensure query goes through
TRIGGER_INTERVAL_MS=10000
## Notify slack when events happen
SLACK_TOKEN=YOUR_WEBHOOK_TOKEN
SLACK_CHANNEL=general
Expand Down Expand Up @@ -162,6 +168,7 @@ Examples:
```
node bin/croncat tasks
node bin/croncat triggers
node bin/croncat register agent.in.testnet agent.in.testnet
node bin/croncat status agent.in.testnet
node bin/croncat go agent.in.testnet
Expand Down
92 changes: 50 additions & 42 deletions bin/croncat.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
require('dotenv').config()
const chalk = require('chalk')
const yargs = require('yargs')
import { utils } from 'near-api-js'
import getConfig from '../src/configuration'
import chalk from 'chalk'
import yargs from 'yargs'
import { createDaemonFile } from '../src/createSystemctl'
const { agentFunction, bootstrapAgent, runAgentTick, registerAgent } = require('../src/actions')
import * as config from '../src/configuration'
import * as entry from '../src/index'
import * as agent from '../src/agent'
import * as rpc from '../src/rpc'
import * as triggers from '../src/triggers'
import * as util from '../src/util'

const AGENT_ACCOUNT_ID = process.env.AGENT_ACCOUNT_ID
const AGENT_ACCOUNT_ID = config.AGENT_ACCOUNT_ID

const registerAgentCmd = {
command: 'register <account_id> [payable_account_id]',
Expand All @@ -23,10 +26,9 @@ const registerAgentCmd = {
required: false
}),
handler: async options => {
// await agentFunction('register_agent', options, false, undefined, 1e25);
await registerAgent(options.account_id, options.payable_account_id || options.account_id, options);
await agent.registerAgent(options.account_id, options.payable_account_id)
}
};
}

const updateAgent = {
command: 'update <account_id> [payable_account_id]',
Expand All @@ -43,9 +45,9 @@ const updateAgent = {
required: false
}),
handler: async options => {
await agentFunction('update_agent', options);
await rpc.call('update_agent', options)
}
};
}

const unregisterAgent = {
command: 'unregister <account_id>',
Expand All @@ -57,9 +59,9 @@ const unregisterAgent = {
required: true
}),
handler: async options => {
await agentFunction('unregister_agent', options, false, undefined, '0.000000000000000000000001')
await rpc.call('unregister_agent', options, false, undefined, '0.000000000000000000000001')
}
};
}

const withdrawBalance = {
command: 'withdraw [account_id]',
Expand All @@ -71,10 +73,10 @@ const withdrawBalance = {
required: false
}),
handler: async options => {
if (!options.account_id) options.account_id = AGENT_ACCOUNT_ID
await agentFunction('withdraw_task_balance', options);
if (!options.account_id) options.account_id = config.AGENT_ACCOUNT_ID
await rpc.call('withdraw_task_balance', options)
}
};
}

const status = {
command: 'status [account_id]',
Expand All @@ -86,37 +88,42 @@ const status = {
required: false
}),
handler: async options => {
if (!options.account_id && AGENT_ACCOUNT_ID) options.account_id = AGENT_ACCOUNT_ID
await agentFunction('get_agent', options, true);
if (!options.account_id && config.AGENT_ACCOUNT_ID) options.account_id = config.AGENT_ACCOUNT_ID
await rpc.call('get_agent', options, true)
}
};
}

const tasks = {
const tasksCmd = {
command: 'tasks',
desc: 'Check how many tasks are currently available',
builder: (yargs) => yargs,
handler: async options => {
await agentFunction('get_slot_tasks', options, true);
// Deprecated in favor of web UI
// await rpc.call('get_slot_tasks', options, true)
const manager = await util.getCronManager()
const { networkId } = manager.account.connection
console.log(`${chalk.gray('View ' + networkId.toLowerCase() + ' Tasks:')} ${chalk.blueBright('https://cron.cat/tasks?network=' + networkId.toLowerCase())}`)
}
};
}

const go = {
command: 'go [account_id]',
desc: 'Run tasks that are available, if agent is registered and has balance',
builder: (yargs) => yargs
.option('account_id', {
desc: 'Account to check',
type: 'string',
required: false
}),
const triggersCmd = {
command: 'triggers',
desc: 'Check how many triggers are currently available',
builder: (yargs) => yargs,
handler: async options => {
if (!options.account_id && AGENT_ACCOUNT_ID) options.account_id = AGENT_ACCOUNT_ID
await bootstrapAgent(options.account_id, options)
await triggers.getTriggers()
}
}

const go = {
command: 'go',
desc: 'Run all types of tasks that are available',
builder: (yargs) => yargs,
handler: async options => {
// MAIN AGENT LOOP
runAgentTick(options)
entry.runMainLoop()
}
};
}

const daemon = {
command: 'daemon [near_env]',
Expand All @@ -131,9 +138,9 @@ const daemon = {
const env = options.near_env || 'testnet'
await createDaemonFile(env)
}
};
}

const config = getConfig(process.env.NODE_ENV || 'development')
const configd = config.getConfig(process.env.NODE_ENV || 'development')
yargs // eslint-disable-line
.strict()
.scriptName('croncat')
Expand All @@ -148,12 +155,12 @@ yargs // eslint-disable-line
.option('nodeUrl', {
desc: 'NEAR node URL',
type: 'string',
default: config.nodeUrl
default: configd.nodeUrl
})
.option('networkId', {
desc: 'NEAR network ID, allows using different keys based on network',
type: 'string',
default: config.networkId
default: configd.networkId
})
.option('helperUrl', {
desc: 'NEAR contract helper URL',
Expand All @@ -173,14 +180,15 @@ yargs // eslint-disable-line
.command(unregisterAgent)
.command(withdrawBalance)
.command(status)
.command(tasks)
.command(tasksCmd)
.command(triggersCmd)
.command(go)
.command(daemon)
.config(config)
.config(configd)
.showHelpOnFail(true)
.recommendCommands()
.demandCommand(1, chalk`Pass {bold --help} to see all available commands and options.`)
.usage(chalk`Usage: {bold $0 <command> [options]}`)
.epilogue(chalk`More info: {bold https://cron.cat}`)
.wrap(null)
.argv;
.argv
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "croncat",
"version": "1.6.4",
"version": "1.7.0-rc3",
"description": "cron.cat CLI and Agent Runner",
"main": "src/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit e93cca7

Please sign in to comment.