Skip to content

Instantly share code, notes, and snippets.

@preaction
preaction / query.sql
Created Jun 4, 2021
Minion Backend queries by function
View query.sql
-- stats()
EXPLAIN SELECT SUM(job.id IS NOT NULL) AS is_active
FROM minion_workers worker
LEFT JOIN minion_jobs job ON worker.id = job.worker AND job.state = 'active'
GROUP BY worker.id;
EXPLAIN SELECT state, COUNT(state) AS jobs, SUM(`delayed` > NOW()) AS `delayed`
FROM minion_jobs
GROUP BY state;
@preaction
preaction / blog-post-1.markdown
Last active Jul 7, 2021
Static Site Generator with Yancy
View blog-post-1.markdown
title published
A Blog Post
2021-03-21

This will appear in the blog because it has a published field in the frontmatter.

@preaction
preaction / Any.pm
Last active Feb 17, 2021
Log::Any v2
View Any.pm
package Log::Any;
=head1 SYNOPSIS
### Basic use
# Get a logger that logs to STDERR
use Log::Any;
my $log = Log::Any->new;
# Shortcut
View MyPlugin.pm
sub register( $self, $site ) {
$site->on( build => sub( $event ) {
for my $page ( $event->pages->@* ) {
$page->dom->find( 'img[src$=.mp3]' )->each( sub( $el ) {
$el->replace( '<audio controls><source type="audio/mp3" src="%s"></audio>', $el->attr( 'src' ) );
} );
}
} );
}
View gist:4ba85579c6bea18dc8b7e2b91bb3a46e
package Yancy::Backend::DBI;
use Mojo::Base -base, -signatures;
use Yancy::Util qw( fill_brackets );
use SQL::Abstract;
my %MAP = (
# schema_name => mapping
# Mapping can be...
# { table => $table_name } # Standard SQL from SQL::Abstract
@preaction
preaction / nginx.conf
Created Jul 19, 2020
nginx docker config
View nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
View gist:edcd513769cb61a1e08637d881626f1b
my $schema = $app->yancy->schema;
for my $key ( keys %$schema ) {
my $props = $schema->{ $key }{ properties };
$schema->{ $key }{ 'x-list-columns' } = [ grep { $props->{$_}{format} ne 'textarea' } keys %$props ];
}
@preaction
preaction / lib-Mojolicious-Plugin-Yancy.diff
Created Mar 20, 2020
patch to fix datetime validation of empty string
View lib-Mojolicious-Plugin-Yancy.diff
diff --git a/lib/Mojolicious/Plugin/Yancy.pm b/lib/Mojolicious/Plugin/Yancy.pm
index 7ed3e54..6ae923e 100644
--- a/lib/Mojolicious/Plugin/Yancy.pm
+++ b/lib/Mojolicious/Plugin/Yancy.pm
@@ -892,6 +892,10 @@ sub _helper_validate {
for my $prop_name ( keys %{ $schema->{properties} } ) {
my $prop = $schema->{properties}{ $prop_name };
+ # These blocks fix problems with validation only. If the
+ # problem is the database understanding the value, it must be
@preaction
preaction / gist:8a9e2cf56e13ff40d3df9fe9e4999bb6
Created Feb 4, 2020
Extend Yancy controller to add a modified by field
View gist:8a9e2cf56e13ff40d3df9fe9e4999bb6
package MyApp::Controller::ModifiedBy;
use Mojo::Base 'Yancy::Controller::Yancy';
sub _add_modified_by {
my ( $c ) = @_;
$c->req->param( modified_by => $c->current_uid );
if ( my $item = $c->req->json ) {
$item->{ modified_by } = $c->current_uid;
}
}
@preaction
preaction / gist:278860ea296d0c5a84f46b1e777491e2
Created Jan 10, 2020
Forbid breaking encapsulation of an object
View gist:278860ea296d0c5a84f46b1e777491e2
package Foo;
use overload '%{}' => sub {
my ( $package ) = caller;
if ( !$package->isa( __PACKAGE__ ) ) {
require Carp;
Carp::cluck( "Use a method instead!" );
}
return $_[0];
};
sub new { return bless { foo => 'bar' }, __PACKAGE__ }