Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rebuild-daily-py
import MySQLdb
import os
import sys
from sqlalchemy import create_engine, MetaData
from sqlalchemy.sql import text
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import Column, String, Integer, Date, Table, ForeignKey, Float
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from urllib import quote_plus as urlquote
from multiprocessing.dummy import Pool as ThreadPool
import json
import urllib
import smtplib
from email.mime.text import MIMEText
import requests
import paramiko
import time
from config import *
import boto3
from models import Subscription, Server
# Set PID file, this prevents the script from running if already running
pid = str(os.getpid())
pidfile = "/tmp/rebuild-daily-py.pid"
if os.path.isfile(pidfile):
print "%s already exists, exiting" % pidfile
sys.exit()
file(pidfile, 'w').write(pid)
# Setup SQLAlchemy
engine = create_engine('mysql://ghostifidbuser:%s@localhost:3306/ghostifi' % urlquote(DB_PASSWORD), echo=False)
metadata = MetaData(bind=engine)
Session = scoped_session(sessionmaker(engine, autoflush=True))
session = Session()
Base = declarative_base()
Base.metadata.create_all(engine)
try:
servers_to_rebuild_daily = session.query(Server).filter(Server.rebuild_schedule=="Daily").all()
# Make the Pool of workers
pool = ThreadPool(4)
def thread_servers_to_rebuild_daily(self):
# Change status to Rebuilding to display this in the user dashboard
self.status = 'Rebuilding'
# Update database set status from Running to Rebuilding
session.commit()
# Run rebuild function
self.rebuild('rebuild_schedule')
# Commit changes - update status, rebuild_location, bandwidth_this_month etc
session.commit()
# Start a thread for each servers_to_rebuild_now
results = pool.map(thread_servers_to_rebuild_daily, servers_to_rebuild_daily)
# close the pool and wait for the work to finish
pool.close()
pool.join()
session.close()
finally:
os.unlink(pidfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment