Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Method to kick off all harvests (protected by application key)
Browse files Browse the repository at this point in the history
The intent of this route and method are for a cron-like job to kick off
the process. As such, we need to handle this in a non-logged in state.
The application defined key should suffice for our level of protection
needs.

Other changes include: all Harvests default to frequency of 1 and that
cannot be changed in the UI. Admins can still kick off a harvest with a
greater frequency for catchup or testing purposes in the manual harvest
start form.
  • Loading branch information
JPrevost committed Jun 29, 2015
1 parent 363a00f commit 9557fdd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
16 changes: 14 additions & 2 deletions app/controllers/Application.scala
Expand Up @@ -228,10 +228,10 @@ object Application extends Controller with Security {
"id" -> ignored(0),
"publisher_id" -> ignored(1),
"name" -> nonEmptyText,
"protocol" -> nonEmptyText,
"protocol" -> default(nonEmptyText, "oai-pmh"),
"service_url" -> nonEmptyText,
"resource_url" -> nonEmptyText,
"freq" -> number,
"freq" -> default(number, 1),
"start" -> date,
"updated" -> ignored(new Date)
)(Harvest.apply)(Harvest.unapply)
Expand Down Expand Up @@ -270,6 +270,18 @@ object Application extends Controller with Security {
).getOrElse(NotFound(views.html.static.trouble("No such harvest: " + id)))
}

def startAllHarvests(key: String) = Action { implicit request =>
val authorized_key = Play.configuration.getString("auth.harvest.key").get
if (key == authorized_key) {
Harvest.all.map{ h =>
harvester ! h
h.complete }
Ok("kicked off all harvests")
} else {
Unauthorized(views.html.static.trouble("You are not authorized"))
}
}

def pullKnownItem(cid: Int, hid: Int, oid: String, force: Boolean) =
isAuthenticated { identity => implicit request =>
Collection.findById(cid).map ( coll =>
Expand Down
6 changes: 6 additions & 0 deletions app/models/Harvest.scala
Expand Up @@ -67,6 +67,12 @@ object Harvest {
}
}

def all: List[Harvest] = {
DB.withConnection { implicit c =>
SQL("SELECT * FROM harvest").as(harv *)
}
}

def findById(id: Int): Option[Harvest] = {
DB.withConnection { implicit c =>
SQL("select * from harvest where id = {id}").on('id -> id).as(harv.singleOpt)
Expand Down
3 changes: 1 addition & 2 deletions app/views/harvest/create.scala.html
Expand Up @@ -8,10 +8,9 @@
<h2>Define a content harvest</h2>
@form(routes.Application.createHarvest(pub.id)) {
@inputText(harvestForm("name"))
@inputText(harvestForm("protocol"))
@inputText(harvestForm("protocol"), 'placeholder -> "oai-pmh is the only supported protocol at this time.")
@inputText(harvestForm("service_url"))
@inputText(harvestForm("resource_url"))
@inputText(harvestForm("freq"))
@inputDate(harvestForm("start"))
<input id="submit" type="submit" value="Create">
}
Expand Down
4 changes: 4 additions & 0 deletions conf/application.conf
Expand Up @@ -71,3 +71,7 @@ auth.client.external_auth_url="https://oidc.mit.edu/authorize?client_id=%s&redir
auth.client.external_auth_url=${?AUTH_EXTERNAL_URL}
auth.client.callback_url="http://localhost:9000/_oauth-callback"
auth.client.callback_url=${?AUTH_CALLBACK_URL}

# keys allowed to start non-authenticated harvests
auth.harvest.key = ""
auth.harvest.key = ${?HARVEST_KEY}
1 change: 1 addition & 0 deletions conf/routes
Expand Up @@ -36,6 +36,7 @@ GET /harvest/:id controllers.Application.harvest(id: Int)
GET /harvest/:id/start controllers.Application.startHarvest(id: Int)
GET /harvest/:id/delete controllers.Application.deleteHarvest(id: Int)
GET /knip/:cid/:hid/:oid controllers.Application.pullKnownItem(cid: Int, hid: Int, oid: String, force: Boolean ?= false)
GET /harvests/startall/:key controllers.Application.startAllHarvests(key: String)

# Scheme pages
GET /schemes controllers.Application.schemes
Expand Down
1 change: 0 additions & 1 deletion test/integration/HarvestPagesSpec.scala
Expand Up @@ -225,7 +225,6 @@ class HarvestPagesSpec extends Specification {
browser.$("#protocol").text("protocol")
browser.$("#service_url").text("http://www.example.com/oai2d")
browser.$("#resource_url").text("http://www.example.com/record/${recordId}/")
browser.$("#freq").text("1")
browser.$("#submit").click
browser.pageSource must contain("Valid date required")

Expand Down
22 changes: 22 additions & 0 deletions test/unit/HarvestSpec.scala
Expand Up @@ -14,6 +14,28 @@ class HarvestSpec extends Specification {

"Harvest model" should {

"#all" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val u = User.make("bob", "bob@example.com", "pass", "roley")
val p = Publisher.make(u.id, "pubtag", "pubname", "pubdesc", "pubcat", "pubstatus", Some(""), Some(""))
Harvest.all must haveSize(0)

val h = Harvest.make(p.id, "name", "protocol", "http://www.example.com", "http://example.org", 1, new Date)
Harvest.all must haveSize(1)
Harvest.all must contain(h)

val h2 = Harvest.make(p.id, "name2", "protocol", "http://www.example.com", "http://example.org", 1, new Date)
Harvest.all must haveSize(2)
Harvest.all must contain(h)
Harvest.all must contain(h2)

Harvest.delete(h2.id)
Harvest.all must haveSize(1)
Harvest.all must contain(h)
Harvest.all must not contain(h2)
}
}

"#findById" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
User.create("bob", "bob@example.com", "pass", "roley")
Expand Down

1 comment on commit 9557fdd

@richardrodgers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.