0%

版本1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var isLeapYear = function(year) {
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}

var addMonths = (function(){
var DAYS_IN_MONTH = [
[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
];
return function(date, m){
var n = date.getFullYear() * 12 + date.getMonth();
n += m;
var ryear = parseInt(n / 12);
var rmonth = n % 12 + 1;
var rday = date.getDate();

var days_in_month = DAYS_IN_MONTH[isLeapYear(date.getFullYear()) ? 1 : 0];
if (rday > days_in_month[rmonth]){
rday = days_in_month[rmonth];
}

var d = new Date(ryear, rmonth - 1, rday);
d.setHours(date.getHours());
d.setMinutes(date.getMinutes());
d.setSeconds(date.getSeconds());
d.setMilliseconds(date.getMilliseconds());
return d;
}
}());
Read more »

按业务场景划分队列

一定一定要更具业务特点来划分出不同的队列,不能将所有的任务都让同一队列消费。 在实际使用异步任务的场景中,一定会有优先级不高的任务(如统计类)和优先级特别高的任务(如微信红包)。 若不划分队列,那么当统计类的异步任务很多很多的时候, 发送红包给微信用户的异步任务就要等待一段时间才能被执行到(任务多可能会等待十几分钟甚至一两个小时),这对用户体验来说是非常不友好的。 此时就可以将队列划分为lowmiddlehigh这几种队列,对于统计类的这些任务可以扔去low的队列中, 而对于发送红包这种重要的任务就扔去high队列中,确保尽可能快的被执行到。具体划分,需要更加业务场景来划分。

通过-Q参数来指定队列名:

1
celery -A proj worker -l info -Q low,middle,high
1
2
3
4
5
6
7
# 方式1
@app.task(bind=True, queue='middle', name='send_wx_text')
def send_wx_text(self, target_origin_id, to_user, txt):
pass

# 方式2
send_wx_text.apply_async((target_origin_id, to_user, txt), queue='middle')
Read more »

这里介绍使用pageres来进行网站截图。

pageres

安装

1
npm install pageres

使用

1
2
3
4
5
6
7
8
9
const Pageres = require('pageres');

const pageres = new Pageres({delay: 2})
.src('yeoman.io', ['480x320', '1024x768', 'iphone 5s'], {crop: true})
.src('todomvc.com', ['1280x1024', '1920x1080'])
.src('data:text/html;base64,PGgxPkZPTzwvaDE+', ['1024x768'])
.dest(__dirname)
.run()
.then(() => console.log('done'));

具体使用参考:pageres

pageres-cli

安装

1
npm install --global pageres-cli

使用

1
2
3
4
5
6
7
8
# pageres <url> <resolution>
pageres todomvc.com 1024x768 1366x768 # 2 screenshots
pageres todomvc.com yeoman.io 1024x768 # 2 screenshots
pageres todomvc.com yeoman.io 1024x768 1366x768 # 4 screenshots

# pageres [ <url> <resolution> ] [ <url> <resolution> ]
pageres [ yeoman.io 1024x768 1600x900 ] todomvc.com 1366x768
pageres [ yeoman.io 1024x768 --no-crop ] todomvc.com 1366x768 --crop

具体使用参考:pageres-cli

最近发现在使用node:8.9-alpine全局安装`node-sass@4.8`时,安装总是失败:

1
npm install -g node-sass@4.8 --sass-binary-site=https://npm.taobao.org/mirrors/node-sass/

执行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/usr/local/bin/node-sass -> /usr/local/lib/node_modules/node-sass/bin/node-sass

> node-sass@4.8.3 install /usr/local/lib/node_modules/node-sass
> node scripts/install.js

Unable to save binary /usr/local/lib/node_modules/node-sass/vendor/linux_musl-x64-57 : { Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/node-sass/vendor'
at Object.fs.mkdirSync (fs.js:885:18)
at sync (/usr/local/lib/node_modules/node-sass/node_modules/mkdirp/index.js:71:13)
at Function.sync (/usr/local/lib/node_modules/node-sass/node_modules/mkdirp/index.js:77:24)
at checkAndDownloadBinary (/usr/local/lib/node_modules/node-sass/scripts/install.js:114:11)
at Object.<anonymous> (/usr/local/lib/node_modules/node-sass/scripts/install.js:157:1)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
errno: -13,
code: 'EACCES',
syscall: 'mkdir',
path: '/usr/local/lib/node_modules/node-sass/vendor' }

> node-sass@4.8.3 postinstall /usr/local/lib/node_modules/node-sass
> node scripts/build.js
......
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.8.3 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.8.3 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-04-02T05_17_18_651Z-debug.log
Read more »

配置Nginx+Gunicorn+Django时,发现所有请求都是返回Bad Request (400)

  1. django的settings.py配置ALLOWED_HOSTS = [*]

  2. 配置nginx反向代理proxy_set_header Host $host;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    client_max_body_size 20m;
    client_body_buffer_size 256k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 128k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;

当访问http://example.com/a/abc.html, 实际上方向代理到后端服务器http://10.0.x.x:8080/b/abc.html, 那么可以按照以下方式进行配置:

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name example.com;

rewrite /a$ /a/ permanent; # 防止访问`/a`时返回404
location ~ /a/(?<section>.*) {
proxy_pass http://10.0.x.x:8080/b/$section;
}
}

