Have You Used Symfony’s Workflow Component?

This week we’ve started two new series – one taking a beginners look at Git, and another covering the awesome Symfony Workflow Component.

The Git series is a member request. I’m always open to doing member requests, so do get in touch if you have a topic in mind.

I’ve wanted to record a series on the Workflow Component since – as best I can remember – at least a couple of months before Christmas. But there were two things stopping me:

  1. Lack of actually having used it 🙂
  2. Lack of understanding where I might use it

It took a determined effort on my part to finally find time to sit down, new up a Symfony project, and get started playing around with it.

In doing this, I took at look at the demo code (more on that below), and tried to work my way backwards, but came unstuck.

Don’t get me wrong, there’s a bunch of interesting and useful things to be learned from doing this, for example the snippet of JavaScript to mark the current ‘place’ on the diagram https://github.com/lyrixx/SFLive-Paris2016-Workflow/blob/89c5a671a765896615af6173ca85cabbf9404a60/app/Resources/views/article/show.html.twig#L51. But this is the overly complex approach.

By starting with a new project and building up my knowledge incrementally, I was able to gather a momentum in learning much faster than I had initially anticipated. Isn’t this so often the case? New things seem scary and difficult when viewed as a whole, but when broken down into chunks and tackled piece by piece, suddenly things don’t seem quite so daunting.

Anyway in doing this I realised I was sitting on the solution to a problem in an app I’ve been working on for over a year now. I had this rather annoying situation whereby my app would run a cron job every minute to figure out if there was anything new to post out to social media. If there was, this would be added to RabbitMQ, which has an army of waiting workers, each designed to do one small task, and then pass on the responsibility downstream.

As part of this, the post could be in various stages – pending, posting, posted, failed, retrying – and I had managed, even in my small scale testing, to have found posts jumping about all over the shop – from pending to posted without ever posting, from pending to retrying without failing… the possibilities were annoying, and the outcome was one of frustration. I shelved my work on the app around August and vowed to return once I found a suitable solution.

In learning about the Workflow Component I was fairly sure I’d found a potential solution. I’m not saying simply adding a workflow definition solved my problems, but in doing so it did stop my entities from transitioning to invalid states. And when these transitions failed, I was able to more easily log out important information, and isolate the root cause of the problem. Actually the root cause turned out to be my lack of understanding of Rabbit MQ, but that’s a different story 🙂

Video Update

This week saw three new videos added to the site:

https://codereviewvideos.com/course/beginners-guide-to-git/video/getting-started-with-git

First up we have an introduction to Git. In this series we cover all my most frequently used commands when working with Git on a day-to-day basis.

I use Git now for every project I do, and have done for at least the last three years. I still have a client who insists on SVN, and by comparison it is nothing but pain, fear, and frustration.

Anyway, I fully appreciate that when initially beginning with Git, it is akin to learning a whole new programming language. The good news is, much like in pretty much any programming language I have ever used, even though there are often 100s (if not 1000s) of commands / functions available, for most tasks you only ever need the same few.

Git is no different.

Sure, there are a bunch of advanced ways to use Git (submodules, anybody?) but mostly it’s just about adding, removing, committing, and pushing.

In this video we cover the most commonly used basics whether working alone, or in a team. In the next video we will cover branches, which whilst incredibly helpful if you’re working alone, become even more powerful – and potentially problematic – when working in a team.

https://codereviewvideos.com/course/symfony-workflow-component-tutorial/video/workflow-component-introduction-demo

Next up is the start of a new series covering Symfony’s Workflow Component.

This is a new component available either:

  • standalone;
  • ready-to-go in Symfony 3.2 onwards;
  • or available as a third party bundle (fduch/workflow-bundle – https://github.com/fduch/workflow-bundle) if you’re still on Symfony 2.

As hinted at earlier, the Workflow Component is all about helping you manage the lifecycle / processes that your objects / entities go through as they pass through your system. Whilst that might sound a little dry / technical, the chances are you have some small, or large part of your existing and future projects that could readily benefit from using this tool.

To begin with we take a look at the rather brilliant Workflow Demo App that was created by Grégoire Pineau for his reveal of the Workflow Component at Symfony Live Paris 2016. In this video we will cover the two examples available – workflows, and state machines – and when and why you may wish to use each.

The Workflow Component brings with it a fantastic visual tool in the form of the Dumper, allowing you to output diagrams which represent the processes you’ve created via your Workflow definitions.

https://codereviewvideos.com/course/symfony-workflow-component-tutorial/video/a-simple-state-machine-example

In the previous video you got to see both workflows and state machines in action.

However, whilst seeing the power of this component is useful, to get your head around how it all fits together is somewhat easier with a more basic example.

There’s loads of interesting things covered in this video – and starting with a new Symfony application you can code along and learn as you. It really is the very best way to inwardly digest this material.

By the end of this video you will have a good understanding of:

  • how and why workflows and state machines differ
  • how workflow definitions are created
  • how to create both places and transitions, and what these terms mean
  • how to customise your marking store, and why you might want too
  • how to create your own workflow diagrams, and a potential bug you might encounter

Honestly I love the Workflow Component, and if you don’t already, hopefully after this series you will too.

Until next week, take care, and happy coding.

Chris

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.