# Path: settings.py
# ZONESTARTPORT = 1300
# 
# ZONEENDPORT = 1400
# 
# Path: elixir_models.py
# class RetryDB(RetryOperationalError, SqliteExtDatabase):
# class ComplexEncoder(json.JSONEncoder):
# class BaseModel(Model):
#     class Meta:
# class User(BaseModel):
# class Character(BaseModel):
# class Zone(BaseModel):
# class Message(BaseModel):
# class Object(BaseModel):
# class Message(BaseModel):
# class ScriptedObject(Object):
#     def default(self, obj):
#     def exists(cls, *args, **kwargs):
#     def json_dumps(self):
#     def __repr__(self):
#     def __repr__(self):
#     def loc(self):
#     def loc(self, val):
#     def set_modified(self, date_time=None):
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
# def setup(db_uri='simplemmo.sqlite', echo=False):


import xmlrpclib
import supervisor
    import socket
from supervisor.xmlrpc import SupervisorTransport
from settings import ZONESTARTPORT, ZONEENDPORT
from elixir_models import Zone, session
            from settings import PROTOCOL, HOSTNAME
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ##### END AGPL LICENSE BLOCK #####




def _add_process(twiddlerproxy, processgroup, zoneid, settings, port):
    '''Adds a zone process to supervisor and does some checks to only start it
    if it isn't already running, and restart it if it's not.'''

    s = twiddlerproxy
    try:
        retval = s.twiddler.addProgramToGroup(processgroup, zoneid, settings)

        print "Added successfully."

        zone = Zone.query.filter_by(zoneid=zoneid).first()
        if zone:
            # This zone already exists, so update the port number.
            zone.port = port
        else:
            # This zone doesn't exist. Create a new one.
            Zone(zoneid=zoneid, port=port)

# Path: settings.py
# ZONESTARTPORT = 1300
# 
# ZONEENDPORT = 1400
# 
# Path: elixir_models.py
# class RetryDB(RetryOperationalError, SqliteExtDatabase):
# class ComplexEncoder(json.JSONEncoder):
# class BaseModel(Model):
#     class Meta:
# class User(BaseModel):
# class Character(BaseModel):
# class Zone(BaseModel):
# class Message(BaseModel):
# class Object(BaseModel):
# class Message(BaseModel):
# class ScriptedObject(Object):
#     def default(self, obj):
#     def exists(cls, *args, **kwargs):
#     def json_dumps(self):
#     def __repr__(self):
#     def __repr__(self):
#     def loc(self):
#     def loc(self, val):
#     def set_modified(self, date_time=None):
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
# def setup(db_uri='simplemmo.sqlite', echo=False):


# Path: start_supervisord_process.py
import xmlrpclib
import supervisor
    import socket
from supervisor.xmlrpc import SupervisorTransport
from settings import ZONESTARTPORT, ZONEENDPORT
from elixir_models import Zone, session
            from settings import PROTOCOL, HOSTNAME

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ##### END AGPL LICENSE BLOCK #####




def _add_process(twiddlerproxy, processgroup, zoneid, settings, port):
    '''Adds a zone process to supervisor and does some checks to only start it
    if it isn't already running, and restart it if it's not.'''

    s = twiddlerproxy
    try:
        retval = s.twiddler.addProgramToGroup(processgroup, zoneid, settings)

        print "Added successfully."

        zone = Zone.query.filter_by(zoneid=zoneid).first()
        if zone:
            # This zone already exists, so update the port number.
            zone.port = port
        else:
            # This zone doesn't exist. Create a new one.
            Zone(zoneid=zoneid, port=port)

        session.commit()