1
2
3
4
5
6
7
8
9
# 缓存15分钟,也就是15分钟内不用重复输入帐号密码
git config credential.helper cache
git config --global credential.helper cache
# 可以自定义缓存时间,以下是缓存1小时
git config credential.helper 'cache --timeout=3600'
git config --global credential.helper 'cache --timeout=3600'
# 也可以永久缓存
git config credential.helper store
git config --global credential.helper store

1
2
3
4
# 按时间正序排列
git tag --sort=v:refname
# 按时间倒序排列
git tag --sort=-v:refname

为修改已经存在的commit中的用户名和邮箱,必须重写整个git repo的提交历史。

警告:这种行为对提交历史具有破坏性,在无必要的情况下,不建议对其修改。

注意:完成重写后,任何fork或clone的人必须重新获取重写后的历史并把所有本地修改rebase入重写后的历史中。

  1. 复制以下脚本,并根据自己的需要修改以下变量:

    • OLD_EMAIL: 要修改的邮箱
    • CORRECT_NAME: 修改之后的用户名
    • CORRECT_EMAIL: 修改之后的邮箱

      脚本:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      #!/bin/sh

      OLD_EMAIL="your-old-email@example.com" CORRECT_NAME="Your Correct Name" CORRECT_EMAIL="your-correct-email@example.com" git filter-branch --env-filter '
      if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then
      export GIT_COMMITTER_NAME="$CORRECT_NAME"
      export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
      fi
      if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then
      export GIT_AUTHOR_NAME="$CORRECT_NAME"
      export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
      fi
      ' --tag-name-filter cat -- --branches --tags
  2. 执行脚本

  3. 常看历史有没有错误

  4. 将重写后的历史提交到Git远程仓库中:git push --force --tags origin 'refs/heads/*'

建议以上操作在一个新clone的仓库中进行,这样的话可以减少一些误操作。

Memoization是一种将函数返回值缓存起来的方法。

Memoization原理非常简单,就是把函数的每次执行结果都放入一个散列表(或数组)中,在接下来的执行中, 在散列表中查找是否已经有相应执行过的值,如果有,直接返回该值,没有才真正执行函数体的求值部分。 很明显,找值,尤其是在散列中找值,比执行函数快多了。现代JavaScript的开发也已经大量使用这种技术。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var fib = (function() {
var cache = [
1, 1
];
var f = function(num) {
if (num <= 0)
throw new Error("Num not less than 1.");
if (num >= cache.length) {
for (var i = cache.length; i <= num; i++) {
cache[i] = cache[i - 2] + cache[i - 1];
}
}
return cache[num - 1];
};
return f;
})();