Skip to content

Instantly share code, notes, and snippets.

View tonywangcn's full-sized avatar
🎯
Focusing

Tony Wang tonywangcn

🎯
Focusing
View GitHub Profile
@tonywangcn
tonywangcn / flask.py
Created April 24, 2017 13:25
flask app for log server
from flask import Flask, request, Response
import json
from pymongo import MongoClient
from datetime import datetime
client = MongoClient("mongodb://logging:password@lcoal:port/logging")
db = client['logging']
post = db.log
# set expire time 7 days, once setuped, couldn't change it again.
post.ensure_index("CreateDate", expireAfterSeconds= 7*24*60*60 )
@tonywangcn
tonywangcn / logger.py
Last active April 24, 2017 13:49
rewrite logger handler to add some features
import logging
import threading
import time
import logging.handlers
import os
class OneLineExceptionFormatter(logging.Formatter):
def formatException(self, exc_info):
@parallel
def install():
cmd = "celery -A test_celery worker --app=test_celery.celeryapp:app --concurrency=10 --loglevel=debug"
run("apt-get install dtach && apt-get install -y python-pip && pip install celery && pip install requests && pip install pymongo")
with settings(warn_only=True):
runResult = run("mkdir -p /root/code/test_celery")
if runResult.return_code == 1:
print 'file exists'
put("./test_celery/celeryapp.py", "/root/code/test_celery")
put("./test_celery/run_tasks.py", "/root/code/test_celery")
@tonywangcn
tonywangcn / fabric.py
Created March 19, 2017 09:04
different users and password
from fabric.api import *
env.hosts = [
'username1@ip:port',
'username2@ip:port',
'username3@ip:port',
]
env.passwords = {
'username1@ip:port': "password1",
@tonywangcn
tonywangcn / faric_server.py
Last active March 19, 2017 09:05
same user and password
from fabric.api import *
env.hosts = [
'ip',
'ip',
'ip',
]
# Set the username
env.user = "username"
# Set the password [NOT RECOMMENDED]
from fabric.api import *
env.hosts = [
'10.211.55.12:32772',
'10.211.55.12:32773',
'10.211.55.12:32774',
]
# Set the username
env.user = "root"
@tonywangcn
tonywangcn / fabric_run_parallel
Last active March 19, 2017 09:59
fabric_run_parallel
tony@tony-MBP:~/Desktop/Test$ fab install
[root@10.211.55.12:32772] Executing task 'install'
[root@10.211.55.12:32773] Executing task 'install'
[root@10.211.55.12:32774] Executing task 'install'
[root@10.211.55.12:32774] run: apt-get install dtach && apt-get install -y python-pip && pip install celery && pip install requests && pip install pymongo
[root@10.211.55.12:32773] run: apt-get install dtach && apt-get install -y python-pip && pip install celery && pip install requests && pip install pymongo
[root@10.211.55.12:32772] run: apt-get install dtach && apt-get install -y python-pip && pip install celery && pip install requests && pip install pymongo
@tonywangcn
tonywangcn / structures-of-file
Created March 19, 2017 06:47
structures-of-file
.
├── _test_celery
| ├── __init__.py
| ├── celeryapp.py
| ├── run_tasks.py
| └── tasks.py
└── fabfile.py
@tonywangcn
tonywangcn / run_tasks.py
Last active March 19, 2017 06:42
run_tasks.py
from .tasks import longtime_add
import time
if __name__ == '__main__':
url = ['http://example1.com', 'http://example2.com', 'http://example3.com', 'http://example4.com'] # change them to your ur list.
for i in url:
result = longtime_add.delay(i)
print 'Task result:', result.result
@tonywangcn
tonywangcn / tasks.py
Created March 19, 2017 06:40
tasks.py
from __future__ import absolute_import
from test_celery.celeryapp import app
import time
import requests
from pymongo import MongoClient
client = MongoClient('10.211.55.12', 27018) # change the ip and port to your mongo database's
db = client.mongodb_test
collection = db.celery_test