• Skip to primary navigation
  • Skip to main content

Chris Mcintosh

Full Stack Web Developer

  • Home
  • Blog

PHP

Database Timing Out or Not Persisting Changes on Laravel Chained Jobs

October 3, 2022 by chrismcintosh Leave a Comment

I ran into an issue with a Laravel application I was working on. The issue was related to a long running job. The job generally takes between 10 to 15 minutes to complete.

Before the job kicked off we would change a value in the database to indicate the job was in progress – we’ll call this state as “active”. At the end of the job we would change the value in the database back – we’ll call this state “inactive”. Changing the record back would allow the record to be picked up by other jobs in the future.

At the end of the job (job still in progress) I could see the value as being set to “inactive” (as desired). However once the job was fully over – a peek into the database would show the record as “active” still. So during the job we would get a false positive of our database record being changed. After the job the record would “snap back” to an undesired state of “active”.

I tried multiple things to rectify this issue and get our database changes to stick after the job completed. One thing that worked – but felt “icky” – was chaining a job onto the main job to do database cleanup. Discovering that this chained job worked led me to discover that the DB::transaction was “locking” the record and preventing permanent changes. Once the DB::commit() line fired our “temporary” changes would go away.

In this instance it made more sense to not use a DB transaction method instead of chaining the job on. Especially since this job runs for so long. Hopefully this can help you identify similar issues in your long running jobs. The best course of action for your application might be different than my solution.

Good luck!

Filed Under: Laravel, PHP Tagged With: Database, Laravel, Laravel Jobs

Using The Events Calendar Plugin with Timber and Twig

March 21, 2022 by chrismcintosh 2 Comments

  1. Ensure the Timber Plugin or Composer dependency is installed

  2. Install the Events Calendar Plugin

    This works for the free version. If using the paid version of the plugin – you might have to modify the other steps.

  3. Activate the Events Calendar Plugin

  4. Create the template file for the override

    Create a file at the following location inside the Timber theme tribe/events/v2/default-template.php

  5. Add the code to the template file

    See code snippet 1 below

  6. Render the markup in twig

    See code snippet 2

Code Snippet 1

<?php
/**
 * Default Events Template
 *
 * @package TribeEventsCalendar
 * @version 4.6.23
 *
 */

use Tribe\Events\Views\V2\Template_Bootstrap;

if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}

$context = Timber::get_context();
$context['tribe_markup'] = tribe( Template_Bootstrap::class )->get_view_html();
Timber::render( array( 'events.twig' ), $context );

Code Snippet 2

  <div class="events-container">
    {{tribe_markup}}
  </div>

More related information can be found here https://github.com/timber/timber/issues/312#issue-39623334

Filed Under: PHP, WordPress Tagged With: The Events Calendar, Timber, Twig

Copyright © 2023 · Genesis Sample on Genesis Framework · WordPress · Log in