forked from indymedia/epicyon
flake8 format
parent
d55fe8bb59
commit
bfe37d5ae7
142
schedule.py
142
schedule.py
|
@ -1,10 +1,10 @@
|
||||||
__filename__="schedule.py"
|
__filename__ = "schedule.py"
|
||||||
__author__="Bob Mottram"
|
__author__ = "Bob Mottram"
|
||||||
__license__="AGPL3+"
|
__license__ = "AGPL3+"
|
||||||
__version__="1.1.0"
|
__version__ = "1.1.0"
|
||||||
__maintainer__="Bob Mottram"
|
__maintainer__ = "Bob Mottram"
|
||||||
__email__="bob@freedombone.net"
|
__email__ = "bob@freedombone.net"
|
||||||
__status__="Production"
|
__status__ = "Production"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
@ -13,30 +13,32 @@ from utils import getStatusNumber
|
||||||
from utils import loadJson
|
from utils import loadJson
|
||||||
from outbox import postMessageToOutbox
|
from outbox import postMessageToOutbox
|
||||||
|
|
||||||
def updatePostSchedule(baseDir: str,handle: str,httpd,maxScheduledPosts: int) -> None:
|
|
||||||
|
def updatePostSchedule(baseDir: str, handle: str, httpd,
|
||||||
|
maxScheduledPosts: int) -> None:
|
||||||
"""Checks if posts are due to be delivered and if so moves them to the outbox
|
"""Checks if posts are due to be delivered and if so moves them to the outbox
|
||||||
"""
|
"""
|
||||||
scheduleIndexFilename=baseDir+'/accounts/'+handle+'/schedule.index'
|
scheduleIndexFilename = baseDir + '/accounts/' + handle + '/schedule.index'
|
||||||
if not os.path.isfile(scheduleIndexFilename):
|
if not os.path.isfile(scheduleIndexFilename):
|
||||||
return
|
return
|
||||||
|
|
||||||
# get the current time as an int
|
# get the current time as an int
|
||||||
currTime=datetime.datetime.utcnow()
|
currTime = datetime.datetime.utcnow()
|
||||||
daysSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days
|
daysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days
|
||||||
|
|
||||||
scheduleDir=baseDir+'/accounts/'+handle+'/scheduled/'
|
scheduleDir = baseDir + '/accounts/' + handle + '/scheduled/'
|
||||||
indexLines=[]
|
indexLines = []
|
||||||
deleteSchedulePost=False
|
deleteSchedulePost = False
|
||||||
nickname=handle.split('@')[0]
|
nickname = handle.split('@')[0]
|
||||||
with open(scheduleIndexFilename, 'r') as fp:
|
with open(scheduleIndexFilename, 'r') as fp:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
if ' ' not in line:
|
if ' ' not in line:
|
||||||
continue
|
continue
|
||||||
dateStr=line.split(' ')[0]
|
dateStr = line.split(' ')[0]
|
||||||
if 'T' not in dateStr:
|
if 'T' not in dateStr:
|
||||||
continue
|
continue
|
||||||
postId=line.split(' ',1)[1].replace('\n','')
|
postId = line.split(' ', 1)[1].replace('\n', '')
|
||||||
postFilename=scheduleDir+postId+'.json'
|
postFilename = scheduleDir + postId + '.json'
|
||||||
if deleteSchedulePost:
|
if deleteSchedulePost:
|
||||||
# delete extraneous scheduled posts
|
# delete extraneous scheduled posts
|
||||||
if os.path.isfile(postFilename):
|
if os.path.isfile(postFilename):
|
||||||
|
@ -45,10 +47,11 @@ def updatePostSchedule(baseDir: str,handle: str,httpd,maxScheduledPosts: int) ->
|
||||||
# create the new index file
|
# create the new index file
|
||||||
indexLines.append(line)
|
indexLines.append(line)
|
||||||
# convert string date to int
|
# convert string date to int
|
||||||
postTime= \
|
postTime = \
|
||||||
datetime.datetime.strptime(dateStr,"%Y-%m-%dT%H:%M:%S%z").replace(tzinfo=None)
|
datetime.datetime.strptime(dateStr, "%Y-%m-%dT%H:%M:%S%z")
|
||||||
postDaysSinceEpoch= \
|
postTime = postTime.replace(tzinfo=None)
|
||||||
(postTime - datetime.datetime(1970,1,1)).days
|
postDaysSinceEpoch = \
|
||||||
|
(postTime - datetime.datetime(1970, 1, 1)).days
|
||||||
if daysSinceEpoch < postDaysSinceEpoch:
|
if daysSinceEpoch < postDaysSinceEpoch:
|
||||||
continue
|
continue
|
||||||
if daysSinceEpoch == postDaysSinceEpoch:
|
if daysSinceEpoch == postDaysSinceEpoch:
|
||||||
|
@ -57,11 +60,11 @@ def updatePostSchedule(baseDir: str,handle: str,httpd,maxScheduledPosts: int) ->
|
||||||
if currTime.time().minute < postTime.time().minute:
|
if currTime.time().minute < postTime.time().minute:
|
||||||
continue
|
continue
|
||||||
if not os.path.isfile(postFilename):
|
if not os.path.isfile(postFilename):
|
||||||
print('WARN: schedule missing postFilename='+postFilename)
|
print('WARN: schedule missing postFilename=' + postFilename)
|
||||||
indexLines.remove(line)
|
indexLines.remove(line)
|
||||||
continue
|
continue
|
||||||
# load post
|
# load post
|
||||||
postJsonObject=loadJson(postFilename)
|
postJsonObject = loadJson(postFilename)
|
||||||
if not postJsonObject:
|
if not postJsonObject:
|
||||||
print('WARN: schedule json not loaded')
|
print('WARN: schedule json not loaded')
|
||||||
indexLines.remove(line)
|
indexLines.remove(line)
|
||||||
|
@ -70,106 +73,113 @@ def updatePostSchedule(baseDir: str,handle: str,httpd,maxScheduledPosts: int) ->
|
||||||
# set the published time
|
# set the published time
|
||||||
# If this is not recent then http checks on the receiving side
|
# If this is not recent then http checks on the receiving side
|
||||||
# will reject it
|
# will reject it
|
||||||
statusNumber,published=getStatusNumber()
|
statusNumber, published = getStatusNumber()
|
||||||
if postJsonObject.get('published'):
|
if postJsonObject.get('published'):
|
||||||
postJsonObject['published']=published
|
postJsonObject['published'] = published
|
||||||
if postJsonObject.get('object'):
|
if postJsonObject.get('object'):
|
||||||
if isinstance(postJsonObject['object'], dict):
|
if isinstance(postJsonObject['object'], dict):
|
||||||
if postJsonObject['object'].get('published'):
|
if postJsonObject['object'].get('published'):
|
||||||
postJsonObject['published']=published
|
postJsonObject['published'] = published
|
||||||
|
|
||||||
print('Sending scheduled post '+postId)
|
print('Sending scheduled post ' + postId)
|
||||||
|
|
||||||
if nickname:
|
if nickname:
|
||||||
httpd.postToNickname=nickname
|
httpd.postToNickname = nickname
|
||||||
if not postMessageToOutbox(postJsonObject,nickname, \
|
if not postMessageToOutbox(postJsonObject, nickname,
|
||||||
httpd,baseDir, \
|
httpd, baseDir,
|
||||||
httpd.httpPrefix, \
|
httpd.httpPrefix,
|
||||||
httpd.domain, \
|
httpd.domain,
|
||||||
httpd.domainFull, \
|
httpd.domainFull,
|
||||||
httpd.onionDomain, \
|
httpd.onionDomain,
|
||||||
httpd.port, \
|
httpd.port,
|
||||||
httpd.recentPostsCache, \
|
httpd.recentPostsCache,
|
||||||
httpd.followersThreads, \
|
httpd.followersThreads,
|
||||||
httpd.federationList, \
|
httpd.federationList,
|
||||||
httpd.sendThreads, \
|
httpd.sendThreads,
|
||||||
httpd.postLog, \
|
httpd.postLog,
|
||||||
httpd.cachedWebfingers, \
|
httpd.cachedWebfingers,
|
||||||
httpd.personCache, \
|
httpd.personCache,
|
||||||
httpd.allowDeletion, \
|
httpd.allowDeletion,
|
||||||
httpd.useTor, \
|
httpd.useTor,
|
||||||
httpd.projectVersion, \
|
httpd.projectVersion,
|
||||||
httpd.debug):
|
httpd.debug):
|
||||||
indexLines.remove(line)
|
indexLines.remove(line)
|
||||||
os.remove(postFilename)
|
os.remove(postFilename)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# move to the outbox
|
# move to the outbox
|
||||||
outboxPostFilename= \
|
outboxPostFilename = \
|
||||||
postFilename.replace('/scheduled/','/outbox/')
|
postFilename.replace('/scheduled/', '/outbox/')
|
||||||
os.rename(postFilename,outboxPostFilename)
|
os.rename(postFilename, outboxPostFilename)
|
||||||
|
|
||||||
print('Scheduled post sent '+postId)
|
print('Scheduled post sent ' + postId)
|
||||||
|
|
||||||
indexLines.remove(line)
|
indexLines.remove(line)
|
||||||
if len(indexLines)>maxScheduledPosts:
|
if len(indexLines) > maxScheduledPosts:
|
||||||
deleteSchedulePost=True
|
deleteSchedulePost = True
|
||||||
|
|
||||||
# write the new schedule index file
|
# write the new schedule index file
|
||||||
scheduleIndexFile=baseDir+'/accounts/'+handle+'/schedule.index'
|
scheduleIndexFile = \
|
||||||
scheduleFile=open(scheduleIndexFile, "w+")
|
baseDir + '/accounts/' + handle + '/schedule.index'
|
||||||
|
scheduleFile = open(scheduleIndexFile, "w+")
|
||||||
if scheduleFile:
|
if scheduleFile:
|
||||||
for line in indexLines:
|
for line in indexLines:
|
||||||
scheduleFile.write(line)
|
scheduleFile.write(line)
|
||||||
scheduleFile.close()
|
scheduleFile.close()
|
||||||
|
|
||||||
def runPostSchedule(baseDir: str,httpd,maxScheduledPosts: int):
|
|
||||||
|
def runPostSchedule(baseDir: str, httpd, maxScheduledPosts: int):
|
||||||
"""Dispatches scheduled posts
|
"""Dispatches scheduled posts
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
# for each account
|
# for each account
|
||||||
for subdir,dirs,files in os.walk(baseDir+'/accounts'):
|
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
|
||||||
for account in dirs:
|
for account in dirs:
|
||||||
if '@' not in account:
|
if '@' not in account:
|
||||||
continue
|
continue
|
||||||
# scheduled posts index for this account
|
# scheduled posts index for this account
|
||||||
scheduleIndexFilename=baseDir+'/accounts/'+account+'/schedule.index'
|
scheduleIndexFilename = \
|
||||||
|
baseDir + '/accounts/' + account + '/schedule.index'
|
||||||
if not os.path.isfile(scheduleIndexFilename):
|
if not os.path.isfile(scheduleIndexFilename):
|
||||||
continue
|
continue
|
||||||
updatePostSchedule(baseDir,account,httpd,maxScheduledPosts)
|
updatePostSchedule(baseDir, account, httpd, maxScheduledPosts)
|
||||||
|
|
||||||
def runPostScheduleWatchdog(projectVersion: str,httpd) -> None:
|
|
||||||
|
def runPostScheduleWatchdog(projectVersion: str, httpd) -> None:
|
||||||
"""This tries to keep the scheduled post thread running even if it dies
|
"""This tries to keep the scheduled post thread running even if it dies
|
||||||
"""
|
"""
|
||||||
print('Starting scheduled post watchdog')
|
print('Starting scheduled post watchdog')
|
||||||
postScheduleOriginal= \
|
postScheduleOriginal = \
|
||||||
httpd.thrPostSchedule.clone(runPostSchedule)
|
httpd.thrPostSchedule.clone(runPostSchedule)
|
||||||
httpd.thrPostSchedule.start()
|
httpd.thrPostSchedule.start()
|
||||||
while True:
|
while True:
|
||||||
time.sleep(20)
|
time.sleep(20)
|
||||||
if not httpd.thrPostSchedule.isAlive():
|
if not httpd.thrPostSchedule.isAlive():
|
||||||
httpd.thrPostSchedule.kill()
|
httpd.thrPostSchedule.kill()
|
||||||
httpd.thrPostSchedule= \
|
httpd.thrPostSchedule = \
|
||||||
postScheduleOriginal.clone(runPostSchedule)
|
postScheduleOriginal.clone(runPostSchedule)
|
||||||
httpd.thrPostSchedule.start()
|
httpd.thrPostSchedule.start()
|
||||||
print('Restarting scheduled posts...')
|
print('Restarting scheduled posts...')
|
||||||
|
|
||||||
def removeScheduledPosts(baseDir: str,nickname: str,domain: str) -> None:
|
|
||||||
|
def removeScheduledPosts(baseDir: str, nickname: str, domain: str) -> None:
|
||||||
"""Removes any scheduled posts
|
"""Removes any scheduled posts
|
||||||
"""
|
"""
|
||||||
# remove the index
|
# remove the index
|
||||||
scheduleIndexFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/schedule.index'
|
scheduleIndexFilename = \
|
||||||
|
baseDir + '/accounts/' + nickname + '@' + domain + '/schedule.index'
|
||||||
if os.path.isfile(scheduleIndexFilename):
|
if os.path.isfile(scheduleIndexFilename):
|
||||||
os.remove(scheduleIndexFilename)
|
os.remove(scheduleIndexFilename)
|
||||||
# remove the scheduled posts
|
# remove the scheduled posts
|
||||||
scheduledDir=baseDir+'/accounts/'+nickname+'@'+domain+'/scheduled'
|
scheduledDir = baseDir + '/accounts/' + \
|
||||||
|
nickname + '@' + domain + '/scheduled'
|
||||||
if not os.path.isdir(scheduledDir):
|
if not os.path.isdir(scheduledDir):
|
||||||
return
|
return
|
||||||
for scheduledPostFilename in os.listdir(scheduledDir):
|
for scheduledPostFilename in os.listdir(scheduledDir):
|
||||||
filePath=os.path.join(scheduledDir,scheduledPostFilename)
|
filePath = os.path.join(scheduledDir, scheduledPostFilename)
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(filePath):
|
if os.path.isfile(filePath):
|
||||||
os.remove(filePath)
|
os.remove(filePath)
|
||||||
except:
|
except BaseException:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue