Chuck Grimmett

  • Action Scheduler clean up

    Action Scheduler is a library for triggering a WordPress hook to run at some time in the future. It is used in a lot of large plugins to handle background processing of large job queues. It is an extremely useful tool. Unfortunately, it is also easy for bugs to make the queue or logs explode in size.

    I had a fun runaway action scheduler issue to deal with at work today that ended up creating 26.7M rows in the wp_actionscheduler_actions table before we patched the bug, so I had to figure out how to clean it up. I’m blogging about what I learned so future me can doesn’t have to figure it out again.

    Action Scheduler Defaults

    By default, after every run, Action Scheduler cleans up completed and canceled actions older than 31 days. It does not clean up failed actions by default. Failed actions will stay in the wp_actionscheduler_actions table until manually cleaned up.

    You can change the default retention period or statuses that are cleaned up with filters:

    action_scheduler_default_cleaner_statuses

    action_scheduler_retention_period

    Action Scheduler CLI

    wp action-scheduler clean

    wp action-scheduler clean --batch-size=500 --batches=1000

    wp action-scheduler clean --status=failed --batch-size=50 --before='90 days ago'

    Bash

    Bash and sql to delete 50K rows at a time with 2s delay after each delete, pausing to re-assess every 1M rows:

    for i in $(seq 1 20); do echo "Batch $i/20..."; wp db query "DELETE FROM wp_actionscheduler_actions WHERE status = 'canceled' LIMIT 50000;"; sleep 2; done

    Truncate

    The fastest option.

    1. Export pending actions
    2. TRUNCATE TABLE wp_actionscheduler_actions;
    3. Import pending actions

    Processing backed up queues

    1. First try https://github.com/woocommerce/action-scheduler-high-volume
    2. When all else fails, use the CLI, increase the batch size, --force, and if it is big enough to have multiple runs going, make it more efficient by having each run focus on a different group or hook.
  • How NYC handles snow

    It is common knowledge that NYC puts blades on garbage trucks to plow snow:

    One thing I didn’t consider is that switching to electric garbage trucks impacted their effectiveness as snow plows.

    My friend Trevor, who designs and installs efficient energy systems in buildings in NYC, told me a few other things this weekend about how the city handles snow that I didn’t know.

    The Department of Sanitation melts snow using huge snow melting machines because there is nowhere else to put it. Trevor needed a 10ft high pile of snow moved this weekend in order to place a crane over structural supports in the subway system, so the sanitation department brought in loaders, scooped the snow into dump trucks, and hauled it a few blocks away to be melted in giant hot tubs. Here’s an article from the NYPost.

    The hot tubs – which can melt roughly 60 to 120 tons of snow per hour – are stationed at sites like Broad and Water streets in lower Manhattan, where football field-sized mountains of snow are dumped into the melting tubs at 38 degrees Fahrenheit.

    A video:

    Trevor also mentioned that the MTA uses old jet engines mounted to old work cars to blow snow out of yards and to help melt ice on the third rail. Untapped New York has a good article about it:

    How NYC Subway, LIRR, and Metro-North Responds to Snowstorms – Untapped New York
    Winter Storm Gail is starting today, dumping a foot or more of snowfall. Here’s how the NYC subway, Long Island Railroad and Metro-North prepare for storms.
    www.untappedcities.com
  • I asked Eric Davis about the difference between …

    I asked Eric Davis about the difference between bsky.app and bsky.social and why a single login works in both places. His answer:

    so your Personal Data Server (PDS) is hosted by Bluesky (the company) at bsky.social(*)

    when you log into bsky.app you’re actually firing off a request to bsky.social to authenticate (check devtools, it should be visible)

    bsky.app is the main client of bluesky, but there are others like https://skeetdeck.pages.dev/ and some mobile apps. But bsky.app is not privileged, they all work the same — by talking directly to your PDS.

    (*) technically bsky.social is the “entryway” to a fleet of individual PDS servers created as a convenience (https://docs.bsky.app/docs/advanced-guides/entryway) but not important here. But if you ever see “mushroom servers” mentioned, those are the individual PDS servers. There are 66 right now, each around 500k users.

    every PDS in operation has a default domain and new user registrations become a subdomain of it — hence why most everybody is “NAME.bsky.social”

    TIL!

  • TIL ch is a length unit in CSS. …

    TIL ch is a length unit in CSS. So if you want to limit something to roughly 50 characters and add an ellipsis at the end if there is overflow, you can do something like:

    .className {
        max-width: 50ch;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
  • Improving Jekyll Build Time

    Improving Jekyll Build Time

    When I migrated all of my old posts to Jekyll, my build time skyrocketed. The average time was 39 seconds for 853 posts. Abysmal. This is especially annoying when I’m actively working on the site with jekyll serve and waiting 40 seconds for changes to reflect.

    The first step to figuring out what takes so long is running the Liquid profiler: $ jekyll build --profile

    This is what it output for me:

    Filename                                                | Count |    Bytes |  Time --------------------------------------------------------+-------+----------+------ _layouts/default.html                                   |   853 | 9652.42K | 7.462 search/feed.json                                        |     1 | 1394.16K | 4.148 _includes/header.html                                   |   853 | 1230.35K | 3.485 _layouts/post.html                                      |   755 | 5119.00K | 2.301 _includes/opengraph.html                                |   853 |  870.67K | 1.694 _includes/icons.html                                    |   810 | 1752.89K | 1.454 sitemap.xml                                             |     1 |  203.34K | 1.321 _includes/footer.html                                   |   853 |  109.12K | 0.273 til/posts.yml                                           |     1 |   14.27K | 0.147 about.md                                                |     1 |   18.02K | 0.085 _posts/2016-07-04-posts-heatmap-calendar.md             |     1 |   16.78K | 0.080 isotope.html                                            |     1 |  389.52K | 0.048 _layouts/book.html                                      |    34 |  198.47K | 0.039 til.md                                                  |     1 |   58.76K | 0.023 feed.xml                                                |     1 |  139.11K | 0.022 ... with a dozen more tiny blog index pages from paginator  done in 39.359 seconds. 

    I tried a bunch of random stuff that made tiny improvements before I started doing research: Removing extraneous files, cleaning up if blocks that I no longer used, hardcoding things in the header that relied on variables from _config.yml, etc. None of this made significant improvements. I had already moved extraneous plugins long ago and I keep Jekyll up to date, so there was nothing more to do there.

    I eventually came across this post by Mike Neumegen, which mentioned jekyll-include-cache, which caches the files you tell it to with the first instance and then serves the cached versions up for subsequent requests. For includes that don’t change based on the page content, you can replace {% include example.html %} with {% include_cached example.html %}. When I did this with my sidebar, nav bar, and footer, it cut my average first build time to 28 seconds, with subsequent regeneration times around 19 seconds. Awesome! (Neumegen’s post, which mentioned jekyll-include-cache contains a lot of other helpful advice for Jekyll build optimization. Check it out!)

    My next step was to look at other includes to see if I could shave more time off. Some files had a mix of static and dynamic assets, so I split out all of the static assets into more include_cached includes. This cut another 5-6 seconds off of my build time. Boom.

    Now that my layouts and my includes were as optimized as I could get them without major rewrites, I turned to the next major time hog: search/feed.json. 4 seconds for a single file! This file loops through everything on the site and dumps it into a single file so that I can crawl it with Javascript to power search on my site.

    I used Mat Hayward’s search project with a few tweaks. As I dug deeper on the specific functions in the feed.json generator, I noticed that everything was being converted from Markdown to HTML first, then HTML stripped out, then turned into JSON. I wondered if skipping the markdown conversion step would lead to inaccurate search, so I tried it. Not only did my search function the same, but it dropped feed.json’s build time down to around a sixth of a second.

    My build time now looks like this:

    Filename                                                | Count |    Bytes |  Time --------------------------------------------------------+-------+----------+------ _layouts/default.html                                   |   853 | 9564.71K | 2.922 _includes/head.html                                     |   853 | 1571.79K | 2.623 _includes/opengraph.html                                |   853 |  870.67K | 2.171 sitemap.xml                                             |     1 |  203.34K | 1.177 _layouts/post.html                                      |   755 | 5195.80K | 0.509 search/feed.json                                        |     1 | 1394.65K | 0.176 _posts/2016-07-04-posts-heatmap-calendar.md             |     1 |   16.78K | 0.091 about.md                                                |     1 |   18.02K | 0.076 isotope.html                                            |     1 |  389.52K | 0.050 _layouts/book.html                                      |    34 |  198.47K | 0.030 _includes/header.html                                   |     1 |    1.43K | 0.021 til.md                                                  |     1 |   58.76K | 0.019 feed.xml                                                |     1 |  139.24K | 0.018 ... with a dozen more tiny blog index pages from paginator  done in 19.594 seconds. 

    Regeneration times when running jekyll serve are hovering around 13 seconds now, which is a significant quality of life improvement for me when managing this blog.

    There is probably more work I can do with moving the highlighter to the front end with JS and simplifying the Liquid in head.html and opengraph.html, but I’m stopping here for now.

  • Troubleshooting Problems Installing Craft CMS on Laravel Homestead

    Troubleshooting Problems Installing Craft CMS on Laravel Homestead

    Today I installed Craft CMS in for a new Praxis project. I use Laravel Homestead for my local development environment. It installs like pretty much every other PHP app on Homestead (use this guide by Matt Collins if you aren’t very familiar with Homestead), but I ran into a few annoying errors along the way:

    Mcrypt is required

    If Mcrypt is required shows when you first go to http://yoursite.local/admin to finish the install, it is probably because you are a good citizen and your Homestead box is actually up to date, running PHP 7.2 as the default. Here’s the issue: mcrypt was removed from PHP 7.2. Craft 2 still needs it. There are three solutions:

    • If you need PHP 7.2, you’ll have to install Craft 3. It is still under development as of this writing, so I didn’t take that path. (Update April 2, 2018 – Apparently Craft 3 is launching on April 4, so you won’t have this issue for long!)
    • You can install the mcrypt extension for PHP 7.2.
    • You can use a different PHP version. I took this route. Homestead makes this super simple by allowing multiple PHP versions. In your Homestead.yaml, set the php variable to php: "7.0" under your site in the Sites block. Here is what mine looks like:
    - map: craft-praxis.local      to: /home/vagrant/craft-praxis/public      php: "7.0"

    GROUP BY incompatible with sql_mode=only_full_group_by

    If you see this message, chances are that you are using MySQL 5.7.5+, which changed the way it deals with GROUP BY. See this StackOverflow post for the two solutions: Removing the ONLY_FULL_GROUP_BY option from sql_mode or adding the initSQLs database config settings in craft/config/db.php.

  • Changing the CSV Delimiter and Enclosure Characters on a Mac

    Changing the CSV Delimiter and Enclosure Characters on a Mac

    TIL that you can’t change the CSV delimiter or enclosure characters on Excel for Mac or Apple Numbers. You have to use Open Office, and even there it is tricky. You need to go to File > Save As, select Text CSV, then make sure you check the “Edit Filter Settings” checkbox:

    CSV Open Office Filter Settings

    Then you get the field option AFTER you hit Save, only if the Edit Filter settings box is checked:

    CSV Open Office Field Settings

  • Building JSON for WPComplete in Excel with Concatenate

    Building JSON for WPComplete in Excel with Concatenate

    I’m using the ImportWP Pro plugin to quickly create pages in the Praxis curriculum portal. One of the custom fields I need to set is for the WPComplete course system we use. It takes JSON for which course the page belongs to and which page to redirect the to once the current page is marked complete. Here is an example:

    {"course":"New Module 4 - Thinking","redirect":{"title":"You Have No Idea How Wrong You Are","url":"https://portal.discoverpraxis.com/bootcamp/new-module-4/no-idea-how-wrong"}}

    I needed to build this JSON in Excel with the rest of the page info. The pages I’m setting the redirect title and URL for are being created in the spreadsheet, too. So I wanted to build this JSON cell from the contents of other cells. The commas and quotes were a problem for my typical way of building text from other cells: =B5&" "&C5

    Here is the new solution I found: Break the JSON down into constituent parts, put each in its own cell, then build the JSON with the concatenate() function.

    Here are the 5 cells I needed:

    1. {"course":"New Module 4 - Thinking","redirect":{"title":"
    2. The title from a spreadsheet column
    3. ","url":"https://portal.discoverpraxis.com/bootcamp/new-module-4/
    4. The page slug from a spreadsheet column
    5. "}}

    I put 1 in A23, 3 in A24, and 5 in A25. Then pulled 2 and 4 from various cells in columns A and B. Then my JSON cells used this formula: =CONCATENATE(A23,A4,A24,B4,A25)

    Concatenated together, it looks like this:

    {"course":"New Module 4 - Thinking","redirect":{"title":"PAGETITLE","url":"https://portal.discoverpraxis.com/bootcamp/new-module-4/PAGESLUG"}}

    Excel Concatenate

  • macOS Excel Fill Down Keyboard Shortcut

    I never remember the keyboard shortcut for Fill Down in Excel on macOS, so I’m posting it here where I’ll have it.

    Control + D

  • How to Back Up Your Laravel Homestead Databases

    Today I upgraded from Homestead 4.0.0 to 5.0.1 so I could test my sites in PHP 7. That was a major upgrade and a number of things changed, so I decided that I needed to back up my databases before I did the upgrade. I’d only ever dumped specific databases before, but TIL how to dump them all at once. Here is how to do that in Laravel Homestead:

    1. $ cd ~/Homestead
    2. $ vagrant ssh
    3. $ mysqldump -u homestead -psecret --all-databases > Code/homestead-YYYY-MM-DD-hh-mm.sql
    4. Test the sql file. I had a syntax error the first time and it only wrote out an error message. You’ve been warned.
    5. Move the SQL file somewhere safe. IE outside of any folder you might delete or change during the upgrade.
    6. After the upgrade, here is how to reload your databases in Homestead: $ mysql -u homestead -psecret < Code/homestead-YYYY-MM-DD-hh-mm.sql

    I had some major issues with the upgrade. When I first tried it, vagrant up hung at the SSH auth step and never moved. I went down a deep rabbit hole with my friend Eric Davis trying to debug the SSH issues, to no avail. My last trial was to roll back to Homestead 4.0.0, check my vagrant ssh-config settings, then try the upgrade with those settings. Then, miraculously, when I tried the upgrade again, vagrant up worked with no SSH problems! No difference in settings. I’m baffled, but relieved.

    FYI, there are some Homestead.yaml differences between Homestead 4 and 5, so make sure you have some time set aside to read the docs, update your yaml file, and rerun vagrant up --provision a bunch of times to get everything working again.

  • SVG Viewbox

    SVG Viewbox

    Today I wrote an article for the Praxis blog outlining four free charting/visualization tools and showing examples of visualizations created with those tools. One outputs SVGs, but they are a fixed size. But since SVGs are just code, I figured I could make it responsive.

    First I tried the old responsive image trick: width: 100%; height: auto; – This resulted in the SVG scaling, but only the top 10% was visible.

    After some Googling, I came across this article: How to Scale SVG on CSS Tricks. Here I learned about the viewbox attribute. The viewBox does many things:

    • It defines the aspect ratio of the image.
    • It defines how all the lengths and coordinates used inside the SVG should be scaled to fit the total space available.
    • It defines the origin of the SVG coordinate system, the point where x=0 and y=0.

    My original SVG was 848px by 500px. So, I set viewbox="0 0 848 500" and width=100% and removed the height attribute.

    This led to a nice scalable SVG:

    2005200620072008200920102011201220132014201520162017East GateNorth GateNortheast GateSouth GateWest GateHighway 191

  • Persistence with Chrome Dev Tools

    Persistence with Chrome Dev Tools

    TIL that you can save changes you make with Chrome Dev Tools directly to your disk instead of having to copy and paste them out (or forgetting to and accidentally hitting refresh, which I do at least once a day.)

    More info here: https://developers.google.com/web/tools/setup/setup-workflow

  • CSS Confetti

    CSS Confetti

    TIL how to make confetti with CSS. No javascript needed.

    I used this codepen as a base and modified it to my needs. The author, @fionnachan, had an interesting method I hadn’t thought of: Make a div for each piece of confetti, transform and skew each one, and then set a random offset so it looks like each one moves independently when you apply keyframes animation. @for in SCSS makes the random offsets and transforms a breeze.

    The only downside is that it compiles to almost 4000 lines of CSS. I’m going to try a JS version and see how it performs in comparison.

    UPDATE January 7, 2018: I moved to a JS solution based on https://codepen.io/anon/pen/JMOQzE. 50 lines of code + a jQuery dependency that was already getting loaded anyway is much better than 4000 lines of CSS, even if the CSS is novel.

    CSS Confetti

  • WordPress Template Hierarchy

    WordPress Template Hierarchy

    TIL a ton about the WordPress template hierarchy and the conditionals that select them.

    Relevant resources:

    1. https://developer.wordpress.org/themes/basics/template-hierarchy/
    2. https://codex.wordpress.org/Conditional_Tags
  • Genesis Framework’s Hooks and Filters

    Genesis Framework’s Hooks and Filters

    TIL about the Genesis framework’s hooks and filters. I’m disappointed in how poorly they are documented. I spent most of my time digging around in the Genesis base theme’s /lib/ folder deciphering their functions and figuring out what powers what via trial and error.

    Here are a few resources that helped along the way:

    1. https://genesistutorials.com/visual-hook-guide/
    2. https://my.studiopress.com/documentation/hooks/hooks/hook-reference/
    3. https://my.studiopress.com/documentation/filters/genesis-filters/filter-reference/
  • CSS Grid

    CSS Grid

    TIL about CSS Grid and actually used it in a project to lay out blog posts. I put in a fall back to flexbox for older browsers.

    CSS grid blog

  • Basic Elements of Communication

    Basic Elements of Communication

    I listened to most of “A Mind at Play: How Claude Shannon Invented the Information Age” on a road trip today. I learned that all forms of communication share the same basic elements, no matter the sender or medium.

    Claude Shannon, the father of information theory, published “A Mathematical Theory of Communication” in 1948, which laid out these basic elements:

    1. A source produces the message.
    2. A transmitter translates the message into a signal that can be sent through a particular medium.
    3. A medium, over which the signal is sent.
    4. Noise interfering with the signal across the medium.
    5. A receiver, which receives the signal and translates it back into a format that the destination can understand.
    6. A destination, for which the message is intended.

    Here is a diagram I drew of these elements:

    Mathematical Theory of Communication

    All forms of communication share these same basic elements, including:

    • Email
    • Tweets
    • Text messages
    • DNA
    • Radio
    • Telephone conversations
    • Traffic signals
    • Television
    • Telegraph
    • Physical letters
    • Carrier Pigeons
    • Smoke signals
  • Remember to Ask for Help

    Remember to Ask for Help

    Yesterday I worked on building the new Praxis website for 12 hours straight, from 12-12, after spending the morning on coaching calls with Praxis participants. I was getting a lot of good work done, then I hit a wall with coding out this timeline:

    timeline

    I tried a bunch of different solutions for about three hours. Nothing worked. I got frustrated and almost convinced myself that this just couldn’t be done. It was about midnight at this point. I knew how to make the month boxes offset from each other on the left and right and make the whole thing responsive OR make the month boxes overlapping like this and the whole thing static/non-responsive. But not both.

    Then, thankfully, I took a step back for a brief moment. The designer we contracted to design this is well-known and a great web developer in his own right. I was pretty sure that he wouldn’t design something that he couldn’t actually build himself. So I emailed him and asked for guidance.

    Within a few hours he got back to me with a rough solution. He immediately pointed out two things that alluded me:

    1. Making 3 discrete sizes makes much more sense than making everything completely fluid
    2. The floats work well with content in them, but are completely broken without content in them. I tried to mock this up with very little content in it, which produced strange results. With content in place it functions as expected.

    With an hour and a half, I took the rough solution he outlined and turned it into a fully functioning polished version of the design.

    This was a great reminder for me:

    I don’t know all the answers, even if I think I do.

    When I start to get frustrated, I need to take a step back.

    Sometimes I need to ask for help.

  • Responsive Breakpoints in Sass

    Responsive Breakpoints in Sass

    Up until last week, Sass breakpoints were a bit of a mystery to me. I finally figured out how they work.

    First, they are programmatically generated. Define the breakpoints as a map. I use the list here.

    $breakpoints: ( 	xs: 512px, 	sm: 768px, 	md: 896px, 	lg: 1152px, 	xl: 1280px );

    Next, use a mixin and expressions to build the media queries. I use min-width. I leave it as an exercise for the reader if you wish to reverse this to use max-width instead.

    @mixin breakpoint( $breakpoint ) {     @if map-has-key( $breakpoints, $breakpoint ) {         @media ( min-width: #{ map-get( $breakpoints, $breakpoint ) } ) {             @content;         }     } @else if type_of( $breakpoint ) == number and unit( $breakpoint ) == px or unit( $breakpoint ) == em or unit( $breakpoint ) == rem {         @media (min-width: $breakpoint ) {             @content;         }     } @else {         @warn "No value could be retrieved from `#{$breakpoint}`. "                + "It is either not defined in the `$breakpoints` map, or it is an invalid number of px, em, or rem.";     } }

    To use, define your breakpoint styles using @import. Here is an example defining H1 font sizes at three different breakpoints:

    h1 { 		text-transform: capitalize; 		@include breakpoint(xs) { 				font-size: 2.2em; 		} 		@include breakpoint(sm) { 				font-size: 2.7em; 		} 		@include breakpoint(xl) { 				font-size: 3em; 		} }

  • Bulk Clearing Expiration Dates in Restrict Content Pro

    Bulk Clearing Expiration Dates in Restrict Content Pro

    At Praxis we use Restrict Content Pro as the membership system for our curriculum portal. We decided that all grads get access for life, not just during the program. So, I needed a way to clear over 200 member expiration dates. The only bulk method available through the WordPress interface is to set the expiration dates to another date in the future, which would just kick my problem further down the road. So I needed to dust off my SQL knowledge and directly edit the database.

    Step 1: Back up the database.

    Don’t be a fool. Back up your database and test the queries on a local development version first. Never run queries for the first time on production. The backup is also a failsafe that you can restore if something goes wrong despite your testing.

    Step 2: Find the data.

    I saw that all data related to Restrict Content Pro usually had rcp somewhere in the table, column, or key. So I started with the rcp tables. They had nothing to do with expiration dates, so I checked the wp_usermeta table since RCP extends the WordPress users with more functionality. Bingo. There was a column called meta_key with rcp_expiration in it with corresponding date values.

    Step 3: Make sure you are editing the correct data by running a SELECT statement first.

    Sure, you could run your UPDATE statement first, but I like to make sure I am editing the correct data by running a SELECT statement first and then using the same WHERE clause for my UPDATE statement.

    After a few stupid syntax errors, here is the SELECT statement that got exactly what I wanted. This shows the user ID so I can spot check, restricts searching to the rcp_expiration meta key, and looks for values that are not none.

    SELECT user_id, meta_key, meta_value FROM wp_usermeta WHERE meta_key = 'rcp_expiration' AND meta_value != 'none';

    This returned 176 results. When I changed it to show only meta values that were none, I got 31 values. 31+176=207, which is the total number of users. Looking good.

    Step 4: Craft your UPDATE statement.

    Now that we know we selected the correct data with our previous statement, it is time to craft our UPDATE statement.

    Here I’m updating the wp_usermeta table and setting the meta_value to none where the meta_key is rcp_expiration and the corresponding meta_value is not none.

    UPDATE wp_usermeta SET meta_value = 'none' WHERE meta_key = 'rcp_expiration' AND meta_value != 'none';

    I tested this on my local machine and it updated 176 rows. Just like we wanted.

    Step 5: Run the same query on production.

    Now that we’ve tested the query in our development environment and verified that we got the results we wanted, we can run the query on the production database. If you use phpmyadmin and want to triple check that you aren’t messing anything up, you can click the “Simulate Query” button first. (I did.)

    Step 6: Verify things worked.

    Log in to WordPress and check the RCP membership area. Verify that all expiration dates are now set to none. Also verify that your users can still log in. You should have a few user test logins specifically for this purpose. You can also check your site logs throughout the day to make sure people are still logging in. You can’t count on them always letting you know when something doesn’t work. More often than not they will just stop using it. It is up to you to verify everything works as it should!