https://geeknote.net/nocode
NoCode
/assets/avatar-c195dbe387bf2d666eedca03bc60e64574323f2a.png
2022-10-28T15:05:22Z
nocode
https://geeknote.net/nocode
https://geeknote.net/nocode/posts/1593
2022-10-11T04:08:46Z
2022-10-28T15:05:22Z
npm postinstall 场景Docker构建优化
<p>有些项目中 npm 会用到postinstall这个配置在安装完依赖做一些事情,这样在Docker 构建的时候,想缓存依赖的话会麻烦点,必须把代码整个复制,不然跑postinstall的就可能失败,可以使用 --ignore-scripts 优化,把postinstall 的逻辑拆开,安装完依赖后跑</p>
<p>比如</p>
<pre class="highlight"><code>{
"name": "dataengine-doc-front",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "NEXT_PUBLIC_APP_ENV=local next dev",
"postinstall": "npm run generate:nav",
"generate:nav": "ts-node -O '{\"module\": \"commonjs\"}' scripts/generate-navigation.ts",
"dev2": "NEXT_PUBLIC_APP_ENV=development next dev",
"build": "npm run generate:nav && next build",
"start": "next start --port 30000"
},
...
</code></pre>
<p>优化改动</p>
<p><img src="/attachments/tLk4inFjgN8n6r7aYmuyqKjx/output.png" alt="output.png"></p>
<p>优化点</p>
<ol>
<li>用阿里云的docker registry</li>
<li>主要缓存依赖</li>
<li>安装依赖的时候使用 --ignore-scripts,不会执行postinstall,因为步骤2只复制了package.json,不用 --ignore-scripts的话,执行postinstall会失败</li>
<li>设置 --ignore-scripts为fasle,执行下esbuild的install.js,因为之前安装依赖的时候--ignore-scripts 为true了,esbuild安装并没有执行install.js,所以需要手动执行esbuild的install.js</li>
</ol>
<p>其他方案</p>
<ul>
<li>package.json 不使用postinstall</li>
</ul>
<p>其他优化点</p>
<p>这个项目用了next
<a href="https://stackoverflow.com/questions/69400243/whats-the-difference-between-npm-run-dev-and-npm-run-start-in-next-js">应该可以用next export 导出html,做多阶段构建,减少镜像体积</a></p>
有些项目中 npm 会用到postinstall这个配置在安装完依赖做一些事情,这样在Docker 构建的时候,想缓存依赖的话会麻烦点,必须把代码整个复制,不然跑postinstall的就可能失...
NoCode
https://geeknote.net/nocode
https://geeknote.net/nocode/posts/1486
2022-09-09T14:16:23Z
2022-10-28T15:03:17Z
docker ubuntu 20.04 config timezone
<pre class="highlight"><code>ENV TZ=Asia/Shanghai
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
&& apt-get update \
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get install tzdata \
&& apt-get clean \
&& apt-get autoclean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
</code></pre>
<p>参考</p>
<ul>
<li><a href="https://dev.to/0xbf/set-timezone-in-your-docker-image-d22">https://dev.to/0xbf/set-timezone-in-your-docker-image-d22</a></li>
</ul>
ENV TZ=Asia/Shanghai
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list...
NoCode
https://geeknote.net/nocode
https://geeknote.net/nocode/posts/275
2021-09-09T09:03:38Z
2022-10-28T14:39:52Z
在PG中使用JavaScript
<p>在PG中使用JavaScript 需要使用 plv8 扩展</p>
<p>需要开启扩展</p>
<pre class="highlight"><code>create extension plv8;
</code></pre>
<p>创建函数模版</p>
<pre class="highlight"><code>create or replace function function_name()
returns void as $$
// V8 Javascript
// 在这里写 Javascript Code
$$ language plv8;
</code></pre>
<p>执行的话运行</p>
<pre class="highlight"><code>select function_name();
</code></pre>
<p>plv8 helloworld 示例</p>
<pre class="highlight"><code>create or replace function hello_world(name text)
returns text as $$
let output = `Hello, ${name}!`;
return output;
$$ language plv8;
</code></pre>
<p>执行sql 使用 <code>plv8.execute</code> 函数</p>
<pre class="highlight"><code>create or replace function update_user(id bigint, first_name text)
returns smallint as $$
var num_affected = plv8.execute(
'update profiles set first_name = $1 where id = $2',
[first_name, id]
);
return num_affected;
$$ language plv8;
</code></pre>
<h2>
<a id="links" href="#links" class="anchor"></a>links</h2>
<ul>
<li><a href="https://supabase.io/docs/guides/database/extensions/plv8">https://supabase.io/docs/guides/database/extensions/plv8</a></li>
<li><a href="https://github.com/nelhage/plv8js/blob/master/doc/plv8.md">https://github.com/nelhage/plv8js/blob/master/doc/plv8.md</a></li>
</ul>
在PG中使用JavaScript 需要使用 plv8 扩展
需要开启扩展
create extension plv8;
创建函数模版
create or replace function fu...
NoCode
https://geeknote.net/nocode
https://geeknote.net/nocode/posts/255
2021-08-20T09:11:20Z
2022-10-28T14:39:29Z
Python ThreadPoolExecutor 限制_work_queue 大小
<p>使用python的<code>futures.ThreadPoolExecutor</code>是,如果调用submit提交任务
ThreadPoolExecutor的会向执行</p>
<pre class="highlight"><code>self._work_queue.put(w)
</code></pre>
<p>其中</p>
<pre class="highlight"><code>self._work_queue = queue.SimpleQueue()
</code></pre>
<p>SimpleQueue 是不限制队列大小的,如果提交的任务太多,处理不及时,则导致占用太多内存</p>
<p>可以替换到_work_queue的实现,使用<code>queue.Queue(maxsize=maxsize)</code></p>
<pre class="highlight"><code>class ThreadPoolExecutorWithQueueSizeLimit(futures.ThreadPoolExecutor):
def __init__(self, maxsize=50, *args, **kwargs):
super(ThreadPoolExecutorWithQueueSizeLimit, self).__init__(*args, **kwargs)
self._work_queue = queue.Queue(maxsize=maxsize)
</code></pre>
<p>links:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/48263704/threadpoolexecutor-how-to-limit-the-queue-maxsize">https://stackoverflow.com/questions/48263704/threadpoolexecutor-how-to-limit-the-queue-maxsize</a></li>
</ul>
使用python的futures.ThreadPoolExecutor是,如果调用submit提交任务
ThreadPoolExecutor的会向执行
self._work_queue.put(...
NoCode
https://geeknote.net/nocode