for(i=0;i<vector.size()-1;++i)
size_t
is unsigned integer, so size()-1
is not -1
. We should use i+1<vector.size()
instead.
服务器nginx偶尔会被杀死,可能是系统资源不够了。
sudo systemctl edit nginx
then paste in:
[Service]
Restart=always
check if it works
sudo systemctl status nginx
sudo pkill -f nginx
sudo systemctl status nginx
sudo apt-get install mosquitto mosquitto-clients
sudo mosquitto_passwd -c /etc/mosquitto/passwd <mqtt_user> (set passwd here)
sudo vi /etc/mosquitto/conf.d/default.conf # new file with
allow_anonymous false # disable anonymous user
listener 1883 0.0.0.0 # open to all IP, open to localhost by default.
password_file /etc/mosquitto/passwd # make password work
sudo /etc/init.d/mosquitto restart
mosquitto_sub -h localhost -u netro -P netrohome -t 'test'
mosquitto_pub -h localhost -u netro -P netrohome -t 'test' -m 'hello'
The default max open file is 1024, which may not be enough for large MQTT service, increase it to 65535
# vi /etc/systemd/system.conf
DefaultLimitNOFILE=65536:524288
sudo systemctl daemon-reexec
sudo /etc/init.d/mosquitto restart
# cat /proc/<pid>/limits should be:
Max open files 65535 524288 files
# count the number of file opened by:
sudo ls /proc/<pid>/fd | wc -l
安装ubuntu,默认LVM,全盘安装。但是装完发现只有100G,另外的130多G去哪里了?
参见guide
扩展
lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
undo remote commits:
git reset --hard 'xxxxx'
git clean -f -d
git push -f
text-right
by text-end
<ul class="navbar-nav me-auto">
<li class="nav-item">
<%= link_to t('cloud'), home_index_path, class:"nav-link" %>
</li>
</ul>
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<%= link_to t('cloud'), home_index_path, class:"nav-link" %>
</li>
</ul>
style="overflow-y:auto; max-height:80vh"
.dropdown-menu {
overflow-y:auto;
max-height:80vh
}
$('yourdiv').find('form')[0].reset();
$(".a, .b")
this will match all elements with class a
OR class b
我将engine_py3重新命名为engine后,不工作了!python转为了系统的py2。
必须重新初始化venv环境。例如
python3.8 -m venv env
这是因为,路径被写进去了。
./pyftmerge:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./f2py3:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./activate.fish:set -gx VIRTUAL_ENV "/home/netro/codes/chao_dev/engine_py3/env"
./activate:VIRTUAL_ENV="/home/netro/codes/chao_dev/engine_py3/env"
./hyper:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./ttx:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./pyrsa-verify:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./futurize:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./activate.csh:setenv VIRTUAL_ENV "/home/netro/codes/chao_dev/engine_py3/env"
./pyrsa-sign:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./fixup_translate_v3beta1_keywords.py:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
可见activate中写死了路径。
改变后,找不到了。有些stupid。可以用链接,例如 ln -s engine_py3 engine
。可以work
遇到一个奇怪的问题,在Python3中,调用MongoDB。Sorting会影响结果。
基本代码
order = pymongo.DESCENDING if self.descending else pymongo.ASCENDING
items = self.tb.find(cond).sort(self.uid,order).limit(self.limit)
logging.info(f"There are {items.count()} items")
for idx,t in enumerate(items):
logging.info(f"Working on {idx}-th item: {t[self.uid]}")
问题:
找不到原因,可能是某个地方的bug。采用ascending order绕开了这个问题。
python vs cpython vs cython
install (version 0.29.21)
sudo pip install cython
setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(
name='Hello world app',
ext_modules=cythonize(["hello.py","utils.py","config.py"],annotate=True),
zip_safe=False,
)
python setup.py build_ext --inplace
~/engine
: ERROR: error: [Errno 2] No such file or directory: 'engine/hello.so'~/engine/tmp
: OK__init__.py
solves the problem. When redirecting use
flash[:notice] = "This message value is available in next request-response cycle"
When rendering use
flash.now[:notice] = "Message is available in same request-response cycle"
Info from here
must put an empty option if want to use html options, e.g. class.
# class won't work.
<%= form.select:status, User.statuses.keys.to_a, class:'form-control col'%>
# works
<%= form.select:status, User.statuses.keys.to_a, {},class:'form-control col'%>
Rails7有很大的改动。其中影响最大的一个就是Hotwire
config/application.rb
中改为config.load_defaults 7.0
才会采用Rails7的默认值。
可以把webpacker相关的内容全部删除了,新的东西可以在rails7中生成一个新的project,相应的拷贝过来。
app/javascripts
只需要application.js,删除其他所有。增加controller目录。注意该目录下需要application.js,否则js报错不能正常工作,我卡在这里好久。
Gemfile中
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# remove webpacker
gem 'webpacker', '~> 4.0'
Gemfile中
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# remove
# gem 'turbolinks', '~> 5'
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
需要的改动
+ 查询所有turbolinks地方,替换为turbo。如把所有 HTML 的 data 属性的data-turbolinks更新为data-turbo。turbolinks:load -> turbo:load
+ link_to data: :delete|post
-> data: {turbo_method: :delete|post}
+ confirm message: data: { confirm: 'Are you sure?' }
-> data:{turbo_confirm: 'Are you sure?'}
. Together: data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'},
+ method works for button_to
# link_to works
<%= link_to t('destroy'), offered_path(@offered), data: {turbo_method: :delete, turbo_confirm: 'Are you sure??'} %>
# not working, still post method.
<%= button_to t('destroy'), offered_path(@offered), data: {turbo_method: :delete, turbo_confirm: 'Are you sure??'} %>
# works
<%= button_to t('destroy'), offered_path(@offered), method: :delete, data: {turbo_confirm: 'Are you sure??'} %>
# confirm not work
<%= button_to t('destroy'), offered_path(@offered), method: :delete, confirm: 'Are you sure??' %>
ruby
# not work
<%= link_to t("download"), link, data: { turbo_confirm: 'sure'} %>
# works, 注意,如果设置了data-turbo=false,则不work
<%= link_to t("download"), link, data: { turbo_method: :get, turbo_confirm:'sure?'}%>
# works
<%= button_to t("download"), link, data: { turbo_method: :get, turbo_confirm: 'sure?'} %>
+ controller中render需要增加status,否则有Form responses must redirect to another location
错误 例如
format.html { render :signup } =>
format.html { render :signup, status: :unprocessable_entity}
+ 出错后render自身页面,需要增加redirect_to
format.html { flash[:error] = "ERROR" } =>
format.html { flash[:error] = "ERROR"; redirect_to({action: :login})}
The "default" rails way to do things that I usually see is:
submit a form
redirect on success
render html on error
As such, since Turbo currently requires redirects on all forms, it's a major blocker to prevent folks switching over.
app/assets/config/manifset.js
用于配置需要编译的静态内容。典型配置如
/* Sprockets config, static resources to be compiled */
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
迁移项目到Rails7,遇到一下问题:
undefined method `javascript_pack_tag'
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
根源: javascript_pack_tag is a Webpacker-specific method, so unless you're using Webpacker, you can't use it.
Rails中webpacker被 importmapped Hotwire取代
正确的做法是
<%= javascript_importmap_tags %>
Rails开发正常,部署之后403错误,Nginx配置之前用过,没有问题啊。
折腾半天,原来是语法错误,丢失了分号。
server_name server1.com www.server1.com # 没分号不行
server_name server1.com www.server1.com; # 有分号就好了