Online Judge from Scratch(2) – Dispatcher

The dispatcher, as the name implies, fetches judge tasks from RabbitMQ, dispatches them to the sandbox workers and gets the results back synchronously. In Justice, the sandboxes are language-specific: If the submission is written in Java, we can sandbox it with Java Security Manager. If the submission is written in C/CPP, we need another sandbox … Continue reading "Online Judge from Scratch(2) – Dispatcher"

Read More

Online Judge from Scratch(1) – Frontend

The frontend of Justice contains two sites: the web UI for users and the admin panel for administrators, the main reason to choose Yii2 is the Advanced Application Template provides both succinct project structure and great convenience to share the same logic between the two sites: Besides, we improved Yii2’s MVC pattern by adding an … Continue reading "Online Judge from Scratch(1) – Frontend"

Read More

Online Judge from Scratch(0) – Architecture

An online judge system(like codeforces, leetcode, etc) contains a problem set of algorithms to solve, while users can compile a piece of code and execute the generated binary with pre-constructed data to test if the code is correct. However details of algorithms won’t be discussed here, we mainly focus on how to build an online judge … Continue reading "Online Judge from Scratch(0) – Architecture"

Read More

Higher-order functions in Javascript

In Javascript, functions are just like the other variables(AKA first-class citizen), and here are several interesting tricks implemented by treating functions as first-class citizen. currying Currying is for partial evaluation, a practical example is Function.prototype.bind() in Javascript: And we can implement bind()(under this circumstance only of course) by ourselves: trampolining Javascript(ES5) does not implement tail call optimization, … Continue reading "Higher-order functions in Javascript"

Read More

RecordRTC 压缩视频和音频

RecordRTC 是一个基于WebRCT实现的一个录制视频/音频的js库。项目主页是:http://RecordRTC.org/ 实际项目中发现,录制的视频和音频文件过大,可能会导致用户上传超时。本文主要记录了减小RecordRTC录制的视频和音频文件大小的几个方法。 压缩视频 修改图片大小,通过修改 config.width 和 config.height 实现: https://github.com/muaz-khan/RecordRTC/blob/master/RecordRTC.js#L2447-L2471 初始化 RecordRTC 时指定 frameInterval: 压缩音频 保留一个声道,设置 numberOfAudioChannels: 1,音频文件提及可以减少为原来的1/2; 降低采样率,设置 sampleRate: 7350,即默认采样率44100的1/6; 减小采样位数,从默认的16减小到8; 压缩音频的详细方法可以参考:http://www.cnblogs.com/blqw/p/3782420.html

Read More

使用 Sentry 监控业务日志

Sentry 是一个错误日志收集和聚合的平台,我们可以在程序中捕获异常,并发送到 Sentry 服务端进行聚合统计、展示和报警。 相关文档参考:http://sentry.readthedocs.org/en/latest/ 准备工作 安装 Docker: 启动 Docker: 安装 redis Docker 镜像: 安装 PostgreSQL Docker 镜像: 生成 secret token(sentry会返回类似于 “9qa)zi)wrz5e9vrln=%fv3wlkyen5#nf-f10q#4h%1ygn!q8ha”的字符串,下文用 <token>代替): 初始化 PostgreSQL,过程中会提示创建用户: 启动 Sentry: 现在从浏览器访问 http://127.0.0.1:12000/,就可以看到 Sentry 的主界面了: 当然,只有界面是不够的,下面会说明如何完成一个上报异常的 demo 选择 “管理” => “项目” 左侧的某一个项目,点击进入设置选项卡,选择 “Client Keys”,右侧会展示 SDK 需要的 API Key: 记下 Sentry 提供的 Key,我们的 demo 中会用到: 运行1次,dashboard 中就会出现对应的记录了:  

Read More

javascript 解析 lisp 表达式

问题 项目中需要前端解析出 lisp 表达式中的内容后,渲染到页面上: 解决 同事离职前实现了一版,思路是使用正则表达式捕获住对应的内容,缺点也是显而易见的:不够灵活。我参考了 (How to Write a (Lisp) Interpreter (in Python)) 中的实现,完成了一版 javascript 的实现。 函数 tokenize 用于将字符串预处理成统一的形式: 函数 read_from_tokens 负责将字符串解析成对应的数据: 工具函数 atom 负责处理变量类型相关的工作: 看下效果,还不错:

Read More