You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-core/core/server/clean.py

252 lines
7.8 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, glob
import signal
import time, datetime
import pickle
import soaplib
# clear server files after restart
def clean (SID_FILE, PID_FILE, SID_PID, SIDS_DIR, PIDS_DIR):
if os.path.exists(SID_FILE):
os.unlink(SID_FILE)
if os.path.exists(PID_FILE):
os.unlink(PID_FILE)
if os.path.exists(SID_PID):
os.unlink(SID_PID)
if os.path.isdir(SIDS_DIR):
for filename in glob.glob(SIDS_DIR+"/*.sid"):
os.unlink (filename)
if os.path.isdir(PIDS_DIR):
for filename in glob.glob(PIDS_DIR+"/*.pid"):
os.unlink (filename)
class CoreWsdl:
# watch for process
def watcher_pid_proc(self, pid):
period = 2
time.sleep(period)
try:
# while process status "Active"
while self.glob_process_dict[pid]['status'] == 1:
# frequency check
time.sleep(period)
self._delete_pid(pid)
except IOError:
print 'Except IOError'
except:
print _("watcher process pid %d error") %pid
try:
self._delete_pid(pid)
except:
pass
time.sleep(0.1)
def _delete_pid(self, pid):
while len(self.glob_frame_list[pid]) > \
self.glob_process_dict[pid]['counter']:
time.sleep(1)
self.del_pid(pid)
self.del_pid_from_sid_pid(pid)
def monitor(certbase, SID_FILE):
""" function to delete old session """
# set default
period = 180
sid_live = 240
cert_live = 10080
# Get value of period and lifetime session from DataVars
try:
from calculate.core.datavars import DataVarsCore
ob = DataVarsCore()
ob.importCore()
ob.flIniFile()
period = float(ob.Get('cl_core_monitor_period'))
sid_live = float(ob.Get('cl_core_sid_live'))
except:
print _("Variables cl_core_monitor_period "
"or cl_core_sid_live not found")
raise
return 1
# Check lifetime. if necessary, remove
while True:
# check session
try:
rst = []
SID_FILE_T = SID_FILE + '_temp'
fd = open(SID_FILE, 'r')
ft = open(SID_FILE_T, 'w')
while 1:
try:
# read all on one record
list_sid = pickle.load(fd)
except:
break
# how time exists session
delta = datetime.datetime.now() - list_sid[2]
# if not outdated, then leave
if (delta.seconds < sid_live * 60):
pickle.dump(list_sid, ft)
fd.close()
ft.close()
# copy all from temp file
ft = open(SID_FILE_T, 'rb')
fd = open(SID_FILE, 'wb')
ft.seek(0)
fd.write(ft.read())
ft.close()
fd.close()
# Delete temp file
os.unlink(SID_FILE_T)
except:
return 1
## check certificate
##try:
#if not os.path.exists(CERT_FILE):
#temp = open(CERT_FILE, 'w')
#temp.close()
#rst = []
#CERT_FILE_T = certbase + '_temp'
#fc = open(CERT_FILE, 'r')
#fct = open(CERT_FILE_T, 'w')
#while 1:
#try:
## Считать по одной строке
#list_cert = pickle.load(fc)
#except:
#break
## Сколько существует certificate
#delta = datetime.datetime.now() - list_cert["date"]
## Если не устарел, то оставить
#if (delta.seconds < cert_live * 60):
#pickle.dump(list_cert, fct)
#else:
#cert_met = serv_get_methods(list_cert["cert"])
#if cert_met[0] != "No Method":
#pickle.dump(list_cert, fct)
#fc.close()
#fct.close()
## Скопировать всё из временного файла
#fct = open(CERT_FILE_T, 'rb')
#fc = open(CERT_FILE, 'wb')
#fct.seek(0)
#fc.write(fct.read())
#fct.close()
#fc.close()
## Удалить временный файл
#os.unlink(CERT_FILE_T)
##except:
##print _("file %s not exists") % CERT_FILE
# Частота проверки
time.sleep(60 * period)
# check client's presence
def sid_monitor(SID_FILE, SIDS_DIR):
# check interval
period = 21
while True:
try:
sids = []
# create, if file not exists
if not os.path.exists(SID_FILE):
temp = open(SID_FILE, 'w')
temp.close()
fd = open(SID_FILE, 'r')
while 1:
try:
# read all on one record
list_sid = pickle.load(fd)
except:
break
# add session id in sesession list
sids.append(list_sid[0])
fd.close()
except:
print _("Error read sid files")
return
try:
# for each session
for filename in sids:
# find file this session
sid_path = SIDS_DIR + "/%d.sid"%filename
with open(sid_path) as fd:
# read information about session
sid_inf = pickle.load(fd)
# if number of missed inspections more 3
if sid_inf[1] > 3:
# flag client absence
sid_inf[2] = 1
fd.close()
ft = open(sid_path, 'w')
# add to digit missed inspections
# client constantly nulls this value!
if sid_inf[1] < 4:
sid_inf[1] += 1
pickle.dump(sid_inf, ft)
ft.close()
except:
pass
# check period
time.sleep(period)
# get available methods for performance
# for delete old certificates with zero's rights
#def serv_get_methods():
#curThread = threading.currentThread()
#certificate = curThread.client_cert
#if certificate == None:
#return -3
#from cert_cmd import find_cert_id
#cert_id = find_cert_id(cert, self.data_path, self.certbase)
#results = []
#cert_id = int(cert_id)
#if cert_id > 0:
#cert_id = str(cert_id)
## open rights file
#with open(RIGHT_FILE) as fd:
#t = fd.read()
## See each line
#for line in t.splitlines():
## and each word in line
#words = line.split()
#for word in words:
## if in line present certificate id
#if cert_id == word:
## add name available method
#results.append(words[0])
#fd.close()
#if results == []:
#results.append("No Methods")
#return results