Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Commit

Permalink
Remove babel. Fixup SIGINT handling. Debugging ormap/mvreg problem.
Browse files Browse the repository at this point in the history
Blocked on a peer-base issue:

peer-base/peer-base#287
  • Loading branch information
jimpick committed May 25, 2019
1 parent a522227 commit 1d2281a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ yarn.lock
yarn-error.log
node_modules
*.swp
dist
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
!dist
81 changes: 53 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env node

import { homedir } from 'os'
import path from 'path'
import { formatWithOptions } from 'util'
import produce from 'immer'
import nanobus from 'nanobus'
import nanostate from 'nanostate'
import delay from 'delay'
import { mineshaftStart, mineshaftStop } from '@jimpick/filecoin-pickaxe-mineshaft'

const activeRequests = new Map()
const { homedir } = require('os')
const path = require('path')
const { formatWithOptions } = require('util')
const produce = require('immer').default
const nanobus = require('nanobus')
const nanostate = require('nanostate')
const delay = require('delay')
const { mineshaftStart, mineshaftStop } = require('@jimpick/filecoin-pickaxe-mineshaft')

const bus = nanobus()

Expand All @@ -21,29 +19,46 @@ const statesAndTransitions = {
done: {}
}

bus.on('newState', ({ dealRequests }) => {
// console.log('New state')
const active = new Set()

bus.on('newState', ({ dealRequests }, context) => {
for (const dealRequestId in dealRequests) {
if (!activeRequests.has(dealRequestId)) {
const dealRequest = dealRequests[dealRequestId]
activeRequests.set(dealRequestId, dealRequest)
bus.emit('newDealRequest', dealRequestId)
const dealRequest = dealRequests[dealRequestId]
if (!dealRequest.agentState && !active.has(dealRequestId)) {
active.add(dealRequestId)
bus.emit('newDealRequest', dealRequestId, dealRequest, context)
}
}
})

bus.on('newDealRequest', async dealRequestId => {
const dealRequest = activeRequests.get(dealRequestId)
bus.on('newDealRequest', async (dealRequestId, dealRequest, context) => {
console.log('New deal request', dealRequestId, dealRequest)
const machine = nanostate('started', statesAndTransitions)
while (machine.state !== 'done') {
console.log('Entered:', machine.state, dealRequestId)
updateDealRequestState()
console.log('Jim1', dealRequestId)
await delay(1000)
console.log('Jim2', dealRequestId)
machine.emit('next')
console.log('Jim3', dealRequestId)
}
console.log('Done', dealRequestId)
updateDealRequestState()

function updateDealRequestState () {
const record = {
state: machine.state
}
context.dealRequests.applySub(
dealRequestId, 'ormap', 'applySub',
`agentState`, 'mvreg', 'write',
JSON.stringify(record)
)
}
})


async function run () {
const configFile = path.resolve(
homedir(),
Expand All @@ -63,6 +78,14 @@ async function run () {
dealRequests.shared.on('state changed', printDealRequests)
minerDealRequests.shared.on('state changed', printMinerDealRequests)

const context = {
mineshaft,
bundles: mineshaft.collaboration.shared,
bundleImports: bundleImports.shared,
dealRequests: dealRequests.shared,
minerDealRequests: minerDealRequests.shared
}

function printCollab () {
// console.log('collaboration', mineshaft.collaboration.shared.value())
}
Expand Down Expand Up @@ -115,28 +138,30 @@ async function run () {
// dealRequests
const rawDealRequests = dealRequests.shared.value()
const formattedDealRequests = {}
Object.keys(rawDealRequests).forEach(dealRequestId => {
for (const dealRequestId in rawDealRequests) {
const rawDealRequest = rawDealRequests[dealRequestId]
const formatted = {}
formatted.dealRequest = JSON.parse(
[...rawDealRequest.dealRequest][0]
)
for (const propKey in rawDealRequest) {
const propValue = rawDealRequest[propKey]
formatted[propKey] = JSON.parse([...propValue][0])
}
formattedDealRequests[dealRequestId] = formatted
})
}
draft.dealRequests = formattedDealRequests
})
state = newState
// console.log('New state', formatWithOptions(state, { depth: Infinity }))
/*
console.log(
'New state',
formatWithOptions({ colors: true, depth: Infinity }, '%O', state)
)
*/
bus.emit('newState', state)
bus.emit('newState', state, context)
}
}

run()

process.on('beforeExit', mineshaftStop)
process.on('SIGINT', async () => {
console.log('Exiting...')
await mineshaftStop()
console.log('Done.')
})
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
"version": "0.0.1",
"description": "Agent to perform requested Filecoin tasks",
"scripts": {
"dev": "nodemon --ignore dist --exec babel-node index.js",
"build": "babel *.js --out-dir dist",
"prepublish": "npm run build"
"dev": "nodemon node index.js",
},
"bin": {
"pickaxe-agent": "dist/index.js"
"pickaxe-agent": "index.js"
},
"keywords": [],
"author": "Jim Pick (@jimpick)",
Expand All @@ -24,10 +22,6 @@
"peer-base": "peer-base/peer-base#fix/load-ormap-causal-context"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.4.3",
"babel-plugin-shebang": "^1.0.0",
"nodemon": "^1.18.11"
},
Expand Down

0 comments on commit 1d2281a

Please sign in to comment.