add git task

This commit is contained in:
knst-kotov 2021-08-08 09:49:15 +00:00
parent 46941749b6
commit 848908aca0
2 changed files with 62 additions and 20 deletions

14
main.py
View File

@ -12,6 +12,9 @@ from auth.authenticate import JWTAuthenticate
from auth.oauth import oauth_login, oauth_authorize from auth.oauth import oauth_login, oauth_authorize
from redis import redis from redis import redis
from resolvers.base import resolvers from resolvers.base import resolvers
from resolvers.zine import GitTask
import asyncio
import_module('resolvers') import_module('resolvers')
schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers) schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers)
@ -22,15 +25,16 @@ middleware = [
] ]
async def start_up(): async def start_up():
await redis.connect() await redis.connect()
git_task = asyncio.create_task(GitTask.git_task_worker())
async def shutdown(): async def shutdown():
await redis.disconnect() await redis.disconnect()
routes = [ routes = [
Route("/oauth/{provider}", endpoint=oauth_login), Route("/oauth/{provider}", endpoint=oauth_login),
Route("/authorize", endpoint=oauth_authorize) Route("/authorize", endpoint=oauth_authorize)
] ]
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware, routes=routes) app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware, routes=routes)

View File

@ -7,6 +7,47 @@ from auth.authenticate import login_required
from settings import SHOUTS_REPO from settings import SHOUTS_REPO
import subprocess import subprocess
import asyncio
class GitTask:
queue = asyncio.Queue()
def __init__(self, shout_id, shout_body, username, user_email, comment):
self.shout_id = shout_id;
self.shout_body = shout_body;
self.username = username;
self.user_email = user_email;
self.comment = comment;
GitTask.queue.put_nowait(self)
def execute(self):
cmd = "cd %s; git checkout master" % (SHOUTS_REPO)
output = subprocess.check_output(cmd, shell=True)
print(output)
shout_filename = "shout%s.md" % (self.shout_id)
shout_full_filename = "%s/%s" % (SHOUTS_REPO, shout_filename)
with open(shout_full_filename, mode='w', encoding='utf-8') as shout_file:
shout_file.write(self.shout_body)
author = "%s <%s>" % (self.username, self.user_email)
cmd = "cd %s; git add %s; git commit -m '%s' --author='%s'" % \
(SHOUTS_REPO, shout_filename, self.comment, author)
output = subprocess.check_output(cmd, shell=True)
print(output)
@staticmethod
async def git_task_worker():
print("git task worker start")
while True:
task = await GitTask.queue.get()
try:
task.execute()
except Exception as err:
print("git task worker error = %s" % (err))
@query.field("topShouts") @query.field("topShouts")
async def top_shouts(_, info): async def top_shouts(_, info):
@ -26,25 +67,22 @@ async def create_shout(_, info, body):
auth = info.context["request"].auth auth = info.context["request"].auth
user_id = auth.user_id user_id = auth.user_id
with local_session() as session:
user = session.query(User).filter(User.id == user_id).first()
new_shout = Shout.create( new_shout = Shout.create(
author_id = user_id, author_id = user_id,
body = body body = body
) )
branch_name = "shout%s" % (new_shout.id) task = GitTask(
new_shout.id,
cmd = "cd %s; git checkout master && git checkout -b %s && git branch %s-dev" % (SHOUTS_REPO, branch_name, branch_name) body,
output = subprocess.check_output(cmd, shell=True) user.username,
print(output) user.email,
"new shout %s" % (new_shout.id)
shout_filename = "%s/body" % (SHOUTS_REPO) )
with open(shout_filename, mode='w', encoding='utf-8') as shout_file:
shout_file.write(body)
cmd = "cd %s; git commit -a -m 'initial version'" % (SHOUTS_REPO)
output = subprocess.check_output(cmd, shell=True)
print(output)
return { return {
"shout" : new_shout "shout" : new_shout
} }