Skip to content

Commit

Permalink
remove mytoken
Browse files Browse the repository at this point in the history
  • Loading branch information
woyoutlz committed Sep 20, 2019
1 parent a742386 commit e173948
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ let config = require('./config/config')
// server.addSub()
server.g.logger = new Logger('appserver', 'server')
agenda.g.logger = new Logger('appserver', 'agenda')
async function main(){
// lib init
async function main() {
// lib init
await agenda.init(config.mongo)
// sub init
await sub.init()

// start services
server.app.use('/', route)
// route要在start前
server.start()
server.start("0.0.0.0", config.port)
agenda.startAgenda()
}
main()
64 changes: 33 additions & 31 deletions sub/coin/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,67 @@ let fs = require('fs')
let path = require('path')
const Logger = require('../../lib/logger')
let init_file = path.join(__dirname, 'init.json')
let {get_mytoken} = require('../mytoken.js')
let {get_coinmarket} = require('../price.js')
let { get_mytoken } = require('../mytoken.js')
let { get_coinmarket } = require('../price.js')
let model = require('./model')
let Cacher = require('../../lib/cache_helper')

let request = require('request')
const util = require('util')
let r_get = util.promisify(request.get)
let {
cached
} = new Cacher()

let g = {
coins: [],
coins: [],
logger: new Logger('appserver', 'coin'),
start_sync: false,
}
let coin2id
let id2coin
async function sync_mytoken(){
async function sync_mytoken() {
let s = await get_mytoken()
if (s.code == 0 ){
await model.msg.findOneAndUpdate({"key":"mytoken"},{"key":"mytoken","value":JSON.stringify(s)}, {
if (s.code == 0) {
await model.msg.findOneAndUpdate({ "key": "mytoken" }, { "key": "mytoken", "value": JSON.stringify(s) }, {
upsert: true,
new: true
})
}
g.logger.log(JSON.stringify(s))
}
async function sync_coinmarket(){
async function sync_coinmarket() {
let s = await get_coinmarket()
if (s){
await model.msg.findOneAndUpdate({"key":"coinmarket"},{"key":"coinmarket","value":JSON.stringify(s)}, {
if (s) {
await model.msg.findOneAndUpdate({ "key": "coinmarket" }, { "key": "coinmarket", "value": JSON.stringify(s) }, {
upsert: true,
new: true
})
}
g.logger.log(JSON.stringify(s))
}
async function price(){
let r = await cached("price",3,null,async ()=>{
let s = await model.msg.findOne({"key":"mytoken"})
if (s){
return JSON.parse(s.value)
}else{
async function price() {
let r = await cached("price", 3, null, async () => {
let s = await r_get("https://app.cybex.io/price")
if (s) {
return JSON.parse(s.body)
} else {
return {}
}
})
return r
}
async function coinmarket_price(){
let r = await cached("coinmarket_price",60,null,async ()=>{
let s = await model.msg.findOne({"key":"coinmarket"})
if (s){
async function coinmarket_price() {
let r = await cached("coinmarket_price", 60, null, async () => {
let s = await model.msg.findOne({ "key": "coinmarket" })
if (s) {
return JSON.parse(s.value)
}else{
} else {
return {}
}
})
return r
}
async function init(){
async function init() {
try {
g.coins = require(init_file)
} catch (e) {
Expand All @@ -76,7 +78,7 @@ init()
function load_quotas() {
coin2id = _.mapValues(_.keyBy(g.coins, 'symbol'), i => i.id)
id2coin = _.mapValues(_.keyBy(g.coins, 'id'), i => i.symbol)
let { from,base_config } = readbase()
let { from, base_config } = readbase()
g.quotas = _.mapKeys(_.mapValues(base_config.bases, o => o.map(i => coin2id[i])), (v, k) => coin2id[k])
return from
}
Expand Down Expand Up @@ -136,16 +138,16 @@ async function sync_coins() {
}
function loadbase_config(base_id) {
return {
base:base_id,
data:g.quotas[base_id]
base: base_id,
data: g.quotas[base_id]
}
}
async function ticker(base, quote) {
let x = await cybex.raw('get_ticker', base, quote)
return x
}
async function loadbase(base_id) {
let quotes = loadbase_config(base_id)
let quotes = loadbase_config(base_id)
if (quotes) {
let a1 = quotes.data.map(i => ticker(base_id, i));
let s1 = await Promise.all(a1)
Expand All @@ -154,16 +156,16 @@ async function loadbase(base_id) {
}
return null
}
async function market_price(base_id){
async function market_price(base_id) {
let keyname = `market${base_id}`
let r = await cached(keyname,2,null,async ()=>{
let r = await cached(keyname, 2, null, async () => {
await cybex.init()
if (base_id){
if (base_id) {
let s = await loadbase(base_id)
return s
}else{
} else {
let base_ids = Object.keys(g.quotas)
let s = await Promise.all(base_ids.map(i=>loadbase(i)))
let s = await Promise.all(base_ids.map(i => loadbase(i)))
return s
}
})
Expand Down
14 changes: 7 additions & 7 deletions sub/coin/task.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
let {addTask} = require('../../lib/agenda_helper')
let {sync_once, sync_mytoken,sync_coinmarket} = require('./c')
let { addTask } = require('../../lib/agenda_helper')
let { sync_once, sync_mytoken, sync_coinmarket } = require('./c')

async function init (){
async function init() {
addTask('1 hours', 'coins_sync', async () => {
await sync_once()
})
addTask('5 seconds', 'sync_mytoken', async () => {
await sync_mytoken()
})
// addTask('5 seconds', 'sync_mytoken', async () => {
// await sync_mytoken()
// })
// addTask('10 minutes','sync_coinmarket',async () => {
// await sync_coinmarket()
// await sync_coinmarket()
// })
}

Expand Down

0 comments on commit e173948

Please sign in to comment.