Skip to content

Commit

Permalink
Completed the cron script and changed moment to use local time.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesronan committed May 16, 2014
1 parent f764775 commit 6cc0622
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
template: Mosh.template('latest-mosh-item'),
render: function() {
this.model.attributes.age
= moment.utc( this.model.attributes.created ).from(moment.utc());
= moment( this.model.attributes.created ).from(moment());
this.$el.html( this.template( this.model.attributes ) );
return this;
},
Expand Down
32 changes: 20 additions & 12 deletions cron/expire-moshes.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/env perl
#!/usr/bin/env perl

#
# Simple script that can be cron'd to expire moshes that are past
Expand All @@ -10,32 +10,40 @@
use DateTime;

my %details = (
dbtype => 'sqlite',
dbtype => 'SQLite',

# If above dbtype is the default SQLite, use this:
database => 'dbname=./db/moshes.sqlite',
database => 'dbname=/path/to/stcm/db/mosh.sqlite',

# For a real DB ie. MySQL, set the dbtype accordingly,
# comment the above database key, and uncomment these.
# database => 'moshes',
# database => 'spinaltapcodemosh',
# username => 'myusername',
# password => 'mypassword',
);

my $dbi_resource = "dbi:$details{dbtype}:$details{database}";
my $dbi = DBI->new($dbi_resource, $details{username}, $details{password});
my $dbi = DBI->connect($dbi_resource, $details{username}, $details{password})
or die("Unable to connect to DB: " . $DBI::errstr);

my $dt_now = DateTime->now();
my %dates = (
2 => $dt_now->

2 => date_previous( months => 1 ),
3 => date_previous( weeks => 1 ),
4 => date_previous( days => 1 ),
5 => date_previous( hours => 1 ),
);

my $sth = $dbi->prepare('DELETE FROM moshes WHERE expiry = ? AND created < ?');
for (my ($expiry, $before_date) = each %dates) {
$sth->execute($expiry, $before_date);
my $sth = $dbi->prepare('DELETE FROM moshes WHERE expiry = ? AND created < ?')
or die("Unable to prepare statement: " . $dbi->errstr);
while (my ($expiry, $before_date) = each(%dates)) {
$sth->execute($expiry, $before_date)
or die("Failed to execute statement: " . $sth->errstr);
}


sub date_previous {
return sprintf "%s %s",
DateTime->now( time_zone => 'local' )->subtract(@_)->ymd,
DateTime->now( time_zone => 'local' )->subtract(@_)->hms;
}


Binary file modified db/mosh.sqlite
Binary file not shown.
6 changes: 6 additions & 0 deletions lib/SpinalTapCodeMosh.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Dancer::Plugin::Database;

use LWP::Simple qw();
use Data::UUID;
use DateTime;
use HTTP::Status qw(:constants);
use JSON qw();
use Digest::SHA1 qw();
Expand Down Expand Up @@ -53,11 +54,16 @@ post '/mosh' => sub {
my %data = map { $_ => params->{$_} }
grep { $_ ~~ \@mosh_fields } keys params('body');

# Set the created datetime to be the local one.
my $dt_now = DateTime->now( time_zone => 'local' );
$data{created} = $dt_now->ymd . " " . $dt_now->hms;

# We want a unique ID, which isn't as long as a Donkey's cock.
$data{id}
= substr Digest::SHA1::sha1_hex( Data::UUID->new->create_str ), 0, 10;
my $inserted = database->quick_insert('moshes', \%data);


my $return_data = { created => $inserted };
if ($inserted) {
$return_data->{mosh} = \%data;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 1 addition & 1 deletion views/minified-css.tt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<link rel="stylesheet" type="text/css" href="/assets/all-9VU1iEwE.css">
<link rel="stylesheet" type="text/css" href="/assets/all-s9BtZspL.css">
2 changes: 1 addition & 1 deletion views/minified-js.tt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script type="text/javascript" src="/assets/all-yrhxqbS4.js"></script>
<script type="text/javascript" src="/assets/all-qh518z6a.js"></script>

0 comments on commit 6cc0622

Please sign in to comment.