兼容Node.js执行

This commit is contained in:
marvzhang
2020-01-03 14:21:47 +08:00
parent 7076985898
commit 7310df1be6
4 changed files with 82 additions and 13 deletions

View File

@@ -19,6 +19,7 @@ import (
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"syscall"
"time"
@@ -104,6 +105,17 @@ func AssignTask(task model.Task) error {
// 设置环境变量
func SetEnv(cmd *exec.Cmd, envs []model.Env, taskId string, dataCol string) *exec.Cmd {
// 默认把Node.js的全局node_modules加入环境变量
envPath := os.Getenv("PATH")
for _, _path := range strings.Split(envPath, ":") {
if strings.Contains(_path, "/.nvm/versions/node/") {
pathNodeModules := strings.Replace(_path, "/bin", "/lib/node_modules", -1)
_ = os.Setenv("PATH", pathNodeModules+":"+envPath)
_ = os.Setenv("NODE_PATH", pathNodeModules)
break
}
}
// 默认环境变量
cmd.Env = append(os.Environ(), "CRAWLAB_TASK_ID="+taskId)
cmd.Env = append(cmd.Env, "CRAWLAB_COLLECTION="+dataCol)

View File

@@ -51,6 +51,10 @@
</el-form>
</el-row>
<el-row class="button-container" v-if="!isView">
<el-button size="normal" v-if="isShowRun" type="danger" @click="onCrawl"
icon="el-icon-video-play" style="margin-right: 10px">
{{$t('Run')}}
</el-button>
<el-upload
v-if="spiderForm.type === 'customized'"
:action="$request.baseUrl + `/spiders/${spiderForm._id}/upload`"
@@ -65,10 +69,6 @@
{{$t('Upload')}}
</el-button>
</el-upload>
<el-button size="normal" v-if="isShowRun" type="danger" @click="onCrawl"
icon="el-icon-video-play">
{{$t('Run')}}
</el-button>
<el-button size="normal" type="success" @click="onSave"
icon="el-icon-check">
{{$t('Save')}}

View File

@@ -3,6 +3,7 @@
<el-form inline>
<el-form-item>
<el-autocomplete
v-if="activeLang.executable_name === 'python'"
v-model="depName"
style="width: 240px"
:placeholder="$t('Search Dependencies')"
@@ -11,6 +12,13 @@
:disabled="isShowInstalled"
@select="onSearch"
/>
<el-input
v-else
v-model="depName"
style="width: 240px"
:placeholder="$t('Search Dependencies')"
:disabled="isShowInstalled"
/>
</el-form-item>
<el-form-item>
<el-button
@@ -26,7 +34,7 @@
<el-checkbox v-model="isShowInstalled" :label="$t('Show installed')" @change="onIsShowInstalledChange"/>
</el-form-item>
</el-form>
<el-tabs v-model="activeTab">
<el-tabs v-model="activeTab" @tab-click="onTabChange">
<el-tab-pane v-for="lang in langList" :key="lang.name" :label="lang.name" :name="lang.executable_name"/>
</el-tabs>
<template v-if="activeLang.installed">
@@ -59,6 +67,7 @@
<el-button
v-if="!scope.row.installed"
v-loading="getDepLoading(scope.row)"
:disabled="getDepLoading(scope.row)"
size="mini"
type="primary"
@click="onClickInstallDep(scope.row)"
@@ -68,6 +77,7 @@
<el-button
v-else
v-loading="getDepLoading(scope.row)"
:disabled="getDepLoading(scope.row)"
size="mini"
type="danger"
@click="onClickUninstallDep(scope.row)"
@@ -82,8 +92,11 @@
<div class="install-wrapper">
<h3>{{activeLang.name + $t(' is not installed, do you want to install it?')}}</h3>
<el-button
v-loading="isLoadingInstallLang"
:disabled="isLoadingInstallLang"
type="primary"
style="width: 240px;font-weight: bolder;font-size: 18px"
@click="onClickInstallLang"
>
{{$t('Install')}}
</el-button>
@@ -108,7 +121,8 @@ export default {
loading: false,
isShowInstalled: false,
installedDepList: [],
depLoadingDict: {}
depLoadingDict: {},
isLoadingInstallLang: false
}
},
computed: {
@@ -123,6 +137,9 @@ export default {
}
return {}
},
activeLangName () {
return this.activeLang.executable_name
},
computedDepList () {
if (this.isShowInstalled) {
return this.installedDepList
@@ -139,12 +156,19 @@ export default {
dep_name: this.depName
})
this.loading = false
this.depList = res.data.data.sort((a, b) => a.name > b.name ? 1 : -1)
this.depList.map(async dep => {
const res = await this.$request.get(`/system/deps/${this.activeLang.executable_name}/${dep.name}/json`)
dep.version = res.data.data.version
dep.description = res.data.data.description
})
this.depList = res.data.data
if (this.activeLangName === 'python') {
// 排序
this.depList = this.depList.sort((a, b) => a.name > b.name ? 1 : -1)
// 异步获取python附加信息
this.depList.map(async dep => {
const res = await this.$request.get(`/system/deps/${this.activeLang.executable_name}/${dep.name}/json`)
dep.version = res.data.data.version
dep.description = res.data.data.description
})
}
},
async getInstalledDepList () {
this.loading = true
@@ -172,6 +196,9 @@ export default {
onIsShowInstalledChange (val) {
if (val) {
this.getInstalledDepList()
} else {
this.depName = ''
this.depList = []
}
},
async onClickInstallDep (dep) {
@@ -226,6 +253,32 @@ export default {
return false
}
return this.depLoadingDict[name]
},
async onClickInstallLang () {
this.isLoadingInstallLang = true
const res = await this.$request.post(`/nodes/${this.nodeForm._id}/langs/install`, {
lang: this.activeLang.executable_name
})
if (!res || res.error) {
this.$notify.error({
title: this.$t('Installing language failed'),
message: this.$t('The language installation is unsuccessful: ') + this.activeLang.name
})
} else {
this.$notify.success({
title: this.$t('Installing language successful'),
message: this.$t('You have successfully installed a language: ') + this.activeLang.name
})
}
this.isLoadingInstallLang = false
},
onTabChange () {
if (this.isShowInstalled) {
this.getInstalledDepList()
} else {
this.depName = ''
this.depList = []
}
}
},
async created () {

View File

@@ -300,7 +300,7 @@ export default {
'Disclaimer': '免责声明',
'Please search dependencies': '请搜索依赖',
'No Data': '暂无数据',
'Show installed': '看已安装',
'Show installed': '看已安装',
'Installing dependency successful': '安装依赖成功',
'Installing dependency failed': '安装依赖失败',
'You have successfully installed a dependency: ': '您已成功安装依赖: ',
@@ -309,6 +309,10 @@ export default {
'Uninstalling dependency failed': '卸载依赖失败',
'You have successfully uninstalled a dependency: ': '您已成功卸载依赖: ',
'The dependency uninstallation is unsuccessful: ': '卸载依赖失败: ',
'Installing language successful': '安装语言成功',
'Installing language failed': '安装语言失败',
'You have successfully installed a language: ': '您已成功安装语言: ',
'The language installation is unsuccessful: ': '安装语言失败: ',
// 登录
'Sign in': '登录',