-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from gwu-libraries/t44-scheduling
T44 scheduling. Fixes #44 .
- Loading branch information
Showing
17 changed files
with
262 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
default_app_config = 'ui.rabbit.RabbitWorker' | ||
from sched import start_sched | ||
|
||
start_sched() | ||
default_app_config = 'ui.rabbit.RabbitWorker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import json | ||
from .rabbit import RabbitWorker | ||
from .models import SeedSet, Collection, Seed, Credential | ||
import datetime | ||
import logging | ||
from sfmutils.consumer import EXCHANGE | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
def seedset_harvest(d): | ||
# To get value of Collection id for the associated collection object. | ||
for collection_id in list(Collection.objects.filter( | ||
id=SeedSet.objects.filter(id=d).values('collection')).values('id')): | ||
if 'id' in collection_id: | ||
value=collection_id['id'] | ||
|
||
# To get value of the token for the associated credential object. | ||
for token in list(Credential.objects.filter( | ||
id=SeedSet.objects.filter(id=d).values('credential')).values( | ||
'token')): | ||
if 'token' in token: | ||
credential = token['token'] | ||
|
||
# To get value of platform | ||
for platform in list(Credential.objects.filter( | ||
id=SeedSet.objects.filter(id=d).values('credential')).values( | ||
'platform')): | ||
if 'platform' in platform: | ||
media = platform['platform'] | ||
|
||
# To get list of seeds | ||
seeds = list(Seed.objects.filter(seed_set=d).select_related( | ||
'seeds').values('token', 'uid')) | ||
# To remove empty token values from the list of seeds -- | ||
# Need to update below code | ||
# | ||
# if item['token'] not in seeds: | ||
# item.pop('token', None) | ||
for item in seeds: | ||
if item['token'] == '': | ||
item.pop('token', None) | ||
|
||
# To get harvest type, options and credentials | ||
harvest_type = SeedSet.objects.filter(id=d).values( | ||
'harvest_type')[0]["harvest_type"] | ||
options = json.loads(SeedSet.objects.filter(id=d).values( | ||
'harvest_options')[0]["harvest_options"]) | ||
credential = json.loads(str(credential)) | ||
|
||
# Routing Key | ||
key = ''.join(['harvest.start.',str(media),'.',harvest_type]) | ||
|
||
# message to be sent to queue | ||
# TODO: Unique id | ||
# TODO: Correct path | ||
m = { | ||
'id': d, | ||
'type': harvest_type, | ||
'options': options, | ||
'credentials': credential, | ||
'collection': { | ||
'id': str(value), | ||
'path': '/tmp/collection/'+str(value) | ||
}, | ||
'seeds': seeds, | ||
} | ||
|
||
log.info("Sending %s message to %s with id %s", harvest_type, key, | ||
m["id"]) | ||
log.debug("Message with id %s is %s", m["id"], json.dumps(m, indent=4)) | ||
|
||
# Publish message to queue via rabbit worker | ||
RabbitWorker.channel.basic_publish(exchange=EXCHANGE, | ||
routing_key=key,body=json.dumps(m)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('ui', '0002_auto_20151013_1522'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='seedset', | ||
name='end_date', | ||
field=models.DateTimeField(null=True, blank=True), | ||
), | ||
migrations.AlterField( | ||
model_name='seedset', | ||
name='schedule', | ||
field=models.CharField(max_length=12), | ||
), | ||
migrations.AlterField( | ||
model_name='seedset', | ||
name='start_date', | ||
field=models.DateTimeField(null=True, blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from apscheduler.schedulers.background import BackgroundScheduler | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
log.debug("Instantiating scheduler") | ||
sched = BackgroundScheduler() | ||
|
||
|
||
def get_sched(): | ||
return sched | ||
|
||
|
||
def start_sched(): | ||
log.info("Starting scheduler") | ||
sched.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from jobs import seedset_harvest | ||
from sched import get_sched | ||
from models import SeedSet | ||
|
||
def schedule_harvest(id, schedule, s, e): | ||
sched = get_sched() | ||
|
||
if sched.get_job(str(id)) is not None: | ||
sched.remove_job(str(id)) | ||
|
||
if schedule=='hourly': | ||
sched.add_job(lambda: seedset_harvest(id),id=str(id) , trigger='cron', | ||
hour='*/1', start_date=s, end_date=e) | ||
elif schedule=='daily': | ||
sched.add_job(lambda: seedset_harvest(id),id=str(id), trigger='cron', | ||
day='*/1', start_date=s, end_date=e) | ||
elif schedule=='minutely': | ||
sched.add_job(lambda: seedset_harvest(id),id=str(id), trigger='cron', | ||
minute='*/1', start_date=s, end_date=e) |
Oops, something went wrong.